private ThriftKsDef toThriftImplementation(final KeyspaceDefinition cfdef) { if(cfdef instanceof ThriftKsDef) { return (ThriftKsDef) cfdef; } else { return new ThriftKsDef(cfdef); } } }
@Override public List<KeyspaceDefinition> execute(Cassandra.Client cassandra) throws HectorException { try { return ThriftKsDef.fromThriftList(cassandra.describe_keyspaces()); } catch (Exception e) { throw xtrans.translate(e); } } };
public ThriftKsDef(String keyspaceName, String strategyClass, int replicationFactor, List<ColumnFamilyDefinition> cfDefs) { this.name = keyspaceName; this.strategyClass = strategyClass; setReplicationFactor(replicationFactor); this.cfDefs = cfDefs; }
@Before public void setUp() throws Exception { super.setupClient(); KEYSPACE_DEV = new ThriftKsDef(new KsDef(KEYSPACE, "org.apache.cassandra.locator.SimpleStrategy", Arrays.asList(new CfDef[] { BLOB_CF_DEF }))); ((ThriftKsDef)KEYSPACE_DEV).setReplicationFactor(1); cassandraHostConfigurator = new CassandraHostConfigurator("localhost:9170"); cassandraCluster = new ThriftCluster("Test Cluster", cassandraHostConfigurator); keyspace = HFactory.createKeyspace(KEYSPACE, cassandraCluster); try { cassandraCluster.dropKeyspace(KEYSPACE); } catch (Exception e) { } cassandraCluster.addKeyspace(KEYSPACE_DEV); }
@Override public String execute(Cassandra.Client cassandra) throws HectorException { try { if (waitForSchemaAgreement) { waitForSchemaAgreement(cassandra); } String schemaId = cassandra.system_update_keyspace(toThriftImplementation(ksdef).toThrift()); if (waitForSchemaAgreement) { waitForSchemaAgreement(cassandra); } return schemaId; } catch (Exception e) { throw xtrans.translate(e); } } };
/** 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 ); }
@Override public String execute(Cassandra.Client cassandra) throws HectorException { try { if (waitForSchemaAgreement) { waitForSchemaAgreement(cassandra); } String schemaId = cassandra.system_add_keyspace(toThriftImplementation(ksdef).toThrift()); if (waitForSchemaAgreement) { waitForSchemaAgreement(cassandra); } return schemaId; } catch (Exception e) { throw xtrans.translate(e); } } };
/** * Use createKeyspaceDefinition to add a new Keyspace to cluster. Example: * * String testKeyspace = "testKeyspace"; KeyspaceDefinition newKeyspace = * HFactory.createKeyspaceDefinition(testKeyspace); * cluster.addKeyspace(newKeyspace); * * @param keyspace */ public static KeyspaceDefinition createKeyspaceDefinition(String keyspace) { return new ThriftKsDef(keyspace); }
public ThriftKsDef(String keyspaceName) { this.name = keyspaceName; this.cfDefs = new ArrayList<ColumnFamilyDefinition>(); this.strategyClass = DEF_STRATEGY_CLASS; setReplicationFactor(1); }
@Override public List<KeyspaceDefinition> execute(Cassandra.Client cassandra) throws HectorException { try { return ThriftKsDef.fromThriftList(cassandra.describe_keyspaces()); } catch (Exception e) { throw xtrans.translate(e); } } };
@Override public String execute(Cassandra.Client cassandra) throws HectorException { try { if (waitForSchemaAgreement) { waitForSchemaAgreement(cassandra); } String schemaId = cassandra.system_update_keyspace(toThriftImplementation(ksdef).toThrift()); if (waitForSchemaAgreement) { waitForSchemaAgreement(cassandra); } return schemaId; } catch (Exception e) { throw xtrans.translate(e); } } };
/** * Use createKeyspaceDefinition to add a new Keyspace to cluster. Example: * * String testKeyspace = "testKeyspace"; KeyspaceDefinition newKeyspace = * HFactory.createKeyspaceDefinition(testKeyspace); * cluster.addKeyspace(newKeyspace); * * @param keyspaceName * @param strategyClass * - example: * org.apache.cassandra.locator.SimpleStrategy.class.getName() * @param replicationFactor * - http://wiki.apache.org/cassandra/Operations */ public static KeyspaceDefinition createKeyspaceDefinition( String keyspaceName, String strategyClass, int replicationFactor, List<ColumnFamilyDefinition> cfDefs) { return new ThriftKsDef(keyspaceName, strategyClass, replicationFactor, cfDefs); }
public ThriftKsDef(KsDef k) { Assert.notNull(k, "KsDef is null"); name = k.name; strategyClass = k.strategy_class; strategyOptions = k.strategy_options != null ? k.strategy_options : new HashMap<String, String>(); durableWrites = k.durable_writes; setReplicationFactor(NumberUtils.toInt(strategyOptions.get(REPLICATION_FACTOR), 1)); cfDefs = ThriftCfDef.fromThriftList(k.cf_defs); }
@Override public List<KeyspaceDefinition> execute(Cassandra.Client cassandra) throws HectorException { try { return ThriftKsDef.fromThriftList(cassandra.describe_keyspaces()); } catch (Exception e) { throw xtrans.translate(e); } } };
@Override public String execute(Cassandra.Client cassandra) throws HectorException { try { if (waitForSchemaAgreement) { waitForSchemaAgreement(cassandra); } String schemaId = cassandra.system_add_keyspace(toThriftImplementation(ksdef).toThrift()); if (waitForSchemaAgreement) { waitForSchemaAgreement(cassandra); } return schemaId; } catch (Exception e) { throw xtrans.translate(e); } } };
public static List<KeyspaceDefinition> fromThriftList(List<KsDef> ks) { if (ks == null || ks.isEmpty()) { return Collections.emptyList(); } List<KeyspaceDefinition> l = new ArrayList<KeyspaceDefinition>(ks.size()); for (KsDef k: ks) { l.add(new ThriftKsDef(k)); } return l; }
public ThriftKsDef(KeyspaceDefinition keyspaceDefinition) { name = keyspaceDefinition.getName(); strategyClass = keyspaceDefinition.getStrategyClass(); strategyOptions = keyspaceDefinition.getStrategyOptions(); setReplicationFactor(keyspaceDefinition.getReplicationFactor()); cfDefs = keyspaceDefinition.getCfDefs(); }
/** * 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); } } } } }
@Override public String execute(Cassandra.Client cassandra) throws HectorException { try { if (waitForSchemaAgreement) { waitForSchemaAgreement(cassandra); } String schemaId = cassandra.system_update_keyspace(toThriftImplementation(ksdef).toThrift()); if (waitForSchemaAgreement) { waitForSchemaAgreement(cassandra); } return schemaId; } catch (Exception e) { throw xtrans.translate(e); } } };
@Override public KeyspaceDefinition execute(Cassandra.Client cassandra) throws HectorException { try { return new ThriftKsDef(cassandra.describe_keyspace(keyspace)); } catch (org.apache.cassandra.thrift.NotFoundException nfe) { setException(xtrans.translate(nfe)); return null; } catch (Exception e) { throw xtrans.translate(e); } } };