cassandra java jar,如何使用Java类连接Cassandra

I am doing this to connect cassandra.But my code is returning an error.. Here is my code

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

}

}

}

this is the error log ..

Failed to execute goal on project CassandraConnection: Could not resolve dependencies for project com.mycompany:CassandraConnection:jar:1.0-SNAPSHOT: The following artifacts could not be resolved: 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: Could not find artifact org.specs2:scalaz-effect_2.11.0-SNAPSHOT:jar:7.0.1-SNAPSHOT -> [Help 1]

To see the full stack trace of the errors, re-run Maven with the -e switch.

Re-run Maven using the -X switch to enable full debug logging.

For more information about the errors and possible solutions, please read the following articles:

[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

解决方案

Did you do any research on the matter?

Picking a driver

You need a way to communicate with cassandra, best option is to use a high level API. You have a wide range of choices here but when we look at it from a high level prespective there are really two choices.

CQL based drivers - Higher level abstraction of what thrift does. Also the newer tool, companies providing support / documentation for cassandra recommend that new cassandra applications are CQL based.

Thrift based drives - Have access to low level storage, so its easier to get things wrong.

Download and build the driver from datastax's github repo OR use maven and add the following dependencies:

com.datastax.cassandra

cassandra-driver-core

2.1.3

com.datastax.cassandra

cassandra-driver-mapping

2.1.2

Picking maven is a good idea, as it will manage all your dependencies for you, but if you dont use maven, at least you will learn about managing jars and reading through stack-traces.

Code

The driver's documentation is coming along nicely. If you get stuck read through it, the documentation contains lots of examples.

I'll use the following two variables throughout the examples.

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!

Read

String cqlStatement = "SELECT * FROM local";

for (Row row : session.execute(cqlStatement)) {

System.out.println(row.toString());

}

Create/Update/Delete

// 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.

Other useful code

Creating a Keyspace

String cqlStatement = "CREATE KEYSPACE myfirstcassandradb WITH " +

"replication = {'class':'SimpleStrategy','replication_factor':1}";

session.execute(cqlStatement);

Creating a ColumnFamily (aka table)

// 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("myfirstcassandradb");

String cqlStatement = "CREATE TABLE users (" +

" user_name varchar PRIMARY KEY," +

" password varchar " +

");";

session.execute(cqlStatement);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值