CollectAgent.cpp 3.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//============================================================================
// Name        : CollectAgent.cpp
// Author      : Axel Auweter
// Version     :
// Copyright   : Leibniz Supercomputing Centre
// Description : As of now this is some rudimentary bad code to talk to the DB
//============================================================================

#include <stdio.h>
#include <stdlib.h>

#include <sys/socket.h>
#include <netinet/in.h>

#include <thrift/Thrift.h>
#include <thrift/transport/TTransport.h>
#include <thrift/transport/TBufferTransports.h>
#include <thrift/transport/TSocket.h>
#include <thrift/protocol/TProtocol.h>
#include <thrift/protocol/TBinaryProtocol.h>

#include "cassandra/cassandra.h"

using namespace apache::thrift;
using namespace apache::thrift::transport;
using namespace apache::thrift::protocol;
using namespace org::apache::cassandra;

int main(void) {
  boost::shared_ptr<TSocket> sock;
  boost::shared_ptr<TTransport> tr;
  boost::shared_ptr<TProtocol> prot;

  std::string clusterName;

  sock = boost::shared_ptr<TSocket>(new TSocket("localhost", 9160));
  tr = boost::shared_ptr<TFramedTransport>(new TFramedTransport(sock));
  prot = boost::shared_ptr<TBinaryProtocol>(new TBinaryProtocol(tr));

  try {
      CassandraClient myClient(prot);
      tr->open();
      myClient.describe_cluster_name(clusterName);
      printf("Cluster name: %s\n", clusterName.c_str());

      printf("Keyspaces:\n");
      std::vector<KsDef> keySpaces;
      myClient.describe_keyspaces(keySpaces);
      for (int i=0; i<keySpaces.size(); i++) {
          printf("   [%d]: %s\n", i, keySpaces[i].name.c_str());
      }

      CqlResult res;
      std::string query = "USE " + keySpaces[0].name;
      printf("Sending CQL statement: %s...", query.c_str());
      myClient.execute_cql3_query(res, query, Compression::NONE, ConsistencyLevel::ONE);
      printf(" Done.\n");

      int resB = 0;
      while (resB < keySpaces[0].cf_defs.size()) {
          query = "SELECT * FROM " + keySpaces[0].cf_defs[resB].name + ";";
          printf("Sending CQL statement: %s...", query.c_str());
          myClient.execute_cql3_query(res, query, Compression::NONE, ConsistencyLevel::ONE);
          printf(" Done.\nResult:\n");
          int resC = 0;
          while (resC < res.rows.size()) {
              int resD = 0;
              while (resD < res.rows[resC].columns.size()) {
                  printf("%s [%lu]:", res.rows[resC].columns[resD].name.c_str(),
                      res.rows[resC].columns[resD].value.length());
                  int resE = 0;
                  while (resE < res.rows[resC].columns[resD].value.length()) {
                      printf(" %02x", res.rows[resC].columns[resD].value.c_str()[resE] & 0xff);
                      resE++;
                  }
                  printf("\n");
                  resD++;
              }
              resC++;
          }
          resB++;
      }
  }
  catch(TTransportException te){
      printf("TP Exception: %s  [%d]\n", te.what(), te.getType());
  }catch(InvalidRequestException ire){
      printf("IRE Exception: %s  [%s]\n", ire.what(), ire.why.c_str());
  }catch(NotFoundException nfe){
      printf("NF Exception: %s\n", nfe.what());
  }
  return EXIT_SUCCESS;
}