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/06/29 16:45:58 UTC

[Cassandra Wiki] Update of "ClientExamples" by NicolasBonvin

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 NicolasBonvin:
http://wiki.apache.org/cassandra/ClientExamples

The comment on the change is:
Added an example using Java

------------------------------------------------------------------------------
  
  }}}
  
+ == Java ==
+ {{{
+ 
+ 
+ import java.io.UnsupportedEncodingException;
+ import java.util.List;
+ 
+ import org.apache.thrift.TException;
+ 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 com.facebook.infrastructure.service.InvalidRequestException;
+ import com.facebook.infrastructure.service.NotFoundException;
+ import com.facebook.infrastructure.service.UnavailableException;
+ import com.facebook.infrastructure.service.column_t;
+ import com.facebook.infrastructure.service.Cassandra.Client;
+ 
+ public class CClient {
+ 
+   public static void main(String[] args) {
+ 
+     TTransport tr = new TSocket("localhost", 9160);
+     TProtocol proto = new TBinaryProtocol(tr);
+     try {
+       
+       Client client = new Client(proto);
+       tr.open();
+       String key_user_id = "1";
+       long timestamp = System.currentTimeMillis();
+       client.insert("users", key_user_id, "base_attributes:name", 
+                     "Chris Goffinet".getBytes("UTF-8"), timestamp, false);
+       client.insert("users", key_user_id, "base_attributes:age", 
+                     "24".getBytes("UTF-8"), timestamp, false);
+       int start = -1;
+       int end = -1;
+       List<column_t> results =  client.get_slice("users", key_user_id,
+                                                  "base_attributes", start, end);
+       
+       for (column_t col : results) {
+         System.out.println(col.getColumnName() + " -> " + 
+                            new String(col.getValue(),"UTF-8"));
+       }
+       
+     } catch (TTransportException e) {
+       e.printStackTrace();
+     } catch (UnsupportedEncodingException e) {
+       e.printStackTrace();
+     } catch (InvalidRequestException e) {
+       e.printStackTrace();
+     } catch (UnavailableException e) {
+       e.printStackTrace();
+     } catch (TException e) {
+       e.printStackTrace();
+     } catch (NotFoundException e) {
+       e.printStackTrace();
+     } finally {
+       tr.close();
+     }
+   }
+ }
+ }}}
+