如何使用Java类连接Cassandra

2022-09-01 07:02:48

我这样做是为了连接卡桑德拉。但是我的代码返回了一个错误。这是我的代码

public class CassandraConnection {

public static void main(String[] args) {
    String serverIp = "166.78.10.41";
    String keyspace = "gamma";
    CassandraConnection connection;

    Cluster cluster = Cluster.builder()
            .addContactPoints(serverIp)
            .build();

    Session session = cluster.connect(keyspace);


    String cqlStatement = "SELECT * FROM TestCF";
    for (Row row : session.execute(cqlStatement)) {
        System.out.println(row.toString());
    }

}
}

这是错误日志。

未能在项目 CassandraConnection 上执行目标: 无法解析项目 com.mycompany:CassandraConnection:jar:1.0-SNAPSHOT 的依赖关系: 无法解析以下工件: org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT, org.scalaz:scalaz-effect_2.9.3:jar:7.1.0-SNAPSHOT: 找不到 artifact org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT -> [Help 1]

要查看错误的完整堆栈跟踪,请使用 -e 开关重新运行 Maven。使用 -X 开关重新运行 Maven 以启用完全调试日志记录。

有关错误和可能的解决方案的更多信息,请阅读以下文章:[帮助1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException


答案 1

你对此事做过任何研究吗?

选择司机

你需要一种与cassandra通信的方式,最好的选择是使用高级API。你在这里有广泛的选择,但是当我们从高层次的角度来看待它时,实际上有两个选择。

  1. 基于 CQL 的驱动程序 - 对 thrift 功能的更高级别抽象。同样是较新的工具,为cassandra提供支持/文档的公司建议新的cassandra应用程序基于CQL。
  2. 基于节俭的驱动器 - 可以访问低级存储,因此更容易出错。

我将使用 datastax 的 CQL 驱动程序

从 datastax 的 github 存储库下载并构建驱动程序,或者使用 maven 并添加以下依赖项:

<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-core</artifactId>
  <version>2.1.3</version>
</dependency>

<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-mapping</artifactId>
  <version>2.1.2</version>
</dependency>

选择 maven 是个好主意,因为它会为你管理所有依赖项,但是如果你不使用 maven,至少你会学习如何管理 jar 和阅读堆栈跟踪。


法典

驱动程序的文档显示得很好。如果您无法通读它,文档包含许多示例。

我将在整个示例中使用以下两个变量。

String serverIP = "127.0.0.1";
String keyspace = "system";

Cluster cluster = Cluster.builder()
  .addContactPoints(serverIP)
  .build();

Session session = cluster.connect(keyspace);

// you are now connected to the cluster, congrats!

String cqlStatement = "SELECT * FROM local";
for (Row row : session.execute(cqlStatement)) {
  System.out.println(row.toString());
}

创建/更新/删除

// for all three it works the same way (as a note the 'system' keyspace cant 
// be modified by users so below im using a keyspace name 'exampkeyspace' and
// a table (or columnfamily) called users

String cqlStatementC = "INSERT INTO exampkeyspace.users (username, password) " + 
                      "VALUES ('Serenity', 'fa3dfQefx')";

String cqlStatementU = "UPDATE exampkeyspace.users " +
                      "SET password = 'zzaEcvAf32hla'," +
                      "WHERE username = 'Serenity';";

String cqlStatementD = "DELETE FROM exampkeyspace.users " + 
                      "WHERE username = 'Serenity';";

session.execute(cqlStatementC); // interchangeable, put any of the statements u wish.

其他有用的代码

创建密钥空间

String cqlStatement = "CREATE KEYSPACE exampkeyspace WITH " + 
  "replication = {'class':'SimpleStrategy','replication_factor':1}";

session.execute(cqlStatement);

创建列家族(又名表)

// based on the above keyspace, we would change the cluster and session as follows:
Cluster cluster = Cluster.builder()
  .addContactPoints(serverIP)
  .build();
Session session = cluster.connect("exampkeyspace");

String cqlStatement = "CREATE TABLE users (" + 
                      " username varchar PRIMARY KEY," + 
                      " password varchar " + 
                      ");";

session.execute(cqlStatement);

答案 2

要从java程序连接到cassandra,您需要向程序添加一些基本的依赖项。将以下依赖项添加到程序中。Maven 依赖项列表:

<dependency>
    <groupId>com.datastax.cassandra</groupId>
    <artifactId>cassandra-driver-core</artifactId>
    <version>3.6.0</version>
</dependency>

<dependency>
  <groupId>com.datastax.cassandra</groupId>
  <artifactId>cassandra-driver-mapping</artifactId>
  <version>3.6.0</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.25</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.25</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>21.0</version>
</dependency>

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.20.Final</version>
</dependency>

<dependency>
    <groupId>com.codahale.metrics</groupId>
    <artifactId>metrics-core</artifactId>
    <version>3.0.2</version>
</dependency>

简单的java程序,用于与cassandra连接到键空间并检索表的值

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

public class Test {

    public static void main(String[] args) {
        String serverIp = "127.0.0.1";
        String keyspace = "test";

        Cluster cluster = Cluster.builder()
                .addContactPoints(serverIp)
                .build();

        Session session = cluster.connect(keyspace);


        String cqlStatement = "SELECT * FROM emp";
        for (Row row : session.execute(cqlStatement)) {
            System.out.println(row.toString());
        }

        session.close();
    }
}

Git Hub Repo for the program as maven project: GitURL


推荐