-
Notifications
You must be signed in to change notification settings - Fork 353
Create keyspace or column family
Astyanax provides a mechanism to programmatically create keyspaces and column families based on the Keyspace and ColumnFamily objects. The API will pick up the names and validators from the object but will allow for overrides in an operation Map<String, Object>. The API supports all properties supported by the CLI.
Keyspaces and column families may be created programmatically using either the Cluster interface or the Keyspace interface. Use the Cluster interface if your application needs a single connection pool to access the entire cluster. Use the Keyspace interface to operate on a specific keyspace.
First, create the Keyspace client. The actual keyspace doesn’t have to exist in cassandra
AstyanaxContext<Keyspace> ctx = new AstyanaxContext.Builder()
.forKeyspace("MyKeyspace")
... // Additional configuration parameters
.buildKeyspace(ThriftFamilyFactory.getInstance());
ctx.start();
Keyspace keyspace = ctx.getClient();
Create the keyspace “MyKeyspace”
// Using simple strategy
keyspace.createKeyspace(ImmutableMap.<String, Object>builder()
.put("strategy_options", ImmutableMap.<String, Object>builder()
.put("replication_factor", "1")
.build())
.put("strategy_class", "SimpleStrategy")
.build()
);
// Using network topology
keyspace.createKeyspace(ImmutableMap.<String, Object>builder()
.put("strategy_options", ImmutableMap.<String, Object>builder()
.put("us-east", "3");
.put("eu-west", "3");
.build())
.put("strategy_class", "NetworkTopologyStrategy")
.build()
);
Drop the keyspace “MyKeyspace”
keyspace.dropKeyspace();
public static ColumnFamily<String, String> CF_STANDARD1 = ColumnFamily
.newColumnFamily("Standard1", StringSerializer.get(),
StringSerializer.get());
Create a column family using types from the ColumnFamily definition. StringSerializer translates to UTF8Type.
keyspace.createColumnFamily(CF_STANDARD1, null);
Create a column family but override the types
keyspace.createColumnFamily(CF_STANDARD1, ImmutableMap.<String, Object>builder()
.put("default_validation_class", "UTF8Type")
.put("key_validation_class", "UTF8Type")
.put("comparator_type", "UTF8Type")
.build());
Create a column family using type from the ColumnFamily definition and specify secondary indexes
keyspace.createColumnFamily(CF_STANDARD1, ImmutableMap.<String, Object>builder()
.put("column_metadata", ImmutableMap.<String, Object>builder()
.put("Index1", ImmutableMap.<String, Object>builder()
.put("validation_class", "UTF8Type")
.put("index_name", "Index1")
.put("index_type", "KEYS")
.build())
.put("Index2", ImmutableMap.<String, Object>builder()
.put("validation_class", "UTF8Type")
.put("index_name", "Index2")
.put("index_type", "KEYS")
.build())
.build())
.build());
Create a column family for composite comparator
Automatic formatting of the composite comparator hasn’t been implemented yet so the comparator_type must be specified manually.
keyspace.createColumnFamily(CF_COMPOSITE, ImmutableMap.<String, Object>builder()
.put("default_validation_class", "UTF8Type")
.put("key_validation_class", "UTF8Type")
.put("comparator_type", "CompositeType(UTF8Type, LongType)")
.build());
keyspace.dropColumnFamily(CF_COMPOSITE);
A Netflix Original Production
Tech Blog | Twitter @NetflixOSS | Jobs
- Getting-Started
- Configuration
- Features
- Monitoring
- Thread Safety
- Timeouts
- Recipes
- Examples
- Javadoc
- Utilities
- Cassandra-Compatibility
- FAQ
- End-to-End Examples
- Astyanax Integration with Java Driver