Apache-Cassandra 0.8.2中的UnavailableException()

Ana*_*oni 5 java cassandra

我是Apache-Cassandra 0.8.2的新手.我试图插入一些数据,但得到这个例外.

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


}

任何人都可以帮助我为什么会这样?

Buh*_*ndi 6

原因UnavailableException是由于你的createKeySpace方法中的事实,你从来没有replication_factor为你的键空间定义指定一个,KsDef.

2.策略类,NetworkTopologyStrategy并且SimpleStrategy需要设置一个复制的因素.在Cassandra 0.8及更高版本中,没有更多的replication_factor字段,KsDef所以你必须自己添加它,就像这样(我已经更新了你的代码,但没有经过测试.另外,看到我已经改变了你strategy_classSimpleStrategy):

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);
Run Code Online (Sandbox Code Playgroud)

因为NetworkTopologyStrategy,您需要为您创建的每个数据中心指定复制因子(请参阅此处的说明).

有关更多信息,请查看我在Java博客中与Apache Cassandra 0.8的接口.

  • 使用NTS,您必须指定每个数据中心副本计数(即NTS的点).因此,除非您有一个名为replication_factor的数据中心,否则这将无法执行您想要的操作.这是一个常见的错误,我们在0.8.3中添加了一个特殊的检查:ConfigurationException("replication_factor是SimpleStrategy的选项,而不是NetworkTopologyStrategy") (6认同)