欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

一个用来测试 Cassandra 的 Java 客户端,,package net.

来源: javaer 分享于  点击 46059 次 点评:185

一个用来测试 Cassandra 的 Java 客户端,,package net.


package net.oschina.test.cassandra;import java.util.Date;import java.util.List;import org.apache.cassandra.thrift.*;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.protocol.TProtocol;import org.apache.thrift.transport.TSocket;import org.apache.thrift.transport.TTransport;/** * 用来测试 Cassandra 的客户端 * @author Winter Lau */public class ClientTester {    public static final String UTF8 = "UTF8";    public static void main(String[] args) throws Exception {        long ct = System.currentTimeMillis();        TTransport tr = new TSocket("localhost", 9160);        TProtocol proto = new TBinaryProtocol(tr);        Cassandra.Client client = new Cassandra.Client(proto);        tr.open();        System.out.println("connect: " + (System.currentTimeMillis() - ct));        ct = System.currentTimeMillis();        String keyspace = "Keyspace1";        String columnFamily = "Standard1";        String keyUserID = "1";        // insert data        long timestamp = System.currentTimeMillis();        ColumnPath colPathName = new ColumnPath(columnFamily);        colPathName.setColumn("fullName".getBytes(UTF8));        client.insert(keyspace, keyUserID, colPathName, "Chris Goffinet"                .getBytes(UTF8), timestamp, ConsistencyLevel.ONE);        ColumnPath colPathAge = new ColumnPath(columnFamily);        colPathAge.setColumn("age".getBytes(UTF8));        client.insert(keyspace, keyUserID, colPathAge, "24".getBytes(UTF8),                timestamp, ConsistencyLevel.ONE);        // read single column        System.out.println("single column:");        Column col = client.get("Keyspace1", 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));        // read entire 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:");        ColumnParent parent = new ColumnParent(columnFamily);        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));        }        System.out.println("get data time: " + (System.currentTimeMillis() - ct));        tr.close();    }}//该片段来自于http://byrx.net
相关栏目:

用户点评