/** 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 ); }
public void makeKeyspace(String keyspace, List<ColumnFamilyDefinition> cf_defs) throws InvalidRequestException, TException { log.info("Creating keyspace: " + keyspace); try { KeyspaceDefinition ks_def = HFactory.createKeyspaceDefinition(keyspace, "org.apache.cassandra.locator.SimpleStrategy", 1, cf_defs); cluster.addKeyspace(ks_def); log.info("Created keyspace: " + keyspace); } catch (Exception e) { log.error("Unable to create keyspace " + keyspace, e); } }
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."); } } }
public static void createKeyspace(Cluster cluster, String name, String strategy, int replicationFactor, List<CfDef> cfDefList) { try { KsDef ksDef = new KsDef(name, strategy, cfDefList); ksDef.setReplication_factor(replicationFactor); cluster.addKeyspace(new ThriftKsDef(ksDef)); return; } catch (Throwable e) { System.out.println("exception while creating keyspace, " + name + " - probably already exists : " + e.getMessage()); } for (CfDef cfDef : cfDefList) { try { cluster.addColumnFamily(new ThriftCfDef(cfDef)); } catch (Throwable e) { System.out.println("exception while creating CF, " + cfDef.getName() + " - probably already exists : " + e.getMessage()); } } }
@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()); }
@Test public void testNonExistingKeyspaceWithHostAutoDiscover() { hThriftClient.open(); // this keyspace won't exist String ksname = "test_ks_2_" + 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); CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("127.0.0.1"); cassandraHostConfigurator.setPort(9170); cassandraHostConfigurator.setRunAutoDiscoveryAtStartup(true); cassandraHostConfigurator.setAutoDiscoverHosts(true); Cluster cluster = HFactory.getOrCreateCluster(clusterName, cassandraHostConfigurator); cluster.addKeyspace(ksdef); // now it should work hThriftClient.getCassandra(ksname); assertTrue(hThriftClient.isOpen()); } }
public static void createKeyspace(Cluster cluster, String name, String strategy, int replicationFactor, List<CfDef> cfDefList) { try { KsDef ksDef = new KsDef(name, strategy, cfDefList); cluster.addKeyspace(new ThriftKsDef(ksDef)); return; } catch (Throwable e) { System.out.println("exception while creating keyspace, " + name + " - probably already exists : " + e.getMessage()); } /* * for (CfDef cfDef : cfDefList) { try { cluster.addColumnFamily(new * ThriftCfDef(cfDef)); } catch (Throwable e) { * System.out.println("exception while creating CF, " + cfDef.getName() + * " - probably already exists : " + e.getMessage()); } } */ }
/** * 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); } } } } }
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."); } } }
commandArgs.workingKeyspace, ThriftKsDef.DEF_STRATEGY_CLASS, 1, Arrays.asList(cfDef)); cluster.addKeyspace(newKeyspace, true);
1, Arrays.asList(cfDefStandard, cfDefSuper)); cluster.addKeyspace(keyspaceDefinition);
); this.cluster.addKeyspace(keyspaceDefinition, true);
cluster.addKeyspace(createKeyspaceDefinition(keyspaceName));
cluster.addKeyspace(createKeyspaceDefinition(keyspaceName));
cluster.addKeyspace(HFactory.createKeyspaceDefinition( PersistencyConstants.BAM_KEY_SPACE, ThriftKsDef.DEF_STRATEGY_CLASS, 1, columnFamilyDefinitionList));
cluster.addKeyspace( HFactory.createKeyspaceDefinition( keyspaceName, //