日期:2014-05-16 浏览次数:20439 次
在VC环境下,创建虚拟机对象,进而调用Java类,实现预先定义好的连接Cassandra数据库功能,满足某种业务需要
首先定义连接Cassandra的Java代码,包含插入、查询功能
import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.util.List; import org.apache.cassandra.thrift.Cassandra; import org.apache.cassandra.thrift.Cassandra.Client; 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.KeyRange; import org.apache.cassandra.thrift.KeySlice; 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.TFramedTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; public class Cassandratest{ public static String KEYSPACE = "NDOUTILS"; public static String HOST = "172.16.0.204"; public static int PORT = 9160; public static String COLUMN_FAMILY = "logentries"; private static Client client; private static TSocket socket; private static TTransport transport; private static TProtocol protocol; private boolean isOpen = false; public static void main(String args[]){ Cassandratest ccl = new Cassandratest(); ccl.init(); ccl.end(); } Cassandratest(String keyspace, String column_family, String host, int port) { // TODO Auto-generated constructor stub KEYSPACE = keyspace; COLUMN_FAMILY = column_family; HOST = host; PORT = port; } Cassandratest() { } private static ByteBuffer strToBB(String msg) { Charset charset = Charset.forName("UTF-8"); return ByteBuffer.wrap(msg.getBytes(charset)); } private static String byteToStr(byte[] buf) { Charset charset = Charset.forName("UTF-8"); return new String(buf, charset); } private void init() { try { socket = new TSocket(HOST, PORT); transport = new TFramedTransport(socket); protocol = new TBinaryProtocol(transport); client = new Cassandra.Client(protocol); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } if (!isOpen) { try { transport.open(); client.set_keyspace(KEYSPACE); System.out.println("Opening...OK"); } catch (TTransportException e) { // TODO Auto-generated catch block System.out.println("Network is unreachable..."); } catch (InvalidRequestException e) { // TODO Auto-generated catch block System.out.println("请求无效"); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); } isOpen = true; // insertNewColumn(COLUMN_FAMILY, "logentry_id", "value", "0000"); } else { System.out.println("连接已经打开"); } } private void end(){ if (isOpen) { try { transport.flush(); transport.close(); System.out.println("Closing...OK"); } catch (TTransportException e) { // TODO Auto-generated catch block System.out.println("Network is unreachable..."); } } else { System.out.println("连接已经关闭"); } } @SuppressWarnings("unused") private Column insertNewColumn(String ColumnFamily, String Field, String Name, String Value) { long timestamp = System.currentTimeMillis(); Column column = new Column(strToBB(Name)); column.setValue(strToBB(Value)); column.setTimestamp(timestamp); try { client.insert(strToBB(Field), new ColumnParent(ColumnFamily), column, ConsistencyLevel.ONE); return c