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