Astyanax ClusterContext和/或KeyspaceContext?

时间:2013-06-20 07:39:17

标签: cassandra astyanax

目前我正在以这种方式启动ClusterContext:

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

在单个节点开发环境中运行。我正在使用ClusterContext,因为我还想创建一个键空间,列族等。

我是否还需要启动KeyspaceContext?如果是这样,出于什么目的或单个ClusterContext足以用于密钥空间/列族管理和读/写场景?

如果我启动了KeyspaceContext,我看到,根据连接池监视器,添加了2个主机并激活了它们。如果我关闭单个Cassandra节点,我仍然看到1标记为活动,这是令人困惑的。

感谢。

1 个答案:

答案 0 :(得分:1)

查看netflix的creating a keyspace文档。它显示您使用Keyspace对象创建键空间。在页面的下方还有关于创建列族的详细信息。

您初始化AstyanaxContext然后用来获取Keyspace对象。

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

然后你可以创建一个键空间(下面)。请注意,如果您在forKeyspace(...)函数中指定的键空间尚未创建,则无关紧要。

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

您现在可以使用和重复使用此Keyspace来执行任意数量的插入/删除/键空间和列族创建。

相关问题