【问题标题】:Is there a Java interface to Cassandra database that works out of the box?是否有开箱即用的 Cassandra 数据库的 Java 接口?
【发布时间】:2012-04-13 11:27:20
【问题描述】:

我在 Eclipse 中为 Cassandra 数据库设置了 Hector API。我有 关于 SLF4J [一些日志记录实用程序] 的错误。我已经度过了最后 [几乎] 2 小时调试错误。导入后 包,我得到了

线程“主”java.lang.ExceptionInInitializerError 中的异常 org.slf4j.LoggerFactory.bind(LoggerFactory.java:128) 在 org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108) 在 org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:279) 在 org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:252) 在 org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:265) 在 me.prettyprint.cassandra.service.AbstractCluster.(AbstractCluster.java:44) 在 me.prettyprint.cassandra.service.ThriftCluster.(ThriftCluster.java:21) 在 me.prettyprint.hector.api.factory.HFactory.createCluster(HFactory.java:196) 在 me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster(HFactory.java:143) 在 me.prettyprint.hector.api.factory.HFactory.getOrCreateCluster(HFactory.java:132) 在 CassandraInterface.main(CassandraInterface.java:7) 引起: java.lang.UnsupportedOperationException:这段代码应该从来没有 把它放进罐子里 org.slf4j.impl.StaticLoggerBinder.(StaticLoggerBinder.java:63) 在 org.slf4j.impl.StaticLoggerBinder.(StaticLoggerBinder.java:44) ... 11 更多

是否有任何 Cassandra 数据库 API 可以放入我的 Eclipse 项目并开箱即用,无需 安装、配置和调试其他第 3 方软件?

另外:我没有安装 Maven。最好的事情是单身 JAR 文件或 JAR 文件/java 源的文件夹。

编辑:我安装了 Hector API,我的程序编译没有错误,但现在出现运行时错误

Exception in thread "main" java.lang.NoSuchMethodError: org.apache.thrift.meta_data.FieldValueMetaData.<init>(BZ)V
    at org.apache.cassandra.thrift.ColumnParent.<clinit>(ColumnParent.java:128)
    at me.prettyprint.cassandra.service.template.AbstractColumnFamilyTemplate.<init>(AbstractColumnFamilyTemplate.java:63)
    at me.prettyprint.cassandra.service.template.ColumnFamilyTemplate.<init>(ColumnFamilyTemplate.java:39)
    at me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate.<init>(ThriftColumnFamilyTemplate.java:38)
    at CassandraInterface.main(CassandraInterface.java:66)

我需要 .java、.class 或 .jar 文件的任意组合,我可以直接放入我的项目中,而不需要任何类型的 Maven 或 XML 文件/目录配置。只是做它所宣传的简单的东西。

【问题讨论】:

  • 我正在编写一个 Java 程序来与 Cassandra 数据库进行通信
  • 如果不想和maven打交道,hector项目在github网站上提供tar.gz下载。它不是一个单一的罐子,而是赫克托创建或依赖的所有罐子。 github.com/rantav/hector/downloads
  • "java.lang.UnsupportedOperationException: 此代码不应该进入 jar" 喜欢那个错误消息。

标签: java eclipse cassandra


【解决方案1】:

当您使用 Eclipse 时,为什么不使用内置的 maven 功能?这将允许您:

"...只需放入我的 Eclipse 项目并开箱即用即可开始使用,无需安装、配置和调试其他第 3 方软件"。

将以下内容放入您的 pom.xml:

      <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-api</artifactId>
                <version>1.6.4</version>
      </dependency>
      <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>1.6.4</version>
      </dependency>
      <dependency>
                <groupId>me.prettyprint</groupId>
                <artifactId>hector-core</artifactId>
                <version>1.0-4</version>
      </dependency>

然后您的代码应该可以工作了。

强烈建议你关注Why maven? What are the benefits?

最后,如果您不愿意走简单且相当标准的道路,您可以按照 Nick 的建议:https://github.com/rantav/hector/downloads 并下载包含所有 jar 的 tar.gz 并将它们添加到您的类路径中。

【讨论】:

  • slf4j 版本最好保持相同,而不是合并不同的版本
  • tar.gz 工作至今。谢谢!如果您没有注意到,我已接受您的帖子作为答案并点赞。
  • 编辑:我得到一个运行时异常:“java.lang.NoClassDefFoundError: org/apache/thrift/TBase”使用最新版本的压缩 JAR 文件。我重新打开这个问题。我的 Eclipse 构建路径中包含 zip 文件中的所有 JAR。
  • 就像我在回答中所说的那样。如果您想避免丢失 JAR 文件的麻烦,请使用 maven。
【解决方案2】:

Netflix 为 Cassandra 创建了客户端,它似乎比 Hector 更好,并且还得到了大公司的支持。只需将 Maven 依赖项放到您的 pom.xml 中,然后您就可以从他们的 wiki 页面尝试一些示例。

https://github.com/Netflix/astyanax

【讨论】:

    【解决方案3】:

    我想通了。

    如何在没有 MAVEN 的情况下使用 HECTOR:

    从 Github 下载 Hector,地址为 https://github.com/rantav/hector/downloads

    现在您需要 TBase 和一堆其他文件。在http://www.java2s.com/Code/Jar/l/Downloadlibthrift060jar.htm获取jar文件

    解压缩所有下载并将所有 JAR 放在您的构建路径中。

    开始编码。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2010-09-22
    • 2011-10-12
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多