Cassandra - SimpleStrategy需要一个replication_factor策略选项

时间:2013-11-04 23:34:46

标签: java cassandra

我有一个小的java代码,它应该创建一个keypace和columnFamily,并为它添加一些值。 但无论我做什么,我都会遇到一个或另一个错误。

现在我收到此错误 - SimpleStrategy需要一个replication_factor策略选项 而我已将replication_factor声明为1,如下所示 -

keyspaceDef = HFactory.createKeyspaceDefinition(“MyKeyspace”,ThriftKsDef.DEF_STRATEGY_CLASS,1,Arrays .asList(cfDef));

我能够从cqlsh创建KeySpace,只能从Java创建这个问题。 另外,当我从cqlsh检查并在系统中列出Keyspace时,我确实发现了那里创建的密钥空间。

所以什么是错的,我不知道。

以下是我的全部代码:

   package com.examples.cassandra;

   import java.util.Arrays;
   import org.apache.cassandra.thrift.Column;
   import org.apache.cassandra.thrift.ColumnPath;
   import me.prettyprint.cassandra.serializers.StringSerializer;
   import me.prettyprint.cassandra.service.ThriftKsDef;
   import me.prettyprint.cassandra.service.template.ColumnFamilyResult;
   import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate;
   import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater;
   import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate;
   import me.prettyprint.hector.api.Cluster;
   import me.prettyprint.hector.api.Keyspace;
   import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
   import me.prettyprint.hector.api.ddl.ComparatorType;
   import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
   import me.prettyprint.hector.api.exceptions.HectorException;
   import me.prettyprint.hector.api.factory.HFactory;

   public class test {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO 
    Cluster cluster = HFactory.getOrCreateCluster("test-cluster",
            "localhost:9160");

    KeyspaceDefinition keyspaceDef = cluster.describeKeyspace("MyKeyspace");

    // If keyspace does not exist, the CFs don't exist either. => create them.
    if (keyspaceDef == null) {
        ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(
                "MyKeyspace", "ColumnFamilyName", ComparatorType.BYTESTYPE);
        keyspaceDef = HFactory.createKeyspaceDefinition(
                "MyKeyspace", ThriftKsDef.DEF_STRATEGY_CLASS, 1, Arrays
                        .asList(cfDef));    

        cluster.addKeyspace(keyspaceDef);
    }
    Keyspace ksp = HFactory.createKeyspace("MyKeyspace", cluster);
    ColumnFamilyTemplate<String, String> template = 
        new ThriftColumnFamilyTemplate<String, String>(ksp,
                                                       "ColumnFamilyName", 
                                                       StringSerializer.get(),        
                                                       StringSerializer.get());
    ColumnFamilyUpdater<String, String> updater = template.createUpdater("a key");
    updater.setString("domain", "www.datastax.com");
    updater.setString("time", "sdjsakldl");
    updater.setString("ID", "dadsadas");

    try {
        template.update(updater);
    } catch (HectorException e) {
        // do something ...
    }
    updater = template.createUpdater("2 key");
    updater.setString("domain", "www.ax.com");
    updater.setString("time", "sdj");
    try {
        template.update(updater);
    } catch (HectorException e) {
        // do something ...
        System.out.println();
        e.printStackTrace();
    }

    try {
        ColumnFamilyResult<String, String> res = template.queryColumns("2 key");
        String value = res.getString("domain");
        System.out.println(value);
        // value should be "www.datastax.com" as per our previous insertion.
    } catch (HectorException e) {
        // do something ...
        System.out.println();
        e.printStackTrace();
    }
}

private static Object string(String string) {

    return null;
}

}

1 个答案:

答案 0 :(得分:1)

错误表示在使用SimpleStrategy创建键空间时需要设置replication_factor。

但是,当你这样做时:

HFactory.createKeyspaceDefinition("MyKeyspace", ThriftKsDef.DEF_STRATEGY_CLASS, 1, Arrays.asList(cfDef));
cluster.addKeyspace(keyspaceDef);  

您正确创建了键空间的定义。 但是,当你这样做时:

Keyspace ksp = HFactory.createKeyspace("MyKeyspace", cluster);

它看起来不会重复使用以前创建的定义,我会说它会创建一个默认设置的键空间。

HFactory类的Javadoc:http://hector-client.github.io/hector/source/content/API/core/1.0-1/me/prettyprint/hector/api/factory/HFactory.html