Java implements additions, deletions, and changes to cassandra

Cassandra uses cql language as the operation language. After 2.0, Cassandra is more and more like the operation of SQL database in operation. If you want to switch from traditional relational database to Cassandra, the cost of getting started is getting lower and lower. Using the official java driver to operate cassandra is very simple.

maven introduces driver package

 

<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>2.0.1</version>
</dependency>

 1. Create a unique session for the application.

 

 

Cluster cluster = Cluster.builder()
                         .addContactPoint("192.168.22.161")
                         .build();

 This builds a cluster object, "192.168.22.161" is the seed node of cassandra.

 

 

Session session = cluster.connect();

 You can also get a session for a specific keyspace

 

 

Session session = cluster.connect("mykeyspace");

 Sessions are thread-safe, so in an application, you can have only one session instance. Officially, it is recommended to use one keyspace for one session.

 

2. The session can directly support the execution of cql statements.

 

String cql = "select * from mykeyspace.tablename;";
session.execute(cql);

 

 You can do anything in this way, just remember to put a semicolon after the cql statement.

 

3. If you don't want to tediously spell strings, you can use com.datastax.driver.core.Querybuilder.

insert a record String cql = "insert into mykeyspace.tablename(a,b) values(1,2);"
You can write:

 

session.execute(
QueryBuilder.insertInto("mykeyspace", "tablename")
            .values(new String[]{"a","b"}, new Object[]{1,2}));

 delete 记录String cql = “delete from mykeyspace.tablename where a=1”;

 

You can write this:

 

session.execute(QueryBuilder.delete()
	   .from("mykeyspace", "tablename")
	   .where(QueryBuilder.eq("a", 1)))

 update 记录String cql = “update mykeyspace.tablename set b=2 where a=1”

 

 

session.execute(QueryBuilder.update("mykeyspace", "tablename")
       .with(QueryBuilder.set("b", 2))
       .where(QueryBuilder.eq("a", 1)));

 select 记录String cql = “select a, b from mykeyspace.tablename where a>1 and b<0”

ResultSet result = session.execute(QueryBuilder.select("a","b")
		.from("mykeyspace", "tablename")
		.where(QueryBuilder.gt("a", 1))
		.and(QueryBuilder.lt("a", 1)));
Iterator<Row> iterator = result.iterator();
while(iterator.hasNext())
{
	Row row = iterator.next();
	row.getInt("a");
	row.getInt("b");
}

 4. You can also use precompiled placeholders like jdbc.

BoundStatement bindStatement =
session.prepare(
"select * from mykeyspace.tablename where a=? and b=?")
.bind("1","2");
session.execute(bindStatement);

 or

PreparedStatement prepareStatement =
session.prepare(
"select * from mykeyspace.tablename where a=? and b=?");
BoundStatement bindStatement =
     new BoundStatement(prepareStatement).bind("1","2");
session.execute(bindStatement);

 或者

Insert insert = 
QueryBuilder.insertInto("mykeyspace", "tablename")
.values(new String[]{"a","b"}, 
new Object[]{QueryBuilder.bindMarker(),QueryBuilder.bindMarker()});
BoundStatement bindStatement = 
    new BoundStatement(session.prepare(insert)).bind("1","2");
session.execute(bindStatement);

 5、批量batch的方式也有的。

BatchStatement batchStatement = new BatchStatement();
batchStatement.add(insert);
batchStatement.add(bindStatement);
session.execute(batchStatement);

 注:批量操作只支持INSERT,UPDATE,DELETE

cassandra新版的java驱动,是灵活多变的,文章里只是简单的示例了下,你可以通过驱动的源码,或者是官方的api文档,灵活的使用。

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326943023&siteId=291194637