cassandra入门应用一
cassandra应用步骤:
1.在http://cassandra.apache.org/下载cassandra保存相应目录中
2.cassandra进行解压,
a.解压命令:gunzip apache-cassandra-0.6.8-bin.tar.gz。
b.对tar解压:tar -vxf apache-cassandra-0.6.8-bin.tar。
3.进入apache-cassandra-0.6.8目录bin下 运行 Cassandra
..............................
INFO 10:49:48,282 Starting up server gossip
INFO 10:49:48,327 Binding thrift service to localhost/127.0.0.1:9160
INFO 10:49:48,331 Cassandra starting up...
出现这个表示cassandra启动成功。
对cassandra简单讨论
1.cassandra是什么?
是基于k-v的是一种高伸缩性,的第二代数据库,用来存储数据
参考blog:
http://www.iteye.com/topic/784054
http://www.iteye.com/topic/722859
cassandra简单示例
/** * */[color=green]package org.wolf.cassandra;import org.apache.cassandra.thrift.Column;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.cassandra.thrift.Cassandra.Client;import org.apache.thrift.TException;import org.apache.thrift.protocol.TBinaryProtocol;import org.apache.thrift.transport.TSocket;import org.apache.thrift.transport.TTransport;import org.apache.thrift.transport.TTransportException;import com.sun.org.apache.bcel.internal.generic.NEW;/** * cassandra 结构分为 server client * * @author wolf * */public class CassandraDevelop {/** *cassandra 客户端 */private Client cassClient;/** * 连接协议,I/O layer,对 Java input/output streams 一个包装,其实现方式有 Tsocket,http等, */private TTransport transport;private void initCassandra() throws TTransportException {String host = "localhost";int port = 9160;/* 指定服务器的地址和接口,以socket方式建立连接 */this.transport = new TSocket(host, port);/* 指定socket通讯协议为二进制流协议 */TBinaryProtocol binaryProtocol = new TBinaryProtocol(this.transport,false, false);this.cassClient = new Client(binaryProtocol);/* 建立通讯连接 */this.transport.open();}private void close(){this.transport.close();}/** * 插入數字 * * @param tSpace * @param tName * @param rPara * @param cName * @param cValue * @throws TException * @throws TimedOutException * @throws UnavailableException * @throws InvalidRequestException */private void insertData(String tSpace, String tName, String rPara,String cName) throws InvalidRequestException, UnavailableException,TimedOutException, TException {ColumnPath col = new ColumnPath(tName);col.setColumn(cName.getBytes());/** * 执行插入操作,指定keysapce, row, col, 和数据内容, 后面两个参数一个是timestamp, * 另外一个是consistency_level timestamp是用来做数据一致性保证的, * 而consistency_level是用来控制数据分布的策略,前者的理论依据是bigtable, 后者的理论依据是dynamo */this.cassClient.insert(tSpace, rPara, col,"using cassandra".getBytes(), System.currentTimeMillis(),ConsistencyLevel.ONE);}/** * 获取列 * * @param tSpace * @param tName * @param rowParam * @param colName * @return * @throws TException * @throws TimedOutException * @throws UnavailableException * @throws NotFoundException * @throws InvalidRequestException */public Column getColumn(String tSpace, String tName, String rowParam,String colName) throws InvalidRequestException, NotFoundException,UnavailableException, TimedOutException, TException {ColumnPath col = new ColumnPath(tName);col.setColumn(colName.getBytes());/** * 执行查询操作,指定keysapce, row, col, timestamp timestamp是用来做数据一致性保证的, * 而consistency_level是用来控制数据分布的策略,前者的理论依据是bigtable, 后者的理论依据是dynamo */return this.cassClient.get(tSpace, rowParam, col, ConsistencyLevel.ONE).column;}public static void main(String[] args) throws InvalidRequestException, UnavailableException, TimedOutException, TException, NotFoundException {CassandraDevelop cassandraDevelop = new CassandraDevelop();cassandraDevelop.initCassandra();String tSpace="Keyspace1";String row="employee";String tName="Standard2";cassandraDevelop.insertData(tSpace, tName, row, tName);Column column = cassandraDevelop.getColumn(tSpace, tName, row, "Standard2");System.out.println(new String(column.name));}}[/color]