Differences between revisions 5 and 6
Revision 5 as of 2013-11-13 19:25:51
Size: 3558
Editor: GehrigKunz
Comment: statcounter
Revision 6 as of 2014-05-01 23:19:43
Size: 0
Editor: TylerHobbs
Comment: Nobody should be writing raw Thrift clients at this point
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
{{{#!java
package ru.easy.imagebase.cassandraclientexample;

import java.util.List;
import java.io.UnsupportedEncodingException;

import java.nio.ByteBuffer;
import java.util.Random;
import org.apache.cassandra.thrift.AuthenticationException;
import org.apache.cassandra.thrift.AuthorizationException;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.ColumnParent;
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.transport.TTransport;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;

public class App {

 public static void main(String[] args) throws TTransportException, UnsupportedEncodingException, InvalidRequestException, NotFoundException, UnavailableException, TimedOutException, TException, AuthenticationException, AuthorizationException {

  TTransport tr = new TFramedTransport(new TSocket("localhost", 9160));
  TProtocol proto = new TBinaryProtocol(tr);

  Cassandra.Client client = new Cassandra.Client(proto);
  tr.open();
  String keyspace = "Keyspace1";
  client.set_keyspace(keyspace);
  //record id
  String key_user_id = "1";
  String columnFamily = "Standard1";
  // insert data
  long timestamp = System.currentTimeMillis();
  Random r = new Random(timestamp);
  Column nameColumn = new Column(ByteBuffer.wrap("name".getBytes()));
  nameColumn.setValue(Long.toHexString(r.nextLong()).getBytes());
  nameColumn.setTimestamp(timestamp);

  Column ageColumn = new Column(ByteBuffer.wrap("age".getBytes()));
  ageColumn.setValue(Long.toHexString(r.nextLong()).getBytes());
  ageColumn.setTimestamp(timestamp);

  ColumnParent columnParent = new ColumnParent(columnFamily);
  client.insert(ByteBuffer.wrap(key_user_id.getBytes()), columnParent,nameColumn,ConsistencyLevel.ALL) ;
  client.insert(ByteBuffer.wrap(key_user_id.getBytes()), columnParent,ageColumn,ConsistencyLevel.ALL);

  //Gets column by key
  SlicePredicate predicate = new SlicePredicate();
  predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]), ByteBuffer.wrap(new byte[0]), false, 100));
  List<ColumnOrSuperColumn> columnsByKey = client.get_slice(ByteBuffer.wrap(key_user_id.getBytes()), columnParent, predicate, ConsistencyLevel.ALL);
  System.out.println(columnsByKey);

  
  //Get all keys
  KeyRange keyRange = new KeyRange(100);
  keyRange.setStart_key(new byte[0]);
  keyRange.setEnd_key(new byte[0]);
  List<KeySlice> keySlices = client.get_range_slices(columnParent, predicate, keyRange, ConsistencyLevel.ONE);
  System.out.println(keySlices.size());
  System.out.println(keySlices);
  for (KeySlice ks : keySlices) {
   System.out.println(new String(ks.getKey()));
  }
  tr.close();
 }
}
}}}

{{https://c.statcounter.com/9397521/0/fe557aad/1/|stats}}