简体   繁体   English

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

[英]Error while trying to connect to cassandra through hector API

I have written java code for reading records from cassandra. 我已经编写了用于从cassandra中读取记录的Java代码。 But it is giving run-time error.. Here is java code:- 但是它给运行时错误。这是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();
    }
}
}

and error it throws is as folows:- 它引发的错误如下:-

log4j:WARN No appenders could be found for logger (me.prettyprint.cassandra.service.CassandraClientPoolFactory). log4j:WARN找不到记录程序的附加程序(me.prettyprint.cassandra.service.CassandraClientPoolFactory)。 log4j:WARN Please initialize the log4j system properly. log4j:WARN请正确初始化log4j系统。 Exception in thread "main" java.lang.NoSuchMethodError: org.apache.cassandra.thrift.Cassandra$Client.describe_keyspaces()Ljava/util/Set; 线程“主”中的异常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) at HectorConnect.main(HectorConnect.java:30) 在me.prettyprint.cassandra.service.CassandraClientImpl.getKeyspaces(CassandraClientImpl.java:141)在me.prettyprint.cassandra.service.CassandraClientImpl.getKeyspace(CassandraClientImpl.java:114)在me.prettyprint.cassandra.service.CassandraClientImpl.getKeyspace (CassandraClientImpl.java:104)在我.prettyprint.cassandra.model.ExecutingKeyspace.doExecute(ExecutingKeyspace.java:57)在我.prettyprint.cassandra.model.thrift.ThriftColumnQuery.execute(ThriftColumnQuery.java:34)在HectorConnect。主要(HectorConnect.java:30)

Anyone knows solution to this?? 有人知道解决方案吗?

This happens when you have an incorrect version of the apache-casssandra-thrift jar. 当您的apache-casssandra-thrift jar版本不正确时,就会发生这种情况。 You should ensure the version that Hector provides is the only one in your CLASSPATH. 您应该确保Hector提供的版本是CLASSPATH中唯一的版本。

This jar is also packaged as part of the Cassandra download and placed in the lib directory. 此jar也打包为Cassandra下载的一部分,并放置在lib目录中。

You have not specified the hector version you are using.Try Following combination: 您尚未指定使用的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM