这段时间看了看分布式数据库Cassandra的一些介绍和使用,前面也做了两篇笔记,包括基本的配置和集群环境下的使用。
这篇将介绍如何通过程序来使用Cassandra。Cassandra提供的较为丰富的API,由于采用Thrift作为API接口,使得其支持绝大数的主流编程语言,如C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml等。官方0.6版的Java Demo在0.66版的cassandra上跑不通过,主要是API版本之间的定义不同。
PS:需要使用Cassandra的API就得自己去生成Java版的Thrift,生成这个就得去装Thrift,安装很简单,到官方下载最新的源代码,解压,configure,make,make install四步就搞定了。然后到cassandra的目录里面,在interface目录里面有cassandra.thrift,然后通过
thrift –gen java cassandra.thrift
就可生成java版的thriftAPI了,同样的换成py就是Python版的API了,支持perl,php,js,cpp,csharp,rb,cocoa等。
经过这么一折腾之后,还报错,生成不了,最后发现原来在Cassandra的Lib包里面已经包含了Java版的thrift API,直接拿来这个jar包来用就行了。在Eclipse里面导入这个包就可以使用Cassandra的API了。
Java API
贴一段忘数据库里面插入10000条数据并查询出结果,这里没有做和传统数据库如Mysql的比较,有兴趣的童鞋可以做做,比较下当插入10000000条数据的时候的差距,网上有人做过的这样比较整体优势是强于Mysql的,体现了分布式数据库的优势。本实验中采用了两台Mac做实验。
import java.io.UnsupportedEncodingException; import java.util.Date; import org.apache.cassandra.thrift.Cassandra; import org.apache.cassandra.thrift.Column; import org.apache.cassandra.thrift.ColumnOrSuperColumn; 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.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 Test { public static void main(String[] args) throws UnsupportedEncodingException, InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException { //Part One TTransport trans = new TSocket("192.168.1.216", 9160); TProtocol proto = new TBinaryProtocol(trans); Cassandra.Client client = new Cassandra.Client(proto); trans.open(); //Part Two String keyspace = "Keyspace1"; String cf = "Standard2"; for (int i=0;i<10000;i++) { String key = "fly3q"+i; long timestamp = new Date().getTime(); ColumnPath path = new ColumnPath(cf); path.setColumn("id".getBytes("UTF-8")); client.insert(keyspace, key, path, String.format("520.%d", i).getBytes("UTF-8"), timestamp, ConsistencyLevel.ONE); path.setColumn("action".getBytes("UTF-8")); client.insert(keyspace, key, path, String.format("Hello, Cassandra!.%d", i).getBytes("UTF-8"), timestamp, ConsistencyLevel.ONE); //Part Three path.setColumn("id".getBytes("UTF-8")); ColumnOrSuperColumn cc = client.get(keyspace, key, path, ConsistencyLevel.ONE); Column c = cc.getColumn(); String value = new String(c.value, "UTF-8"); System.out.println(value); path.setColumn("action".getBytes("UTF-8")); ColumnOrSuperColumn cc2 = client.get(keyspace, key, path, ConsistencyLevel.ONE); Column c2 = cc2.getColumn(); String value2 = new String(c2.value, "UTF-8"); System.out.println(value2); } //Part four trans.close(); } }
未完待续
后续将了解下cassandra的设计结构等。


拜访这里,呵呵,留名纪念
欢迎常来坐坐
无聊的时候就来看看,坐坐沙发…
呵呵,来做吧
走过 你生命里的春暖花开
面朝大海春暖花开