适用Driver执行操作
DDL操作// 创建keyspace,指定对应strategy, replication factor。
session.execute(
"CREATE KEYSPACE IF NOT EXISTS testKeyspace WITH replication "
+ "= {'class':'SimpleStrategy', 'replication_factor':1};");
// 创建table,给table指定对应的primary key 以及cluster key 和regular key
session.execute(
"CREATE TABLE IF NOT EXISTS testKeyspace.testTable ("
+ "id int PRIMARY KEY,"
+ "name text,"
+ "age int,"
+ "address text"
+ ");");
//清空表
session.execute("TRUNCATE TABLE testKeyspace.testTable;");
//删除表
session.execute("DROP TABLE testKeyspace.testTable ");
DML操作// 执行insert 操作
session.execute(
"INSERT INTO testKeyspace.testTable (id, name, age, address) "
+ "VALUES ("
+ "1,"
+ "'testname',"
+ "11,"
+ "'hangzhou');");
// 执行select 操作,这里select * 表示获取所有列,也可以指定需要select 的列名获取对应列数据
ResultSet res = session.execute(
"SELECT * FROM testKeyspace.testTable ;");
// 如果想要获取每一列对应的数据,可以如下操作
for (Row row : results)
{
int id = row.getInt("id");
String name = row.getString("name");
int age = row.getInt("age");
String address = row.getString("address");
}
// 关闭Session
session.close();
// 关闭Cluster
cluster.close();