您的位置:首页 > 编程语言 > Java开发

cassandra学习笔记4--Cassandra Java客户端2

2010-07-03 00:25 197 查看
0.6.1 Thrift Java API

import java.io.UnsupportedEncodingException;
import java.util.Date;
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.TSocket;
import org.apache.thrift.transport.TTransport;

public class Main {
//定义编码
public static final String UTF8 = "UTF8";
public static void main(String[] args) throws UnsupportedEncodingException,InvalidRequestException, UnavailableException, TimedOutException,TException, NotFoundException {
//首先指定cassandra server的地址
TTransport tr = new TSocket("localhost", 9160);
//指定通信协议为二进制流协议TBinaryProtocol
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);
//建立通信连接
tr.open();
//这里的keyspace和columnFamily要在cassandra.conf的配置文件里面配置好,否则程序会找不到的
//设置keyspace名字为Keyspace1
String keyspace = "Keyspace1";
//设置columnFamily名字为Standard1
String columnFamily = "Standard1";
//设置key名为1
String keyUserID = "1";

//insert data
long timestamp = System.currentTimeMillis();
//设置name列
ColumnPath colPathName = new ColumnPath(columnFamily);
colPathName.setColumn("fullName".getBytes(UTF8));
client.insert(keyspace, keyUserID, colPathName, "Chris Goffinet".getBytes(UTF8), timestamp, ConsistencyLevel.ONE);
//设置age列
ColumnPath colPathAge = new ColumnPath(columnFamily);
colPathAge.setColumn("age".getBytes(UTF8));
client.insert(keyspace, keyUserID, colPathAge, "24".getBytes(UTF8),timestamp, ConsistencyLevel.ONE);
//设置
ColumnPath colPathCompany = new ColumnPath(columnFamily);
colPathCompany.setColumn("Company".getBytes(UTF8));
client.insert(keyspace, keyUserID, colPathCompany, "人人".getBytes(UTF8), timestamp, ConsistencyLevel.ONE);

//读取单个column(获取名字那个column)
System.out.println("single column:");
Column col = client.get(keyspace, keyUserID, colPathName,ConsistencyLevel.ONE).getColumn();
//如初
System.out.println("column name: " + new String(col.name, UTF8));
System.out.println("column name: " + new String(col.value, UTF8));
System.out.println("column timestamp: " + new Date(col.timestamp));
// 读取整个 row
SlicePredicate predicate = new SlicePredicate();
SliceRange sliceRange = new SliceRange();
sliceRange.setStart(new byte[0]);
sliceRange.setFinish(new byte[0]);
predicate.setSlice_range(sliceRange);

System.out.println("/nrow:");
//创建columnFamily名字为"Standard1"的ColumnParent
ColumnParent parent = new ColumnParent(columnFamily);
//获取ColumnOrSuperColumn结合
List<ColumnOrSuperColumn> results = client.get_slice(keyspace,keyUserID, parent, predicate, ConsistencyLevel.ONE);
for (ColumnOrSuperColumn result : results) {
Column column = result.column;
System.out.println(new String(column.name, UTF8) + " -> " + new String(column.value, UTF8));
}
//关闭通信连接
tr.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: