Skip to content

Create keyspace or column family

elandau edited this page Nov 8, 2012 · 5 revisions

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. With the Keyspace level API astyanax automatically populates certain attributes such as the keyspace name and column family definition using the ColumnFamily definition object.

The API accepts maps of Object for properties and may contain any parameter that is supported by the CLI.

Creating a keyspace

First, create the Keyspace object. 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.getEntity();

Create the keyspace “MyKeyspace”

keyspace.createKeyspace(ImmutableMap.<String, Object>builder()
    .put("strategy_options", ImmutableMap.<String, Object>builder()
        .put("replication_factor", "1")
        .build())
    .put("strategy_class",     "SimpleStrategy")
        .build()
     );

Dropping a keyspace

Drop the keyspace “MyKeyspace”

keyspace.dropKeyspace();

Create a column family

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

Dropping a column family

keyspace.dropColumnFamily(CF_COMPOSITE);
Clone this wiki locally