package client; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.util.List; import org.apache.cassandra.thrift.Cassandra; import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnOrSuperColumn; import org.apache.cassandra.thrift.ColumnParent; import org.apache.cassandra.thrift.ColumnPath; import org.apache.cassandra.thrift.ConsistencyLevel; import org.apache.cassandra.thrift.InvalidRequestException; import org.apache.cassandra.thrift.NotFoundException; import org.apache.cassandra.thrift.SlicePredicate; import org.apache.cassandra.thrift.SliceRange; import org.apache.cassandra.thrift.TimedOutException; import org.apache.cassandra.thrift.UnavailableException; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TFramedTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; public class CClient { public static void main(String[] args) throws TException, InvalidRequestException, UnavailableException, UnsupportedEncodingException, NotFoundException, TimedOutException { //建立连接 TTransport tr = new TFramedTransport(new TSocket("127.0.0.1", 9160)); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); String key_user_id = "1"; // insert data long timestamp = System.currentTimeMillis(); //相当于DB NAME client.set_keyspace("DEMO"); //相当于DB Table ColumnParent parent = new ColumnParent("Users"); //字段名 Column nameColumn = new Column(toByteBuffer("name")); //字段值 nameColumn.setValue(toByteBuffer("scott")); //插入时间 nameColumn.setTimestamp(timestamp); //将数据添加到cassandra client.insert(toByteBuffer(key_user_id), parent, nameColumn, ConsistencyLevel.ONE); //字段名 Column ageColumn = new Column(toByteBuffer("password")); //字段值 ageColumn.setValue(toByteBuffer("tiger")); //插入时间 ageColumn.setTimestamp(timestamp); //将数据添加到cassandra client.insert(toByteBuffer(key_user_id), parent, ageColumn, ConsistencyLevel.ONE); //得到相当于DB Table ColumnPath path = new ColumnPath("Users"); // read single column path.setColumn(toByteBuffer("name")); System.out.println(client.get(toByteBuffer(key_user_id), path,
cassandra新增、更新、删除数据。
最新推荐文章于 2023-10-29 21:39:35 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)