【发布时间】:2012-06-30 20:36:14
【问题描述】:
我无法使用 Hector 访问 Casandra。以下是代码
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:WARN 找不到记录器的附加程序(me.prettyprint.cassandra.connection.CassandraHostRetryService)。 log4j:WARN 请正确初始化 log4j 系统。 log4j:WARN 请参阅http://logging.apache.org/log4j/1.2/faq.html#noconfig 了解更多信息。 线程“主”java.lang.IllegalAccessError 中的异常:试图从类 me.prettyprint.cassandra.connection.HConnectionManager 访问类 me.prettyprint.cassandra.service.JmxMonitor 在 me.prettyprint.cassandra.connection.HConnectionManager.(HConnectionManager.java:78) 在 me.prettyprint.cassandra.service.AbstractCluster.(AbstractCluster.java:69) 在 me.prettyprint.cassandra.service.AbstractCluster.(AbstractCluster.java:65) 在 me.prettyprint.cassandra.service.ThriftCluster.(ThriftCluster.java:17) 在 me.prettyprint.hector.api.factory.HFactory.createCluster(HFactory.java:176) 在 me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster(HFactory.java:155) 在 com.im.tcs.Hector.main(Hector.java:20)
请帮忙看看为什么会这样。
【问题讨论】:
-
Cassandra 和 Hector 有哪些版本? IllegalAccessError 表明您的某些类有不同的、不兼容的版本...