JAVA操做cassandra數據庫

若是是maven項目,在pom.xml里加入依賴。不是的話下載相應的jar包放到lib目錄下。這裏驅動包的版本要和你cassandra的大版本一致。我這裏cassandra的版本是最新的3.9,驅動是3.0session

1 <dependency>
2     <groupId>com.datastax.cassandra</groupId>
3     <artifactId>cassandra-driver-core</artifactId>
4     <version>3.0.0</version>
5 </dependency>

新建一個類CassandraTest。maven

鏈接cassandraui

 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鏈接池的配置,將上面的稍做修改spa

 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的時候完成)code

 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操做orm

 

 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 }

查詢顯示:xml

相關文章
相關標籤/搜索