cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Apache Wiki <wikidi...@apache.org>
Subject [Cassandra Wiki] Update of "ClientExamples" by JonathanEllis
Date Mon, 27 Jul 2009 22:40:16 GMT
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:
add 04 Java

------------------------------------------------------------------------------
  See ClientExamples03 for examples fitting the 03 API.
  
  == PHP ==
- Cassandra 0.4 Preview
  
  {{{
  <?php
@@ -89, +88 @@

  ?>
  }}}
  
+ == Java ==
+ {{{
+ import java.util.List;
+ 
+ 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.cassandra.service.*;
+ import org.apache.cassandra.service.Cassandra.Client;
+ 
+ public class CClient
+ {
+ 
+     public static void main(String[] args)
+     {
+         TTransport tr = new TSocket("localhost", 9160);
+         TProtocol proto = new TBinaryProtocol(tr);
+         Client client = new Client(proto);
+         try
+         {
+             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();
+         }
+     }
+ }
+ }}}
+ 

Mime
View raw message