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 2011/06/21 23:07:29 UTC

[Cassandra Wiki] Update of "ThriftExamples" by ChrisLarsen

Dear Wiki user,

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

The "ThriftExamples" page has been changed by ChrisLarsen:
http://wiki.apache.org/cassandra/ThriftExamples?action=diff&rev1=87&rev2=88

Comment:
Added a CPP example for Thrift

  ## page was copied from ClientExamples
- 
  '''If you are new to Cassandra and just want to get started, take a look at the available clients [[ClientOptions06|ClientOptions (v0.6)]] instead.'''
  
  <<TableOfContents()>>
  
- 
- 
- This page shows examples of using the low-level [[http://incubator.apache.org/thrift/|Thrift]] interface, primarily intended for client library developers.  
+ This page shows examples of using the low-level [[http://incubator.apache.org/thrift/|Thrift]] interface, primarily intended for client library developers.
- 
- 
  
  To generate the bindings for a particular language, first find out if Thrift supports that language.  If it does, you can run "thrift --gen XYZ interface/cassandra.thrift" for whatever XYZ you fancy.
  
@@ -556, +551 @@

      }
  }
  }}}
+ == C++ ==
+ This is for Cassandra version 0.7 or later
+ 
+ {{{#!c
+ #include "Thrift.h"
+ #include "transport/TSocket.h"
+ #include "transport/TTransport.h"
+ #include "transport/TBufferTransports.h"
+ #include "protocol/TProtocol.h"
+ #include "protocol/TBinaryProtocol.h"
+ #include "Cassandra.h"
+ 
+ using namespace apache::thrift;
+ using namespace apache::thrift::transport;
+ using namespace apache::thrift::protocol;
+ using namespace org::apache::cassandra;
+ 
+ int main(int argc, char *argv[]){
+   try{
+     boost::shared_ptr<TTransport> socket = boost::shared_ptr<TSocket>(new TSocket("127.0.0.1", 9160));
+     boost::shared_ptr<TTransport> tr = boost::shared_ptr<TFramedTransport>(new TFramedTransport (socket));
+     boost::shared_ptr<TProtocol> p = boost::shared_ptr<TBinaryProtocol>(new TBinaryProtocol(tr));
+     CassandraClient cass(p);
+     tr->open();
+ 
+     cass.set_keyspace("Keyspace1");
+ 
+     string key = "1";
+     ColumnParent cparent;
+     cparent.column_family = "Standard1";
+     Column c;
+     c.name = "name";
+     c.value = "John Smith";
+ 
+     // have to go through all of this just to get the timestamp in ms
+     struct timeval td;
+     gettimeofday(&td, NULL);
+     int64_t ms = td.tv_sec;
+     ms = ms * 1000;
+     int64_t usec = td.tv_usec;
+     usec = usec / 1000;
+     ms += usec;
+     c.timestamp = ms;
+ 
+     // insert the "name" column
+     cass.insert(key, cparent, c, ConsistencyLevel::ONE);
+ 
+     // insert another column, "age"
+     c.name = "age";
+     c.value = "42";
+     cass.insert(key, cparent, c, ConsistencyLevel::ONE);
+ 
+     // get a single cell
+     ColumnPath cp;
+     cp.__isset.column = true;		// this must be set of you'll get an error re: Padraig O'Sullivan
+     cp.column = "name";
+     cp.column_family = "Standard1";
+     cp.super_column = "";
+     ColumnOrSuperColumn sc;
+ 
+     cass.get(sc, key, cp, ConsistencyLevel::ONE);
+     printf("Column [%s]  Value [%s]  TS [%lld]\n",
+       sc.column.name.c_str(), sc.column.value.c_str(), sc.column.timestamp);
+ 
+     // get the entire row for a key
+     SliceRange sr;
+     sr.start = "";
+     sr.finish = "";
+ 
+     SlicePredicate sp;
+     sp.slice_range = sr;
+     sp.__isset.slice_range = true; // set __isset for the columns instead if you use them
+ 
+     KeyRange range;
+     range.start_key = key;
+     range.end_key = "";
+     range.__isset.start_key = true;
+     range.__isset.end_key = true;
+ 
+     vector<KeySlice> results;
+     cass.get_range_slices(results, cparent, sp, range, ConsistencyLevel::ONE);
+     for(size_t i=0; i<results.size(); i++){
+       printf("Key: %s\n", results[i].key.c_str());
+       for(size_t x=0; x<results[i].columns.size(); x++){
+         printf("Column: %s  Value: %s\n", results[i].columns[x].column.name.c_str(),
+         results[i].columns[x].column.value.c_str());
+       }
+     }
+ 
+     tr->close();
+   }catch(TTransportException te){
+     printf("Exception: %s  [%d]\n", te.what(), te.getType());
+   }catch(InvalidRequestException ire){
+     printf("Exception: %s  [%s]\n", ire.what(), ire.why.c_str());
+   }catch(NotFoundException nfe){
+     printf("Exception: %s\n", nfe.what());
+   }
+   printf("Done!!!\n");
+   return;
+ }
+ }}}
  == Notes ==
  The Cassandra.Client object is always sending its request to the same Cassandra node in the cluster. The server then determines if and where the request should be routed to (Server-based routing). DNS Round Robin or a Cassandra.Client object pool connected to several servers in the cluster can be used to get higher throughput and availability.