2011年3月23日水曜日

Cassandra C++ であそぼ

#include <string>
#include <sstream>
#include <iostream>
 
#include "Cassandra.h"
#include "cassandra_types.h"
 
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
 
 
const int port= 9160;
 
int main()
{
  using namespace apache::thrift;
  using namespace apache::thrift::protocol;
  using namespace apache::thrift::transport;
  using namespace org::apache::cassandra;

  boost::shared_ptr<TSocket> socket(new TSocket("localhost", port));
  // cassandra version 7.0 interface needs to TFramedTransport !!!
  boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));
  boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
  CassandraClient client(protocol);

  try {
    transport->open();
    client.set_keyspace( "Keyspace1" );

    ColumnPath path;
    path.__isset.column = true; // this is required!
    path.column_family = "Users";
    path.column = "last";

    ColumnOrSuperColumn ret_val;
    client.get(ret_val, "jsmith", path, ConsistencyLevel::ONE);
    std::cout << "name : " << ret_val.column.name.c_str() << std::endl;
    std::cout << "value: " << ret_val.column.value.c_str() << std::endl;
 
    transport->close();
  } catch (InvalidRequestException &re) {
    std::cerr << "invalid request: " << re.why.c_str() << std::endl;
  } catch (TException &tx) {
    std::cerr << "ERROR: " << tx.what() << std::endl;
  }
}


0 件のコメント: