Cassandra学习笔记1

所用版本 Cassandra 1.0

简单的增删改java测试代码:

import java.nio.ByteBuffer;
import org.apache.cassandra.thrift.*;
import org.apache.cassandra.thrift.TBinaryProtocol;
import org.apache.thrift.protocol.*;
import org.apache.thrift.transport.*;

public class Test {
	public static void main(String[] args) throws Exception {
//		insertOrUpdateData(); //增加或者修改 
		getData();    //查询
//		delete();     //删除
		
	}
	
	public static void insertOrUpdateData() throws Exception{
		TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",9160));
		TProtocol protocol = new TBinaryProtocol(transport);
		Cassandra.Client client = new Cassandra.Client(protocol);
		transport.open();
		 
		client.set_keyspace("ksTest1");
		ColumnParent parent = new ColumnParent("User1");
		
		ByteBuffer rowid = ByteBuffer.wrap("100".getBytes());
		
		Column column = new Column();
		column.setName("description2".getBytes());
		
		column.setValue("some value here222...".getBytes());
		column.setTimestamp(System.currentTimeMillis());
		client.insert(rowid, parent, column, ConsistencyLevel.ONE);
		
		transport.flush();
		transport.close();
		
		
	}
	
	public static void getData() throws Exception{
		TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",9160));
		TProtocol protocol = new TBinaryProtocol(transport);
		Cassandra.Client client = new Cassandra.Client(protocol);
		transport.open();
		 
		client.set_keyspace("ksTest1");
		ColumnParent parent = new ColumnParent("User1");
		
		ByteBuffer rowid = ByteBuffer.wrap("100".getBytes());
		
		Column column = new Column();
		column.setName("description".getBytes());
		
		ColumnPath path = new ColumnPath();
        path.column_family = "User1";
        path.column = ByteBuffer.wrap("description".getBytes());
		
		ColumnOrSuperColumn c1 = client.get(rowid, path, ConsistencyLevel.ONE) ;
		String res = new String(c1.getColumn().getValue(),"UTF-8");
		
		System.out.println(res);
		transport.flush();
		transport.close();
	} 
	
	
	public static void delete() throws Exception{
	       // open connection to Cassandra server
		TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",9160));
        TProtocol protocol = new TBinaryProtocol(transport);
        Cassandra.Client client = new Cassandra.Client(protocol);
        transport.open();

        // specify keyspace
        client.set_keyspace("ksTest1");

        // specify row id
        ByteBuffer rowid = ByteBuffer.wrap("100".getBytes());

        // specify column path
        ColumnPath path = new ColumnPath();
        path.column_family = "User1";
        path.column = ByteBuffer.wrap("description2".getBytes());

        // specify timestamp value of data to be removed
        long timestamp = 1301874610643000L;

        // set consistency level to one
        ConsistencyLevel consistency = ConsistencyLevel.ONE;

        // remove the description column from the specified row
        // in the User column family
        client.remove(rowid, path, timestamp, consistency);

        // release resources
        transport.flush();
        transport.close();
	} 
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值