简体   繁体   中英

Error while trying to connect to cassandra through hector API

I have written java code for reading records from cassandra. But it is giving run-time error.. Here is java code:-

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 Please initialize the log4j system properly. Exception in thread "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) at HectorConnect.main(HectorConnect.java:30)

Anyone knows solution to this??

This happens when you have an incorrect version of the apache-casssandra-thrift jar. You should ensure the version that Hector provides is the only one in your CLASSPATH.

This jar is also packaged as part of the Cassandra download and placed in the lib directory.

You have not specified the hector version you are using.Try Following combination:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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