我所依赖的jar包有以下这些:

建立数据库连接:使用TTransport 的 open 方法建立起与 Cassandra 服务端的连接。

数据库操作:使用 Cassandra.Client 创建一个客户端实例操作数据库。

关闭数据库连接:使用 TTransport 的 close 方法断开与 Cassandra 服务端的连接。

  1 package com.lk.jdbc;
  2 
  3 
  4 import java.nio.ByteBuffer;
  5 import java.util.List;
  6 import org.apache.cassandra.thrift.Cassandra;
  7 import org.apache.cassandra.thrift.Column;
  8 import org.apache.cassandra.thrift.ColumnOrSuperColumn;
  9 import org.apache.cassandra.thrift.ColumnParent;
 10 import org.apache.cassandra.thrift.ConsistencyLevel;
 11 import org.apache.cassandra.thrift.InvalidRequestException;
 12 import org.apache.cassandra.thrift.KeyRange;
 13 import org.apache.cassandra.thrift.KeySlice;
 14 import org.apache.cassandra.thrift.SlicePredicate;
 15 import org.apache.cassandra.thrift.SliceRange;
 16 import org.apache.thrift.TException;
 17 import org.apache.thrift.protocol.TBinaryProtocol;
 18 import org.apache.thrift.protocol.TProtocol;
 19 import org.apache.thrift.transport.TFramedTransport;
 20 import org.apache.thrift.transport.TSocket;
 21 import org.apache.thrift.transport.TTransport;
 22 import org.apache.thrift.transport.TTransportException;
 23 
 24 /**
 25  * 使 Java 客户端连接 Cassandra 并进行读写操作
 26  * @author Administrator
 27  *
 28  */
 29 public class OperationDb {
 30     public static String keyspace = "demo";
 31     public static String columnFamily = "users";
 32     public static TTransport transport;
 33     public static Cassandra.Client client;
 34     
 35     /**
 36      * 建立数据库连接
 37      */
 38     static {
 39         try {
 40             transport = new TFramedTransport(new TSocket("localhost", 9160));
 41             TProtocol protocol = new TBinaryProtocol(transport);
 42             client = new Cassandra.Client(protocol);
 43             transport.open();
 44             client.set_keyspace(keyspace);
 45         } catch (TTransportException e) {
 46             e.printStackTrace();
 47         } catch (InvalidRequestException e) {
 48             e.printStackTrace();
 49         } catch (TException e) {
 50             e.printStackTrace();
 51         }
 52     }
 53     
 54     /**
 55      * 关闭数据库连接
 56      */
 57     public static void close() {
 58         if(transport != null && transport.isOpen()) {
 59             transport.close();
 60         }
 61     }
 62     
 63     public ByteBuffer convertByteBuffer(String value) throws Exception {
 64         return ByteBuffer.wrap(value.getBytes("UTF-8"));
 65     }
 66     
 67     /**
 68      * 插入数据
 69      * @throws Exception
 70      */
 71     public void insert() throws Exception {
 72         //插入数据
 73         long timestamp = System.currentTimeMillis();
 74         
 75         Column nameColumn = new Column(convertByteBuffer("name"));
 76         nameColumn.setValue("Damon".getBytes());
 77         nameColumn.setTimestamp(timestamp);
 78         
 79         Column passwordColumn = new Column(convertByteBuffer("age"));
 80         passwordColumn.setValue("22".getBytes());
 81         passwordColumn.setTimestamp(timestamp);
 82         
 83         ColumnParent columnParent = new ColumnParent(columnFamily);
 84         client.insert(convertByteBuffer("u2"), columnParent, nameColumn, ConsistencyLevel.ALL);
 85         client.insert(convertByteBuffer("u2"), columnParent, passwordColumn, ConsistencyLevel.ALL);
 86         close();
 87     }
 88     
 89     /**
 90      * 根据key值获得其对应的columns
 91      * @param key
 92      * @throws Exception
 93      */
 94     public void getColumnByKey(String key) throws Exception {
 95         SlicePredicate predicate = new SlicePredicate();
 96         predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 100));
 97         ColumnParent columnParent = new ColumnParent(columnFamily);
 98         List<ColumnOrSuperColumn> columnsByKey = client.get_slice(ByteBuffer.wrap(key.getBytes()), columnParent, predicate, ConsistencyLevel.ALL);
 99         for(ColumnOrSuperColumn c : columnsByKey) {
100             Column column = c.getColumn();
101             System.out.println(new String(column.getName(), "UTF-8") + ": " + new String(column.getValue(), "UTF-8"));
102         }
103         close();
104     }
105     
106     /**
107      * 获取所有columns
108      * @throws Exception
109      */
110     public void getAllKeys() throws Exception {
111         KeyRange keyRange = new KeyRange(100);
112         keyRange.setStart_key(new byte[0]);
113         keyRange.setEnd_key(new byte[0]);
114         SlicePredicate predicate = new SlicePredicate();
115         predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 100));
116         ColumnParent columnParent = new ColumnParent(columnFamily);
117         List<KeySlice> keySlices  = client.get_range_slices(columnParent, predicate, keyRange, ConsistencyLevel.ONE);
118         System.out.println(keySlices.size());
119         for (KeySlice ks : keySlices) {
120             System.out.print(new String(ks.getKey(), "UTF-8") + " {");
121             List<ColumnOrSuperColumn> columnsByKey = ks.getColumns();
122             for(ColumnOrSuperColumn c : columnsByKey) {
123                 Column column = c.getColumn();
124                 System.out.print(new String(column.getName(), "UTF-8") + " : " + new String(column.getValue(), "UTF-8") + ", ");
125             }
126             System.out.println("}");
127         }
128         close();
129     }
130 
131 }

 

posted on 2013-09-01 12:09  寒岁青松  阅读(414)  评论(0)    收藏  举报