【问题标题】:Can't connect to Cassandra - Guava compatibility issue in Cassandra Datastax Driver 3.6无法连接到 Cassandra - Cassandra Datastax 驱动程序 3.6 中的番石榴兼容性问题
【发布时间】:2020-01-27 07:42:10
【问题描述】:

我正在使用 Cassandra 驱动程序 3.6.0 和 Guava 19.0; 我得到以下异常:

[main] INFO com.datastax.driver.core - DataStax Java driver 3.6.0 for Apache Cassandra
[main] INFO com.datastax.driver.core.GuavaCompatibility - Detected Guava >= 19 in the classpath, using modern compatibility layer 
[main] INFO com.datastax.driver.core.ClockFactory - Using native clock to generate timestamps.
Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9042 
(com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9042] Cannot connect), localhost/0:0:0:0:0:0:0:1:9042 
(com.datastax.driver.core.exceptions.TransportException: [localhost/0:0:0:0:0:0:0:1:9042] Cannot connect))
  at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:268)

我从 Cassandra Core 驱动程序 20 降级到 19;当我从客户端代码连接到集群时,我仍然遇到上述问题。

pom.xml sn-p

        <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>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>

Java 驱动程序

package com.cassandra.javaConnect;

import java.time.Instant;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;

public class CassandraV3Tutorial {

    private final static String KEYSPACE_NAME = "example_keyspace";
    private final static String REPLICATION_STRATEGY = "SimpleStrategy";
    private final static int REPLICATION_FACTOR = 1;
    private final static String TABLE_NAME = "example_table";

    public static void main(String[] args) {

        // Setup a cluster to your local instance of Cassandra
        Cluster cluster = Cluster.builder()
                .addContactPoint("localhost")
                .withPort(9042)
                .build();

        // Create a session to communicate with Cassandra
        Session session = cluster.connect();

        // Create a new Keyspace (database) in Cassandra
        String createKeyspace = String.format(
                "CREATE KEYSPACE IF NOT EXISTS %s WITH replication = " +
                        "{'class':'%s','replication_factor':%s};",
                KEYSPACE_NAME,
                REPLICATION_STRATEGY,
                REPLICATION_FACTOR
        );
        session.execute(createKeyspace);

        // Create a new table in our Keyspace
        String createTable = String.format(
                "CREATE TABLE IF NOT EXISTS %s.%s " + "" +
                        "(id uuid, timestamp timestamp, value double, " +
                        "PRIMARY KEY (id, timestamp)) " +
                        "WITH CLUSTERING ORDER BY (timestamp ASC);",
                KEYSPACE_NAME,
                TABLE_NAME
        );
        session.execute(createTable);

        // Create an insert statement to add a new item to our table
        PreparedStatement insertPrepared = session.prepare(String.format(
                "INSERT INTO %s.%s (id, timestamp, value) values (?, ?, ?)",
                KEYSPACE_NAME,
                TABLE_NAME
        ));

        // Some example data to insert
        UUID id = UUID.fromString("1e4d26ed-922a-4bd2-85cb-6357b202eda8");
        Date timestamp = Date.from(Instant.parse("2018-01-01T01:01:01.000Z"));
        double value = 123.45;

        // Bind the data to the insert statement and execute it
        BoundStatement insertBound = insertPrepared.bind(id, timestamp, value);
        session.execute(insertBound);

        // Create a select statement to retrieve the item we just inserted
        PreparedStatement selectPrepared = session.prepare(String.format(
                "SELECT id, timestamp, value FROM %s.%s WHERE id = ?",
                KEYSPACE_NAME,
                TABLE_NAME));

        // Bind the id to the select statement and execute it
        BoundStatement selectBound = selectPrepared.bind(id);
        ResultSet resultSet = session.execute(selectBound);

        // Print the retrieved data
        resultSet.forEach(row -> System.out.println(
                String.format("Id: %s, Timestamp: %s, Value: %s",
                row.getUUID("id"),
                row.getTimestamp("timestamp").toInstant().atZone(ZoneId.of("UTC")),
                row.getDouble("value"))));

        // Close session and disconnect from cluster
        session.close();
        cluster.close();
    }
}

【问题讨论】:

    标签: java cassandra guava cassandra-3.0 datastax-java-driver


    【解决方案1】:

    此错误并不意味着您的 Guava 版本错误。该消息是在 cassandra-driver-core-3.2.0 中引入的,请参阅 JAVA-1328JAVA-1435INFO com.datastax.driver.core.GuavaCompatibility - Detected Guava &gt;= 19 in the classpath, using modern compatibility layer 表示驱动程序检测到 Guava 19+ 并且它正在使用一些兼容层进行操作。您可以使用 Guava 20,甚至任何更新的版本。

    另一方面,您的错误意味着驱动程序无法连接到端口 9042 上的本地 Cassandra 实例。它要么未启动,要么在其他端口上启动,要么防火墙等软件阻止了它。请确保 cassandra 进程正常工作并使用例如 netstat 来查看 Cassandra 是否正在使用端口 9042。

    【讨论】:

    • 感谢您的反馈。我的 cassandra 实例没有启动,因为我的 JAVA_HOME 没有设置为 jdk 1.8; JAVA_HOME 指向 openjdk 12;在 bash_profile 中设置 JAVA_HOME 后,我重新启动并运行了服务器,没有任何异常。
    猜你喜欢
    • 2013-12-23
    • 2017-01-24
    • 2016-02-18
    • 2018-04-16
    • 2015-08-16
    • 2019-08-01
    • 2016-04-26
    • 2016-02-19
    • 2021-02-09
    相关资源
    最近更新 更多