简体   繁体   中英

Astyanax ClusterContext and/or KeyspaceContext?

currently I'm starting a ClusterContext that way:

        AstyanaxContext.Builder builder = new AstyanaxContext.Builder()
            .forCluster(clusterName)
            .forKeyspace(keyspaceName)
            .withAstyanaxConfiguration(getAstyanaxProperties(properties))
            .withConnectionPoolConfiguration(getConnectionPoolProperties(properties))
            .withConnectionPoolMonitor(connectionPoolMonitor);

    clusterContext = builder.buildCluster(ThriftFamilyFactory.getInstance());
    clusterContext.start();
    cluster = clusterContext.getEntity();

Running on a single node dev environment. I'm using a ClusterContext, because I also want to create a keyspace, column families etc.

Do I additionally also need to start up a KeyspaceContext? If so, for what purpose or is a single ClusterContext sufficient for keyspace/column family management and read/write scenarios?

If I do start up a KeyspaceContext, I see, according to the connection pool monitor, 2 hosts added and active. If I shutdown the single Cassandra node, I still see 1 marked as active, which is confusing.

Thanks.

Take a look at netflix's documentation for creating a keyspace . It shows that you create keyspaces using the Keyspace object. A little further down the page there are details about creating a column family too.

You initialise an AstyanaxContext with you then use to get your Keyspace object.

AstyanaxContext<Keyspace> ctx = new AstyanaxContext.Builder()
    .forKeyspace("MyKeyspace")
    // Additional configuration parameters
    .buildKeyspace(ThriftFamilyFactory.getInstance());
ctx.start();
Keyspace keyspace = ctx.getClient();

Then you can create a keyspace (below). As a note, it doesnt matter if the keyspace you are specifying in the forKeyspace(...) function isn't created yet.

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

You can now use and reuse this Keyspace to do as many inserts / deletes / keyspace and column-families creations as you wish.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM