thrift java 客户端_cassandra基于thrift的java客户端测试

本文档介绍了如何进行Cassandra基于Thrift的Java客户端测试,包括所需的库文件,如apache-cassandra-2.0.3.jar、apache-cassandra-thrift-2.0.3.jar等。为了远程访问Cassandra,需要关闭Linux防火墙并修改cassandra.yaml中的RPC地址。在Java代码中,展示了如何连接到Cassandra,插入数据,以及查询数据的操作步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cassandra基于thrift的java客户端测试

需要apache-cassandra-2.0.3.jar

apache-cassandra-thrift-2.0.3.jar

libthrift-0.9.1.jar

log4j。。。

slf4j。。。

安装完cassandra2.0.3后可以从lib目录下获得上述lib

为了远程访问,

1.关闭linux防火墙,

2.修改thrift的地址即(rpc_address),在cassandra.yaml里

#rpc_address: localhost

rpc_address:192.168.53.233

****************************************************

package dbtest;

import java.util.List;

import java.util.Random;

import java.io.UnsupportedEncodingException;

import java.nio.ByteBuffer;

import org.apache.thrift.transport.TFramedTransport;

import org.apache.thrift.transport.TTransport;

import org.apache.thrift.transport.TSocket;

import org.apache.thrift.protocol.TProtocol;

import org.apache.thrift.protocol.TBinaryProtocol;

import org.apache.thrift.TException;

import org.apache.cassandra.thrift.ColumnParent;

import org.apache.cassandra.service.*;

import org.apache.cassandra.thrift.*;

public class Cassandralient

{

public

static void main(String[] args)

throws

TException, InvalidRequestException, UnavailableException,

UnsupportedEncodingException, NotFoundException

{

TTransport tr = new TFramedTransport(new TSocket("localhost",

9160));

TProtocol proto = new TBinaryProtocol(tr);

Cassandra.Client client = new Cassandra.Client(proto);

tr.open();

//keyspace相当于数据库,需要手动创建

String keyspace = "Keyspace1";

client.set_keyspace(keyspace);

//columnfamily

name, 就是数据库的table,需要手动创建

String

columnFamily = "Standard1";

//row

key

String key_user_id = "1";

// insert data

long timestamp = System.currentTimeMillis();

Random r = new Random(timestamp);

Column nameColumn = new

Column(ByteBuffer.wrap("name".getBytes()));

nameColumn.setValue(Long.toHexString(r.nextLong()).getBytes());

nameColumn.setTimestamp(timestamp);

Column ageColumn = new

Column(ByteBuffer.wrap("age".getBytes()));

ageColumn.setValue(Long.toHexString(r.nextLong()).getBytes());

ageColumn.setTimestamp(timestamp);

ColumnParent columnParent = new ColumnParent(columnFamily);

client.insert(ByteBuffer.wrap(key_user_id.getBytes()),

columnParent,nameColumn,ConsistencyLevel.ALL) ;

client.insert(ByteBuffer.wrap(key_user_id.getBytes()),

columnParent,ageColumn,ConsistencyLevel.ALL);

//Gets column by key

SlicePredicate predicate = new SlicePredicate();

predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new

byte[0]), ByteBuffer.wrap(new byte[0]), false, 100));

List columnsByKey =

client.get_slice(ByteBuffer.wrap(key_user_id.getBytes()),

columnParent, predicate, ConsistencyLevel.ALL);

System.out.println(columnsByKey);

//Get all keys

KeyRange keyRange = new KeyRange(100);

keyRange.setStart_key(new byte[0]);

keyRange.setEnd_key(new byte[0]);

List keySlices = client.get_range_slices(columnParent, predicate,

keyRange, ConsistencyLevel.ONE);

System.out.println(keySlices.size());

System.out.println(keySlices);

for (KeySlice ks : keySlices) {

System.out.println(new String(ks.getKey()));

}

tr.close();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值