版本:0.7.6
客户端:cassandra自带的thrift客户端
标准column的查询、删除、新增:
写道
TTransport tr = new TFramedTransport(new TSocket("localhost",9160));
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);
tr.open();
client.set_keyspace("Keyspace1");
ColumnPath path = new ColumnPath();
long timestamp = System.currentTimeMillis();
// 插入数据
Column column = new Column();
column.setName("age".getBytes());
column.setValue("19".getBytes());
column.setTimestamp(timestamp);
client.insert(ByteBuffer.wrap("user".getBytes()), new ColumnParent("standard1"), column, ConsistencyLevel.ONE);
// 读取数据
path = new ColumnPath();
path.setColumn_family("standard1");
path.setColumn("age".getBytes("UTF-8"));
ColumnOrSuperColumn cc = client.get(ByteBuffer.wrap("user".getBytes()), path, ConsistencyLevel.ONE);
Column c = cc.getColumn();
String v = new String(c.getValue(), "UTF-8");
System.out.println("Value--------" + v);
//删除数据
// path = new ColumnPath();
// path.setColumn_family("standard1");
// path.setColumn("age".getBytes("UTF-8"));
// client.remove(ByteBuffer.wrap("user".getBytes()), path, timestamp, ConsistencyLevel.ONE);
// 关闭数据库连接
tr.close();
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);
tr.open();
client.set_keyspace("Keyspace1");
ColumnPath path = new ColumnPath();
long timestamp = System.currentTimeMillis();
// 插入数据
Column column = new Column();
column.setName("age".getBytes());
column.setValue("19".getBytes());
column.setTimestamp(timestamp);
client.insert(ByteBuffer.wrap("user".getBytes()), new ColumnParent("standard1"), column, ConsistencyLevel.ONE);
// 读取数据
path = new ColumnPath();
path.setColumn_family("standard1");
path.setColumn("age".getBytes("UTF-8"));
ColumnOrSuperColumn cc = client.get(ByteBuffer.wrap("user".getBytes()), path, ConsistencyLevel.ONE);
Column c = cc.getColumn();
String v = new String(c.getValue(), "UTF-8");
System.out.println("Value--------" + v);
//删除数据
// path = new ColumnPath();
// path.setColumn_family("standard1");
// path.setColumn("age".getBytes("UTF-8"));
// client.remove(ByteBuffer.wrap("user".getBytes()), path, timestamp, ConsistencyLevel.ONE);
// 关闭数据库连接
tr.close();
SuperColumn新增和查询:
/** * 插入SuperColumn * * @throws Exception */ public void insertSuperColumn() throws Exception { TTransport tr = new TFramedTransport(new TSocket("localhost",9160)); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); client.set_keyspace("Keyspace1"); ColumnParent cp1 = new ColumnParent(); cp1.setColumn_family("Super2"); cp1.setSuper_column("address".getBytes()); Column city = new Column(); city.setName("province".getBytes()); city.setValue("guangdong".getBytes()); city.setTimestamp(time); client.insert(ByteBuffer.wrap("user2".getBytes()), cp1, city, ConsistencyLevel.ONE); } /** * 读取SuperColumn * * @throws Exception */ public void getSuper() throws Exception { TTransport tr = new TFramedTransport(new TSocket("localhost",9160)); TProtocol proto = new TBinaryProtocol(tr); Cassandra.Client client = new Cassandra.Client(proto); tr.open(); client.set_keyspace("Keyspace1"); ColumnPath path = new ColumnPath("Super2"); path.setSuper_column("address".getBytes()); path.setColumn("province".getBytes()); ColumnOrSuperColumn s = client.get(ByteBuffer.wrap("user5".getBytes()), path, ConsistencyLevel.ONE); System.out.println(new String(s.column.getValue(), "utf8")); }
困惑:如果superColumn下的Column是一个SuperColumn怎么插入数据?没有找到相关的api。