2011年3月24日木曜日

Cassandra C++ であそぼ(3)

#include <string>
#include <sstream>
#include <iostream>
#include <vector>
 
#include "Cassandra.h"
#include "cassandra_types.h"
 
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>

#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
 
 
const int port= 9160;
 
int main(int argc, char* argv[]) {
  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));
  boost::shared_ptr<TTransport>  tport(new TFramedTransport(socket));
  boost::shared_ptr<TProtocol>   pcol(new TBinaryProtocol(tport));
  CassandraClient                client(pcol);

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

    ColumnParent pare;
    pare.column_family = "Users";

    std::map<std::string, std::map<std::string, std::vector<Mutation> > > mmm;
    Mutation  mut;
    mut.__isset.column_or_supercolumn = true;
    mut.column_or_supercolumn.__isset.column = true;
    //mut.column_or_supercolumn.column.__isset = true;
    const char* keys[] = { "Aa", "Bb", "Cc", "Dd", "Ee", "Ff", "Gg", "Hh", "Ii", "Jj" };
    const char* firsts[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
    const char* lasts[] =  { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J" };
    std::string age;
    age.resize( sizeof(long) );
    long* lage = (long*)&age[0];
    for( int i = 0; i < 10; ++i ) {
      std::vector<Mutation> vm;
      mut.column_or_supercolumn.column.name = "first";
      mut.column_or_supercolumn.column.value = firsts[i];
      mut.column_or_supercolumn.column.timestamp 
        = (boost::posix_time::second_clock::universal_time() 
         - boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
      vm.push_back( mut );
      mut.column_or_supercolumn.column.name = "last";
      mut.column_or_supercolumn.column.value = lasts[i];
      vm.push_back( mut );
      *lage = 35 + i;
      std::reverse( age.begin(), age.end() );
      mut.column_or_supercolumn.column.name = "age";
      mut.column_or_supercolumn.column.value = age;
      vm.push_back( mut );
      mmm[ keys[i] ][ "Users" ] = vm;
    }
    client.batch_mutate( mmm, ConsistencyLevel::ONE );

    SlicePredicate predicate;
    predicate.__isset.column_names = true;
    predicate.column_names.push_back( "first" );
    predicate.column_names.push_back( "last" );
    predicate.column_names.push_back( "age" );

    std::vector<ColumnOrSuperColumn> v;
    client.get_slice( v, "Aa", pare, predicate, ConsistencyLevel::ONE );

    for( int i = 0; i < v.size(); ++i ) {
      std::cout << v[i].column.name << ":";
      if( v[i].column.name == "age" ) { 
        // big endian 
        std::reverse( v[i].column.value.begin(), v[i].column.value.end() );
        std::cout << *(long*)(&v[i].column.value[0]) << std::endl;
      } else {
        std::cout << v[i].column.value << std::endl;
      }
    }

    tport->close();
  } catch (InvalidRequestException &re) {
    std::cerr << "invalid request: " << re.why.c_str() << std::endl;
  } catch (TException &tx) {
    std::cerr << "ERROR: " << tx.what() << std::endl;
  }
  return 0;
}



ん???何か間違ってる…
ちゃう
create column family Users with comparator=UTF8Type and default_validation_class=UTF8Type;

create column family Users;
だけしていると、あかんだけやった。

0 件のコメント: