首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Hector连接到cassandra

无法使用Hector连接到cassandra
EN

Stack Overflow用户
提问于 2012-06-28 19:10:00
回答 1查看 2K关注 0票数 1

我无法使用赫克托访问卡桑德拉。以下是代码

代码语言:javascript
复制
 import java.util.Arrays;
 import java.util.List;
 import me.prettyprint.cassandra.service.CassandraHostConfigurator;
 import me.prettyprint.cassandra.service.ThriftCluster;
 import me.prettyprint.cassandra.service.ThriftKsDef;
 import me.prettyprint.hector.api.Cluster;
 import me.prettyprint.hector.api.Keyspace;
 import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
 import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
 import me.prettyprint.hector.api.factory.HFactory;
 import me.prettyprint.hector.api.mutation.Mutator;

 public class Hector {
 public static void main (String[] args){
boolean cfExists = false;
Cluster cluster = HFactory.getOrCreateCluster("mycluster", new                     CassandraHostConfigurator("host:9160"));
Keyspace keyspace = HFactory.createKeyspace("Keyspace1", cluster);
// first check if the key space exists
        KeyspaceDefinition keyspaceDetail = cluster.describeKeyspace("Keyspace1");
        // if not, create one
        if (keyspaceDetail == null) {

            CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("host:9160");
            ThriftCluster cassandraCluster = new ThriftCluster("mycluster", cassandraHostConfigurator);

            ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("Keyspace1", "base");
            cassandraCluster.addKeyspace(new ThriftKsDef("Keyspace1", "org.apache.cassandra.locator.SimpleStrategy", 1,
                    Arrays.asList(cfDef)));

        } else {

            // even if the key space exists, we need to check if the column family exists
            List<ColumnFamilyDefinition> columnFamilyDefinitions = keyspaceDetail.getCfDefs();
            for (ColumnFamilyDefinition def : columnFamilyDefinitions)    {
                String columnFamilyName = def.getName();
                if (columnFamilyName.equals("tcs_im"))
                    cfExists = true;
            }
        }
 }
 } 

遇到以下错误

Log4j:警告找不到记录器(me.prettyprint.cassandra.connection.CassandraHostRetryService).的附加程序log4j :警告请正确初始化log4j系统。log4j:WARN更多信息请参见http://logging.apache.org/log4j/1.2/faq.html#noconfig。线程"main“java.lang.IllegalAccessError中出现异常:试图从me.prettyprint.cassandra.connection.HConnectionManager.(HConnectionManager.java:78) at me.prettyprint.cassandra.service.AbstractCluster.(AbstractCluster.java:69) at me.prettyprint.cassandra.service.AbstractCluster.(AbstractCluster.java:65) at me.prettyprint.cassandra.service.ThriftCluster.(ThriftCluster.java:17) at me的me.prettyprint.cassandra.connection.HConnectionManager类访问类java.lang.IllegalAccessError.prettyprint.hector.api.factory.HFactory.createCluster(HFactory.java:176) at me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster(HFactory.java:155) at com.im.tcs.Hector.main(Hector.java:20)

请帮助解释为什么会发生这种情况。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-30 20:36:15

我们使用CassandraConnection类作为便利类:

代码语言:javascript
复制
import me.prettyprint.cassandra.connection.DynamicLoadBalancingPolicy;
import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import me.prettyprint.cassandra.service.ExhaustedPolicy;
import me.prettyprint.cassandra.service.OperationType;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.HConsistencyLevel;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.factory.HFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * lazy connect
 */
final class CassandraConnection {

    // Constants -----------------------------------------------------

    private static final String HOSTS = "localhost";
    private static final int PORT = "9160";
    private static final String CLUSTER_NAME = "myCluster";
    private static final int TIMEOUT = 500);
    private static final String KEYSPACE = "Keyspace1";
    private static final ConsistencyLevelPolicy CL_POLICY = new ConsistencyLevelPolicy();

    // Attributes ----------------------------------------------------

    private Cluster cluster;
    private volatile Keyspace keyspace;

    // Constructors --------------------------------------------------

    CassandraConnection() {}

    // Methods --------------------------------------------------------

    Cluster getCluster() {
        if (null == cluster) {
            CassandraHostConfigurator config = new CassandraHostConfigurator();
            config.setHosts(HOSTS);
            config.setPort(PORT);
            config.setUseThriftFramedTransport(true);
            config.setUseSocketKeepalive(true);
            config.setAutoDiscoverHosts(false);
            // maxWorkerThreads provides the throttling for us. So hector can be let to grow freely...
            config.setExhaustedPolicy(ExhaustedPolicy.WHEN_EXHAUSTED_GROW);
            config.setMaxActive(1000); // hack since ExhaustedPolicy doesn't work
            // suspend hosts if response is unacceptable for web response
            config.setCassandraThriftSocketTimeout(TIMEOUT);
            config.setUseHostTimeoutTracker(true);
            config.setHostTimeoutCounter(3);
            config.setLoadBalancingPolicy(new DynamicLoadBalancingPolicy());

            cluster = HFactory.createCluster(CLUSTER_NAME, config);

        }
        return cluster;
    }

    Keyspace getKeyspace() {
        if (null == keyspace) {
            keyspace = HFactory.createKeyspace(KEYSPACE, getCluster(), CL_POLICY);
        }
        return keyspace;
    }

    private static class ConsistencyLevelPolicy implements me.prettyprint.hector.api.ConsistencyLevelPolicy {

        @Override
        public HConsistencyLevel get(final OperationType op) {
            return HConsistencyLevel.ONE;
        }

        @Override
        public HConsistencyLevel get(final OperationType op, final String cfName) {
            return get(op);
        }
    }
}

使用示例:

代码语言:javascript
复制
private final CassandraConnection conn = new CassandraConnection();

SliceQuery<String, String, String> sliceQuery = HFactory.createSliceQuery(
                conn.getKeyspace(), StringSerializer.get(), StringSerializer.get(), StringSerializer.get());
sliceQuery.setColumnFamily("myColumnFamily");
sliceQuery.setRange("", "", false, Integer.MAX_VALUE);
sliceQuery.setKey("myRowKey");
ColumnSlice<String, String> columnSlice = sliceQuery.execute().get();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11243342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档