使用 CQL jdbc 驱动程序时,连接字符串应该是什么?
我能否在 Java 中在线找到使用 CQL JDBC 驱动程序的 CQL 的正确/完整示例?
最佳答案
您需要来自 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)似乎是任意的,只要确保指定 key 空间(MyKeyspace)
请注意,'first' 在查询字符串中被引用,因为它是 CQL 关键字
关于java - 使用 CQL jdbc 驱动程序时连接字符串应该是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7688571/