8

使用 CQL jdbc 驱动程序时的连接字符串应该是什么?

我能否在线找到使用 Java 中的 CQL JDBC 驱动程序的 CQL 的正确/完整示例?

4

2 回答 2

12

您需要来自 apache 站点的 cql jar。

这是我通过 CLI 输入数据后使用的基本测试(使用来自 wiki 的示例):

public class CqlJdbcTestBasic {
public static void main(String[] args) {
    Connection con = null;
    try {
        Class.forName("org.apache.cassandra.cql.jdbc.CassandraDriver");
        con = DriverManager.getConnection("jdbc:cassandra:root/root@localhost:9160/MyKeyspace");

        String query = "SELECT KEY, 'first', last FROM User WHERE age=42";

        Statement stmt = con.createStatement();
        ResultSet result = stmt.executeQuery(query);

        while (result.next()) {
            System.out.println(result.getString("KEY"));
            System.out.println(result.getString("first"));
            System.out.println(result.getString("last"));
        }

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            con = null;
        }
    }
}
}

用户/密码 (root/root) 似乎是任意的,请务必指定 Keyspace (MyKeyspace)

注意,'first'在查询字符串中被引用,因为它是一个 CQL 关键字

于 2011-10-07T19:24:26.747 回答
2

您也可以尝试使用http://code.google.com/a/apache-extras.org/p/cassandra-jdbc/中的 cassandra-jdbc 驱动程序。

或者使用以下 Maven 依赖项:

<dependency>
    <groupId>org.apache-extras.cassandra-jdbc</groupId>
    <artifactId>cassandra-jdbc</artifactId>
    <version>1.2.1</version>
</dependency>
于 2013-03-26T20:37:37.513 回答