Refine search
/** Create the keyspace */ private void createKeySpace( String keyspace ) { logger.info( "Creating keyspace: {}", keyspace ); String strategy_class = getString( properties, "cassandra.keyspace.strategy", "org.apache.cassandra.locator.SimpleStrategy" ); logger.info( "Using strategy: {}", strategy_class ); int replication_factor = getIntValue( properties, "cassandra.keyspace.replication", 1 ); logger.info( "Using replication (may be overriden by strategy options): {}", replication_factor ); // try { ThriftKsDef ks_def = ( ThriftKsDef ) HFactory .createKeyspaceDefinition( keyspace, strategy_class, replication_factor, new ArrayList<ColumnFamilyDefinition>() ); @SuppressWarnings({ "unchecked", "rawtypes" }) Map<String, String> strategy_options = filter( ( Map ) properties, "cassandra.keyspace.strategy.options.", true ); if ( strategy_options.size() > 0 ) { logger.info( "Strategy options: {}", mapToFormattedJsonString( strategy_options ) ); ks_def.setStrategyOptions( strategy_options ); } cluster.addKeyspace( ks_def ); waitForCreation( keyspace ); logger.info( "Created keyspace {}", keyspace ); }
/** Check if the keyspace exsts */ public boolean keySpaceExists( String keyspace ) { KeyspaceDefinition ksDef = cluster.describeKeyspace( keyspace ); return ksDef != null; }
/** * Lazy creates a column family in the keyspace. If it doesn't exist, it will be created, then the call will sleep * until all nodes have acknowledged the schema change */ public void createColumnFamily( String keyspace, ColumnFamilyDefinition cfDef ) { if ( !keySpaceExists( keyspace ) ) { createKeySpace( keyspace ); } //add the cf if ( !cfExists( keyspace, cfDef.getName() ) ) { //default read repair chance to 0.1 cfDef.setReadRepairChance( 0.1d ); cfDef.setCompactionStrategy( "LeveledCompactionStrategy" ); cfDef.setCompactionStrategyOptions( new MapUtils.HashMapBuilder().map("sstable_size_in_mb", "512" ) ); cluster.addColumnFamily( cfDef, true ); logger.info( "Created column family {} in keyspace {}", cfDef.getName(), keyspace ); } }
public static <E> Keyspace createVirtualKeyspace(String keyspace, E keyPrefix, Serializer<E> keyPrefixSerializer, Cluster cluster, ConsistencyLevelPolicy consistencyLevelPolicy, FailoverPolicy failoverPolicy) { return new ExecutingVirtualKeyspace<E>(keyspace, keyPrefix, keyPrefixSerializer, cluster.getConnectionManager(), consistencyLevelPolicy, failoverPolicy, cluster.getCredentials()); }
/** * Shutdown this cluster, removing it from the Map. This operation is * extremely expensive and should not be done lightly. * @param cluster */ public static void shutdownCluster(Cluster cluster) { synchronized (clusters) { String clusterName = cluster.getName(); if (clusters.get(clusterName) != null ) { cluster.getConnectionManager().shutdown(); clusters.remove(clusterName); } } }
public static void main(String[] args) throws Exception { Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); try { Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, StringSerializer.get()); mutator.insert("jsmith", "Standard1", HFactory.createStringColumn("first", "John")); ColumnQuery<String, String, String> columnQuery = HFactory.createStringColumnQuery(keyspaceOperator); columnQuery.setColumnFamily("Standard1").setKey("jsmith").setName("first"); QueryResult<HColumn<String, String>> result = columnQuery.execute(); System.out.println("Read HColumn from cassandra: " + result.get()); System.out.println("Verify on CLI with: get Keyspace1.Standard1['jsmith'] "); } catch (HectorException e) { e.printStackTrace(); } cluster.getConnectionManager().shutdown(); }
configurator.setMaxActive(builder.getMaxActive()); cluster = HFactory.getOrCreateCluster(builder.getCluster(), configurator); consistencyLevelPolicy.setDefaultReadConsistencyLevel(builder.getReadConsistencyLevel()); consistencyLevelPolicy.setDefaultWriteConsistencyLevel(builder.getWriteConsistencyLevel()); keyspace = HFactory.createKeyspace(keyspaceName, cluster, consistencyLevelPolicy); HFactory.createColumnFamilyDefinition(keyspaceName, builder.getGaugeValuesColumnFamily(), ComparatorType.LONGTYPE); if (cluster.describeKeyspace(keyspaceName) == null) { LOGGER.info("Creating Sirona Cassandra '" + keyspaceName + "' keyspace."); cluster.addKeyspace( HFactory.createKeyspaceDefinition(keyspaceName, //
public static void main(String[] args) throws Exception { Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); try { Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, stringSerializer); he.printStackTrace(); cluster.getConnectionManager().shutdown();
public static void main(String[] args) throws Exception { Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); Keyspace keyspace = HFactory.createKeyspace("Keyspace1", cluster); Mutator<String> mutator = HFactory.createMutator(keyspace, stringSerializer); he.printStackTrace(); cluster.getConnectionManager().shutdown();
KeyspaceDefinition keyspaceDefinition = this.cluster.describeKeyspace(this.cassandraMapping.getKeyspaceName()); if (keyspaceDefinition == null) { List<ColumnFamilyDefinition> columnFamilyDefinitions = this.cassandraMapping.getColumnFamilyDefinitions(); keyspaceDefinition = HFactory.createKeyspaceDefinition( this.cassandraMapping.getKeyspaceName(), this.cassandraMapping.getKeyspaceReplicationStrategy(), ); this.cluster.addKeyspace(keyspaceDefinition, true); LOG.debug("Hector write consistency configured to '" + opConsisLvl + "'."); HFactory.createKeyspace("Keyspace", this.cluster, ccl); keyspaceDefinition = null;
public static void main(String[] args) throws Exception { Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); try { Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, stringSerializer); mutator.insert("billing", "Super1", HFactory.createSuperColumn("jsmith", Arrays.asList(HFactory.createStringColumn("first", "John")), stringSerializer, stringSerializer, stringSerializer)); SuperColumnQuery<String, String, String, String> superColumnQuery = HFactory.createSuperColumnQuery(keyspaceOperator, stringSerializer, stringSerializer, stringSerializer, stringSerializer); superColumnQuery.setColumnFamily("Super1").setKey("billing").setSuperName("jsmith"); QueryResult<HSuperColumn<String, String, String>> result = superColumnQuery.execute(); System.out.println("Read HSuperColumn from cassandra: " + result.get()); System.out.println("Verify on CLI with: get Keyspace1.Super1['billing']['jsmith'] "); } catch (HectorException e) { e.printStackTrace(); } cluster.getConnectionManager().shutdown(); } }
Cluster cluster = HFactory.createCluster("StressCluster", cassandraHostConfigurator); KeyspaceDefinition ksDef = cluster.describeKeyspace(commandArgs.workingKeyspace); if (ksDef == null) { ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition( commandArgs.workingKeyspace, commandArgs.workingColumnFamily, ComparatorType.BYTESTYPE); KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition( commandArgs.workingKeyspace, ThriftKsDef.DEF_STRATEGY_CLASS, 1, Arrays.asList(cfDef)); cluster.addKeyspace(newKeyspace, true); commandArgs.keyspace = clc == null ? HFactory.createKeyspace(commandArgs.workingKeyspace, cluster) : HFactory.createKeyspace(commandArgs.workingKeyspace, cluster, clc); commandRunner = new CommandRunner(cluster.getKnownPoolHosts(true)); if ( commandArgs.validateCommand() && commandArgs.getOperation() != Operation.REPLAY) { commandRunner.processCommand(commandArgs);
public static void main(String[] args) throws Exception { Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); Keyspace keyspaceOperator = HFactory.createKeyspace("Keyspace1", cluster); Mutator<String> mutator = HFactory.createMutator(keyspaceOperator, stringSerializer); he.printStackTrace(); cluster.getConnectionManager().shutdown();
public static void main(String[] args) throws Exception { Cluster cluster = HFactory.getOrCreateCluster("TestCluster", "localhost:9160"); if ( cluster.describeKeyspace(DYN_KEYSPACE) != null ) { cluster.dropKeyspace(DYN_KEYSPACE); HFactory.createKeyspaceDefinition(DYN_KEYSPACE, "org.apache.cassandra.locator.SimpleStrategy", 1, Arrays.asList(cfDefStandard, cfDefSuper)); cluster.addKeyspace(keyspaceDefinition); List<KeyspaceDefinition> keyspaces = cluster.describeKeyspaces(); for (KeyspaceDefinition kd : keyspaces) { if ( kd.getName().equals(DYN_KEYSPACE) ) { he.printStackTrace(); cluster.getConnectionManager().shutdown();
final Cluster cluster = _dataAccessLayerFactory.getCluster(); final KeyspaceDefinition ksdef_rdf = cluster.describeKeyspace(keyspaceName); cluster.addKeyspace(createKeyspaceDefinition(keyspaceName)); : HConsistencyLevel.valueOf(_dataAccessLayerFactory.getWriteConsistencyLevel()); final Keyspace keyspace = HFactory.createKeyspace(keyspaceName, cluster, new ConsistencyLevelPolicy() {
private void checkCreateLockSchema() { KeyspaceDefinition keyspaceDef = cluster.describeKeyspace(keyspace.getKeyspaceName()); if (keyspaceDef == null) { ColumnFamilyDefinition cfDef = createColumnFamilyDefinition(); KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition(keyspace.getKeyspaceName(), ThriftKsDef.DEF_STRATEGY_CLASS, lockManagerConfigurator.getReplicationFactor(), Arrays.asList(cfDef)); log.info("Creating Keyspace and Column Family for LockManager with name (KSPS/CF): (" + newKeyspace.getName() + " / " + cfDef.getName()); cluster.addKeyspace(newKeyspace, true); } else { log.info("Keyspace for LockManager already exists. Skipping creation."); // The Keyspace exists but we don't know anything about the CF yet. if (!doesLockCFExist(keyspaceDef)) { // create it ColumnFamilyDefinition cfDef = createColumnFamilyDefinition(); log.info("Creating Column Family for LockManager with name: " + cfDef.getName()); cluster.addColumnFamily(cfDef, true); } else { log.info("Column Family for LockManager already exists. Skipping creation."); } } }
@Override public void createRequiredSchemaEntities() throws DataAccessLayerException { final String keyspaceName = _keyspace.getKeyspaceName(); KeyspaceDefinition ksdef = _factory.getCluster().describeKeyspace(keyspaceName); if (!hasColumnFamily(ksdef, _cf_name)) { ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(keyspaceName, _cf_name); cfDef.setKeyValidationClass(ComparatorType.UTF8TYPE.getClassName()); cfDef.setComparatorType(ComparatorType.UTF8TYPE); cfDef.setCompactionStrategy("LeveledCompactionStrategy"); cfDef.setDefaultValidationClass(ComparatorType.COUNTERTYPE.getClassName()); cfDef.setColumnType(ColumnType.STANDARD); _factory.getCluster().addColumnFamily(cfDef, true); } setDefaultValue(-1L); } }
@Test public void testNonExistingKeyspace() { hThriftClient.open(); // this keyspace won't exist String ksname = "test_ks_" + Thread.currentThread().getName() + Thread.currentThread().getId(); Exception caughtException = null; try { hThriftClient.getCassandra(ksname); } catch (Exception e) { caughtException = e; } assertTrue("if you try to access a non-existent ks, hclient should throw exception", (caughtException !=null) && (caughtException instanceof HInvalidRequestException ) ); // now create the ks KeyspaceDefinition ksdef = HFactory.createKeyspaceDefinition(ksname); Cluster cluster = HFactory.getOrCreateCluster(clusterName, "127.0.0.1:9170"); cluster.addKeyspace(ksdef); // now it should work hThriftClient.getCassandra(ksname); assertTrue(hThriftClient.isOpen()); }
public static void main(String[] args) { Cluster cluster = HFactory.getOrCreateCluster("TestPool", "localhost:9160"); Keyspace keyspace = HFactory.createKeyspace("TestKeyspace", cluster); try { EntityManagerImpl em = new EntityManagerImpl(keyspace, "com.mycompany"); MyPojo pojo1 = new MyPojo(); pojo1.setId(UUID.randomUUID()); pojo1.setLongProp1(123L); pojo1.setColor(Colors.RED); em.persist(pojo1); // do some stuff MyPojo pojo2 = em.find(MyPojo.class, pojo1.getId()); // do some more stuff System.out.println("Color = " + pojo2.getColor()); } finally { cluster.getConnectionManager().shutdown(); } } }
/** * Private helper to initialize the schema */ protected void initializeSchema() throws Exception { if (keyspaceDefinitions.size() == 0) throw new IllegalStateException("no keyspace definitions founds, maybe add some entities to the factory"); for (KeyspaceDefinition keyspaceDefinition : ThriftKsDef.fromThriftList(new ArrayList<KsDef>(keyspaceDefinitions.values()))) { KeyspaceDefinition existingKeyspace = cluster.describeKeyspace(keyspaceDefinition.getName()); if (existingKeyspace == null) { cluster.addKeyspace(keyspaceDefinition, true); } else { for (ColumnFamilyDefinition columnFamilyDefinition : keyspaceDefinition.getCfDefs()) { if (keyspaceContainsColumnFamily(columnFamilyDefinition, existingKeyspace)) { log.debug(String.format("found column family %s, updating schema", columnFamilyDefinition.getName())); ColumnFamilyDefinition existingColumnFamilyDefinition = getColumnFamilyFromKeyspace(columnFamilyDefinition.getName(), existingKeyspace); existingColumnFamilyDefinition.getColumnMetadata().clear(); for (ColumnDefinition columnDefinition : columnFamilyDefinition.getColumnMetadata()) { existingColumnFamilyDefinition.addColumnDefinition(columnDefinition); } cluster.updateColumnFamily(existingColumnFamilyDefinition, true); } else { log.debug(String.format("not found column family %s, adding to schema", columnFamilyDefinition.getName())); cluster.addColumnFamily(columnFamilyDefinition, true); } } } } }