1 package ru.easy.imagebase.cassandraclientexample;
   2 
   3 import java.util.List;
   4 import java.io.UnsupportedEncodingException;
   5 
   6 import java.nio.ByteBuffer;
   7 import java.util.Random;
   8 import org.apache.cassandra.thrift.AuthenticationException;
   9 import org.apache.cassandra.thrift.AuthorizationException;
  10 import org.apache.cassandra.thrift.Cassandra;
  11 import org.apache.cassandra.thrift.Column;
  12 import org.apache.cassandra.thrift.ColumnOrSuperColumn;
  13 import org.apache.cassandra.thrift.ColumnParent;
  14 import org.apache.cassandra.thrift.ConsistencyLevel;
  15 import org.apache.cassandra.thrift.InvalidRequestException;
  16 import org.apache.cassandra.thrift.KeyRange;
  17 import org.apache.cassandra.thrift.KeySlice;
  18 import org.apache.cassandra.thrift.NotFoundException;
  19 import org.apache.cassandra.thrift.SlicePredicate;
  20 import org.apache.cassandra.thrift.SliceRange;
  21 import org.apache.cassandra.thrift.TimedOutException;
  22 import org.apache.cassandra.thrift.UnavailableException;
  23 import org.apache.thrift.transport.TTransport;
  24 import org.apache.thrift.transport.TFramedTransport;
  25 import org.apache.thrift.transport.TSocket;
  26 import org.apache.thrift.protocol.TProtocol;
  27 import org.apache.thrift.protocol.TBinaryProtocol;
  28 import org.apache.thrift.TException;
  29 import org.apache.thrift.transport.TTransportException;
  30 
  31 public class App {
  32 
  33         public static void main(String[] args) throws TTransportException, UnsupportedEncodingException, InvalidRequestException, NotFoundException, UnavailableException, TimedOutException, TException, AuthenticationException, AuthorizationException {
  34 
  35                 TTransport tr = new TFramedTransport(new TSocket("localhost", 9160));
  36                 TProtocol proto = new TBinaryProtocol(tr);
  37 
  38                 Cassandra.Client client = new Cassandra.Client(proto);
  39                 tr.open();
  40                 String keyspace = "Keyspace1";
  41                 client.set_keyspace(keyspace);
  42                 //record id
  43                 String key_user_id = "1";
  44                 String columnFamily = "Standard1";
  45                 // insert data
  46                 long timestamp = System.currentTimeMillis();
  47                 Random r = new Random(timestamp);
  48                 Column nameColumn = new Column(ByteBuffer.wrap("name".getBytes()));
  49                 nameColumn.setValue(Long.toHexString(r.nextLong()).getBytes());
  50                 nameColumn.setTimestamp(timestamp);
  51 
  52                 Column ageColumn = new Column(ByteBuffer.wrap("age".getBytes()));
  53                 ageColumn.setValue(Long.toHexString(r.nextLong()).getBytes());
  54                 ageColumn.setTimestamp(timestamp);
  55 
  56                 ColumnParent columnParent = new ColumnParent(columnFamily);
  57                 client.insert(ByteBuffer.wrap(key_user_id.getBytes()), columnParent,nameColumn,ConsistencyLevel.ALL) ;
  58                 client.insert(ByteBuffer.wrap(key_user_id.getBytes()), columnParent,ageColumn,ConsistencyLevel.ALL);
  59 
  60                 //Gets column by key
  61                 SlicePredicate predicate = new SlicePredicate();
  62                 predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 100));
  63                 List<ColumnOrSuperColumn> columnsByKey = client.get_slice(ByteBuffer.wrap(key_user_id.getBytes()), columnParent, predicate, ConsistencyLevel.ALL);
  64                 System.out.println(columnsByKey);
  65 
  66                 
  67                 //Get all keys
  68                 KeyRange keyRange = new KeyRange(100);
  69                 keyRange.setStart_key(new byte[0]);
  70                 keyRange.setEnd_key(new byte[0]);
  71                 List<KeySlice> keySlices = client.get_range_slices(columnParent, predicate, keyRange, ConsistencyLevel.ONE);
  72                 System.out.println(keySlices.size());
  73                 System.out.println(keySlices);
  74                 for (KeySlice ks : keySlices) {
  75                         System.out.println(new String(ks.getKey()));
  76                 }
  77                 tr.close();
  78         }
  79 }

stats

ClientExamples (last edited 2013-11-13 19:25:51 by GehrigKunz)