0

I have a small java code that should create a keyspace and columnFamily and add some values to it. But whatever I do, i keep getting one or the other error.

Now I am getting this error - SimpleStrategy requires a replication_factor strategy option whereas I have declared replication_factor as 1 as follows -

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

I am able to create KeySpace from cqlsh, only from Java I am having this issue. Also, when I check from cqlsh, and list the Keyspaces in system, I do find the keyspace created there.

So whats is wrong, I have no idea.

Here is my whole code:

   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;
}

}

4

1 回答 1

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

于 2013-11-07T14:37:01.467 回答