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

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();
       }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值