UnavailableException()在Apache-Cassandra 0.8.2 [英] UnavailableException() in Apache-Cassandra 0.8.2
问题描述
我刚接触Apache-Cassandra 0.8.2。我尝试插入一些数据,但得到这个异常。
I new to Apache-Cassandra 0.8.2. I am trying to insert some data but getting this exception.
Exception in thread "main" UnavailableException()
at org.apache.cassandra.thrift.Cassandra$insert_result.read(Cassandra.java:14902)
at org.apache.cassandra.thrift.Cassandra$Client.recv_insert(Cassandra.java:858)
at org.apache.cassandra.thrift.Cassandra$Client.insert(Cassandra.java:830)
at TestCassandra.main(TestCassandra.java:166)
我的代码是:
public class TestCassandra {
public static void createKeySpace( Cassandra.Client client,String ksname)
throws TException, InvalidRequestException, UnavailableException, UnsupportedEncodingException, NotFoundException, TimedOutException, SchemaDisagreementException {
KsDef ksdef = new KsDef();
ksdef.name = ksname;
ksdef.strategy_class = "NetworkTopologyStrategy";
List l = new ArrayList();
ksdef.cf_defs =l;
client.system_add_keyspace(ksdef);
System.out.println("KeySpace Created");
}
public static void createColumnFamily(Cassandra.Client client,String ksname,String cfname)
throws TException, InvalidRequestException, UnavailableException, UnsupportedEncodingException, NotFoundException, TimedOutException, SchemaDisagreementException {
CfDef cfd = new CfDef(ksname, cfname);
client.system_add_column_family(cfd);
System.out.println("ColumnFamily Created");
}
public static void main(String[] args)
throws TException, InvalidRequestException, UnavailableException, UnsupportedEncodingException, NotFoundException, TimedOutException, SchemaDisagreementException {
TTransport tr = new TFramedTransport(new TSocket("localhost", 9160));
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);
tr.open();
String keySpace = "Keyspace1";
String columnFamily = "Users";
//Drop the Keyspace
client.system_drop_keyspace(keySpace);
//Creating keyspace
KsDef ksdef = new KsDef();
ksdef.name = keySpace;
ksdef.strategy_class = "NetworkTopologyStrategy";
List l = new ArrayList();
ksdef.cf_defs =l;
client.system_add_keyspace(ksdef);
System.out.println("KeySpace Created");
//createKeySpace(client,keySpace);
client.set_keyspace(keySpace);
//Creating column Family
CfDef cfd = new CfDef(keySpace, columnFamily);
client.system_add_column_family(cfd);
System.out.println("ColumnFamily Created");
//createColumnFamily(client,keySpace,columnFamily);
ColumnParent parent = new ColumnParent(columnFamily);
Column description = new Column();
description.setName("description".getBytes());
description.setValue("I’m a nice guy".getBytes());
description.setTimestamp(System.currentTimeMillis());
ConsistencyLevel consistencyLevel = ConsistencyLevel.ONE;
ByteBuffer rowid = ByteBuffer.wrap("0".getBytes());
//Line No. 166
client.insert(rowid, parent, description, consistencyLevel);
System.out.println("Record Inserted...");
tr.flush();
tr.close();
}
}
任何人都可以帮我是这样的?
Can anybody help me why this is so?
推荐答案
UnavailableException
的原因是由于在您的 createKeySpace
方法中,您从未为键空间定义指定 replication_factor
, KsDef
。
The reason for the UnavailableException
is due to the fact of in your createKeySpace
method, you have never specified a replication_factor
for your keyspace definition, KsDef
.
2 Strategy类, NetworkTopologyStrategy
和 SimpleStrategy
需要要设置的复制因子。在Cassandra 0.8和更高版本中,在 KsDef
中没有更多 replication_factor
字段,因此您必须自己添加,此外,看到我已将您的 strategy_class
更改为 SimpleStrategy $ c $更新您的代码, c>):
The 2 Strategy class, NetworkTopologyStrategy
and SimpleStrategy
requires a replication factor to be set. In Cassandra 0.8 and higher, there is no more a replication_factor
field in KsDef
so you will have to add it yourself, like so (I've updated your code, but not tested. Also, see that I've changed your strategy_class
to SimpleStrategy
):
KsDef ksdef = new KsDef();
ksdef.name = ksname;
ksdef.strategy_class = SimpleStrategy.class.getName();
//Set replication factor
if (ksdef.strategy_options == null) {
ksdef.strategy_options = new LinkedHashMap<String, String>();
}
//Set replication factor, the value MUST be an integer
ksdef.strategy_options.put("replication_factor", "1");
//Cassandra must now create the Keyspace based on our KsDef
client.system_add_keyspace(ksdef);
对于 NetworkTopologyStrategy
,您需要指定您对自己创建的每个数据中心的复制因素(请参阅此处的说明)。
For NetworkTopologyStrategy
, you will need to specify your replication factor to each datacentre you've created (See explanation here).
有关详情,请查看我的在Java 博客中与Apache Cassandra 0.8接口。
For more information, view my Interfacing with Apache Cassandra 0.8 in Java blog.
这篇关于UnavailableException()在Apache-Cassandra 0.8.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!