(一)依赖:cassandra.jar
libthrift.jar
(二)连接:
//该方法将返回一个Cassandra.Client实例,该实例包含和server端指定节点会话的API
public Cassandra.Client getClient()
{
//192.168.0.169为想连接到的某个节点的ip,9160为端口
TSocket socket = new TSocket("192.168.0.169", 9160);
TTransport transport_ = socket;
TBinaryProtocol binaryProtocol = new TBinaryProtocol(transport_,false, false);
Cassandra.Client cassandraClient = newCassandra.Client(binaryProtocol);
try
{
transport_.open();
}
catch( e)
{
// Should move this to Log4J as well probably…
.err.println("Exception " + e.getMessage());
e.printStackTrace();
}
return cassandraClient;
}
(三)API
1)该方法向key的指定path插入一个数据,另外想要对key某指定path的数据进行修改也使用这个方法
代码中的cli是一个Cassandra.Client实例
public void test_insert()
{
try {
tableName = "Table1";
key = "testkey";
columnFamily = "Standard1";
columnName = "testColumn";
value = "testValue";
//对数据进行定位
path = columnFamily+":"+columnName;
//向cassandra中插入一条数据
cli.insert(tableName,key,path,value.getBytes(),.currentTimeMillis(),true);
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} catch (UnavailableException e) {
e.printStackTrace();
}
}
2)该方法试用Cql语句的方式发送请求,有一个问题就是当请求的参数或不正确的时候,方法不会报错,只是返回为空;
但是在语法出现错误的时候还是会有InvalidRequestException抛出,提示Unresolved compilation problems
代码中的cli是一个Cassandra.Client实例
public void test_excuteQuery()
{
try {
cli.executeQuery("set Table1.Standard1['testKey2']['testColumn']=’testValue2′");
CqlResult_t crt = cli.executeQuery("get Table1.Standard1['testKey2']");
List
//遍历显示所有column_t中的内容
if(rs != null){
for(int i = 0 ; i < rs.size() ; i++){
Map map = rs.get(i);
for( key : map.keySet()){
.out.println(key+" : "+map.get(key));
}
}
}else{
.out.println("result set is null");
}
} catch (TException e) {
e.printStackTrace();
}
}
3)根据path信息获取一个column
path是一个定位信息,standard column family是两层 super column family是三层
superColumnFamily:superColumn:standardColumn。
public void test_get_column()
{
tableName = "Table1";
key = "testKey2";
columnFamily = "Standard1";
column = "testColumn";
path = columnFamily+":"+column;
try {
column_t cmt = cli.get_column(tableName, key, path);
//以下两个是等价的
.out.println(cmt.getColumnName());
.out.println(cmt.getFieldValue(1));
//以下两个是等价的
.out.println(new (cmt.getValue(),"UTF-8"));
.out.println(new ((byte[])cmt.getFieldValue(2),"UTF-8"));
//以下两个是等价的
.out.println(cmt.getTimestamp());
.out.println(cmt.getFieldValue(3));
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} catch ( e) {
e.printStackTrace();
}
}
4)
//术语解释:每个key都对应一个Row的信息这个Row的信息又按照columnFamliy 分成了很多slice
//给定table,key和columnFamily的信息,
//此方法将返回,该key在当前columnFamily的slice中有都少个column元素
//该例子中key:”testKey2″在columnFamily:”Standard1″中的slice有几个column元素
public void test_get_column_count()
{
tableName = "Table1";
keyName = "testKey2";
columnFamily = "Standard1";
try {
.out.println(cli.get_column_count(tableName, keyName, columnFamily));
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
}
5) //暂不确定
//根据给定的时间点,取出以后的对应给定key的一组columns
//但是好像在时间上有延迟,这个不确定
public void test_get_columns_since()
{
tableName = "Table1";
keyName = "testKey2";
columnFamily = "Standard1";
sinceTime = .currentTimeMillis()-5000*1000;//取当前时间之前120秒以内的
try {
List cmts = cli.get_columns_since(tableName , keyName , columnFamily , sinceTime);
.out.println("cmts size : "+cmts.size());
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} catch ( e) {
e.printStackTrace();
}
}
阅读(831) | 评论(0) | 转发(0) |