如何使用Java类连接Cassandra

我这样做是为了连接cassandra.But我的代码返回错误..这是我的代码

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:找不到工件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

你有没有研究过这个问题?

挑选司机

您需要一种与cassandra通信的方法,最好的选择是使用高级API。 你在这里有各种各样的选择,但是当我们从高层看时,有两种选择。

  1. 基于CQL的驱动程序 – 对thrift所做的更高级别的抽象。 另外,较新的工具,为cassandra提供支持/文档​​的公司建议新的cassandra应用程序是基于CQL的。
  2. 基于Thrift的驱动器 – 可以访问低级存储,因此更容易出错。

我将使用datastax的CQL驱动程序 。

从datastax的github repo下载并构建驱动程序使用maven并添加以下依赖项:

  com.datastax.cassandra cassandra-driver-core 2.1.3   com.datastax.cassandra cassandra-driver-mapping 2.1.2  

挑选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. 

其他有用的代码

创造一个Keyspace

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

创建ColumnFamily(又名表)

 // 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);