Skip to content

Create keyspaces and column families

ash2k edited this page Nov 15, 2012 · 2 revisions

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.

Create a Keyspace

Assuming you already created your keyspace object you can then create the actual keyspace by calling

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()
     );

Dropping a keyspace

keyspace.dropKeyspace()

Create a column family using defaults

ColumnFamily<String, String> CF_STANDARD1 = ColumnFamily
    .newColumnFamily("Standard1", StringSerializer.get(), StringSerializer.get());

keyspace.createColumnFamily(CF_STANDARD1,null);

Create a column family and add 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());

Drop a column family

keyspace.dropColumnFamily(CF_STANDARD1);
Clone this wiki locally
You can’t perform that action at this time.