尝试通过hector API连接到cassandra时出错

时间:2013-05-21 11:00:02

标签: java cassandra

我编写了用于从cassandra读取记录的java代码。但它给出了运行时错误.. 这是java代码: -

import javax.naming.spi.DirStateFactory.Result;

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.hector.api.*;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.QueryResult;


public class HectorConnect {

public static void main(String args[]){

    try{

        Cluster c = HFactory.getOrCreateCluster("Test", "localhost:9160");
        Keyspace ko = HFactory.createKeyspace("myspace", c);

        StringSerializer se = StringSerializer.get();

        ColumnQuery<String, String> q = HFactory.createColumnQuery(ko, se, se);

        QueryResult<HColumn<String, String>> r = q.setKey("cara").
                setName("customername").
                setColumnFamily("customer").
                execute();

        }
            catch(Exception e){
        e.printStackTrace();
    }
}
}

并且它抛出的错误如下: -

log4j:WARN找不到logger的附加程序(me.prettyprint.cassandra.service.CassandraClientPoolFactory)。 log4j:WARN请正确初始化log4j系统。 线程“main”中的异常java.lang.NoSuchMethodError:org.apache.cassandra.thrift.Cassandra $ Client.describe_keyspaces()Ljava / util / Set;     at me.prettyprint.cassandra.service.CassandraClientImpl.getKeyspaces(CassandraClientImpl.java:141)     at me.prettyprint.cassandra.service.CassandraClientImpl.getKeyspace(CassandraClientImpl.java:114)     at me.prettyprint.cassandra.service.CassandraClientImpl.getKeyspace(CassandraClientImpl.java:104)     at me.prettyprint.cassandra.model.ExecutingKeyspace.doExecute(ExecutingKeyspace.java:57)     at me.prettyprint.cassandra.model.thrift.ThriftColumnQuery.execute(ThriftColumnQuery.java:34)     在HectorConnect.main(HectorConnect.java:30)

任何人都知道解决这个问题?

2 个答案:

答案 0 :(得分:0)

如果您的apache-casssandra-thrift jar版本不正确,就会发生这种情况。您应该确保Hector提供的版本是CLASSPATH中唯一的版本。

此jar也作为Cassandra下载的一部分打包并放在lib目录中。

答案 1 :(得分:0)

您尚未指定正在使用的hector版本。请尝试以下组合:

     <dependency>
        <groupId>me.prettyprint</groupId>
        <artifactId>hector-core</artifactId>
        <version>1.0-2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
    </dependency>