You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by Apache Wiki <wi...@apache.org> on 2009/08/19 02:44:26 UTC

[Cassandra Wiki] Update of "ClientExamples" by JonathanEllis

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Cassandra Wiki" for change notification.

The following page has been changed by JonathanEllis:
http://wiki.apache.org/cassandra/ClientExamples

The comment on the change is:
update java example

------------------------------------------------------------------------------
  == Java ==
  {{{
  import java.util.List;
+ import java.io.UnsupportedEncodingException;
  
+ import org.apache.thrift.transport.TTransport;
+ import org.apache.thrift.transport.TSocket;
+ import org.apache.thrift.protocol.TProtocol;
  import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.protocol.TProtocol;
- import org.apache.thrift.transport.TSocket;
- import org.apache.thrift.transport.TTransport;
- import org.apache.thrift.transport.TTransportException;
+ import org.apache.thrift.TException;
- 
  import org.apache.cassandra.service.*;
- import org.apache.cassandra.service.Cassandra.Client;
  
  public class CClient
  {
- 
      public static void main(String[] args)
+     throws TException, InvalidRequestException, UnavailableException, UnsupportedEncodingException, NotFoundException
      {
          TTransport tr = new TSocket("localhost", 9160);
          TProtocol proto = new TBinaryProtocol(tr);
-         Client client = new Client(proto);
+         Cassandra.Client client = new Cassandra.Client(proto);
-         try
+         tr.open();
+ 
+         String key_user_id = "1";
+ 
+         // insert data
+         long timestamp = System.currentTimeMillis();
+         client.insert("Keyspace1",
+                       key_user_id,
+                       new ColumnPath("Standard1", null, "name".getBytes("UTF-8")),
+                       "Chris Goffinet".getBytes("UTF-8"),
+                       timestamp,
+                       ConsistencyLevel.ONE);
+         client.insert("Keyspace1",
+                       key_user_id,
+                       new ColumnPath("Standard1", null, "age".getBytes("UTF-8")),
+                       "24".getBytes("UTF-8"),
+                       timestamp,
+                       ConsistencyLevel.ONE);
+ 
+         // read single column
+         ColumnPath path = new ColumnPath("Standard1", null, "name".getBytes("UTF-8"));
+         System.out.println(client.get("Keyspace1", key_user_id, path, ConsistencyLevel.ONE));
+ 
+         // read entire row
+         SlicePredicate predicate = new SlicePredicate(null, new SliceRange(new byte[0], new byte[0], false, 10));
+         ColumnParent parent = new ColumnParent("Standard1", null);
+         List<ColumnOrSuperColumn> results = client.get_slice("Keyspace1", key_user_id, parent, predicate, ConsistencyLevel.ONE);
+         for (ColumnOrSuperColumn result : results)
          {
+             Column column = result.column;
+             System.out.println(new String(column.name, "UTF-8") + " -> " + new String(column.value, "UTF-8"));
-             tr.open();
-         }
-         catch (TTransportException e)
-         {
-             throw new RuntimeException(e);
          }
  
-         try
-         {
-             String key_user_id = "1";
- 
-             // insert data
-             long timestamp = System.currentTimeMillis();
-             client.insert("Table1",
-                           key_user_id,
-                           new ColumnPath("Standard1", null, "name".getBytes("UTF-8")),
-                           "Chris Goffinet".getBytes("UTF-8"),
-                           timestamp,
-                           0);
-             client.insert("Table1",
-                           key_user_id,
-                           new ColumnPath("Standard1", null, "age".getBytes("UTF-8")),
-                           "24".getBytes("UTF-8"),
-                           timestamp,
-                           0);
- 
-             // read single column
-             System.out.println(client.get_column("Table1", key_user_id, new ColumnPath("Standard1", null, "name".getBytes("UTF-8"))));
- 
-             // read entire row
-             final List<Column> results = client.get_slice("Table1", key_user_id, new ColumnParent("Standard1", null), new byte[0], new byte[0], true, 10);
-             for (Column column : results)
-             {
-                 System.out.println(new String(column.name, "UTF-8") + " -> " + new String(column.value, "UTF-8"));
-             }
-         }
-         catch (Exception e)
-         {
-             throw new RuntimeException(e);
-         }
-         finally
-         {
-             tr.close();
+         tr.close();
-         }
      }
  }
  }}}