JAVA with Cassandra
maven项目,在pom.xml里加入依赖。不是的话下载相应的jar包放到lib目录下。
这里驱动包的版本要和你cassandra的大版本一致。
<dependency>
<groupId>com.datastax.cassandra</groupId> <artifactId>cassandra-driver-core</artifactId> <version>3.0.0</version>
</dependency>
新建一个类CassandraTest。
连接cassandra
1 public Cluster cluster;
2
3 public Session session;
4
5 public void connect()
6 {
7 // addContactPoints:cassandra节点ip withPort:cassandra节点端口 默认9042
8 // withCredentials:cassandra用户名密码 如果cassandra.yaml里authenticator:AllowAllAuthenticator 可以不用配置
9 cluster = Cluster.builder().addContactPoints("192.168.3.89").withPort(9042)
10 .withCredentials("cassandra", "cassandra").build();
11 session = cluster.connect();
12 }
驱动里自带了cassandra连接池的配置,将上面的稍作修改
1 public Cluster cluster;
2
3 public Session session;
4
5 public void connect()
6 {
7 PoolingOptions poolingOptions = new PoolingOptions();
8 // 每个连接的最大请求数 2.0的驱动好像没有这个方法
9 poolingOptions.setMaxRequestsPerConnection(HostDistance.LOCAL, 32);
10 // 表示和集群里的机器至少有2个连接 最多有4个连接
11 poolingOptions.setCoreConnectionsPerHost(HostDistance.LOCAL, 2).setMaxConnectionsPerHost(HostDistance.LOCAL, 4)
12 .setCoreConnectionsPerHost(HostDistance.REMOTE, 2).setMaxConnectionsPerHost(HostDistance.REMOTE, 4);
13
14 // addContactPoints:cassandra节点ip withPort:cassandra节点端口 默认9042
15 // withCredentials:cassandra用户名密码 如果cassandra.yaml里authenticator:AllowAllAuthenticator 可以不用配置
16 cluster = Cluster.builder().addContactPoints("192.168.3.89").withPort(9042)
17 .withCredentials("cassandra", "cassandra").withPoolingOptions(poolingOptions).build();
18 // 建立连接
19 // session = cluster.connect("test");连接已存在的键空间
20 session = cluster.connect();
21
22 }
创建键空间和表(这2个最好在搭建cassandra的时候完成)
1 /**
2 * 创建键空间
3 */
4 public void createKeyspace()
5 {
6 // 单数据中心 复制策略 :1
7 String cql = "CREATE KEYSPACE if not exists mydb WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}";
8 session.execute(cql);
9 }
10
11 /**
12 * 创建表
13 */
14 public void createTable()
15 {
16 // a,b为复合主键 a:分区键,b:集群键
17 String cql = "CREATE TABLE if not exists mydb.test (a text,b int,c text,d int,PRIMARY KEY (a, b))";
18 session.execute(cql);
19 }
对test表的CURD操作
1 /**
2 * 插入
3 */
4 public void insert()
5 {
6 String cql = "INSERT INTO mydb.test (a , b , c , d ) VALUES ( 'a2',4,'c2',6);";
7 session.execute(cql);
8 }
9
10 /**
11 * 修改
12 */
13 public void update()
14 {
15 // a,b是复合主键 所以条件都要带上,少一个都会报错,而且update不能修改主键的值,这应该和cassandra的存储方式有关
16 String cql = "UPDATE mydb.test SET d = 1234 WHERE a='aa' and b=2;";
17 // 也可以这样 cassandra插入的数据如果主键已经存在,其实就是更新操作
18 String cql2 = "INSERT INTO mydb.test (a,b,d) VALUES ( 'aa',2,1234);";
19 // cql 和 cql2 的执行效果其实是一样的
20 session.execute(cql);
21 }
22
23 /**
24 * 删除
25 */
26 public void delete()
27 {
28 // 删除一条记录里的单个字段 只能删除非主键,且要带上主键条件
29 String cql = "DELETE d FROM mydb.test WHERE a='aa' AND b=2;";
30 // 删除一张表里的一条或多条记录 条件里必须带上分区键
31 String cql2 = "DELETE FROM mydb.test WHERE a='aa';";
32 session.execute(cql);
33 session.execute(cql2);
34 }
35
36 /**
37 * 查询
38 */
39 public void query()
40 {
41 String cql = "SELECT * FROM mydb.test;";
42 String cql2 = "SELECT a,b,c,d FROM mydb.test;";
43
44 ResultSet resultSet = session.execute(cql);
45 System.out.print("这里是字段名:");
46 for (Definition definition : resultSet.getColumnDefinitions())
47 {
48 System.out.print(definition.getName() + " ");
49 }
50 System.out.println();
51 System.out.println(String.format("%s\t%s\t%s\t%s\t\n%s", "a", "b", "c", "d",
52 "--------------------------------------------------------------------------"));
53 for (Row row : resultSet)
54 {
55 System.out.println(String.format("%s\t%d\t%s\t%d\t", row.getString("a"), row.getInt("b"),
56 row.getString("c"), row.getInt("d")));
57 }
58 }
www.52newblog.com,本博客专注于励志、人生、名人、故事、时事热点等富有正能量文章的分享。是一款简洁美观、自适应的博客系统。


浙公网安备 33010602011771号