execute Cassandra cqlsh in Java

UPDATE: 2018-9-12 一年了才知道push的第一行代码是用的Cassandra的官方java驱动。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package club.kaii;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
/**
* author Genius Kaii
*
*/
public class CassandraConnecter {
public Cluster cluster;
public Session session;

public void connect(String ip, int port, String username, String pwd) {
cluster = Cluster.builder().addContactPoints(ip).withPort(port).withCredentials(username, pwd).build();
session = cluster.connect();
}

public void createKeyspace(String keyspaceName, String strategy, int factor){
String cql = "CREATE KEYSPACE IF NOT EXISTS" + keyspaceName + " WITH replication = {'class': '" + strategy + "', 'replication_factor': '" + factor + "'};";
session.execute(cql);
}

public void closeConnection(){
session.close();
}
}
深得我心!博主晚餐加鸡腿!