thrift_wrapper.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "Thrift.h"
  2. #include "transport/TSocket.h"
  3. #include "transport/TTransport.h"
  4. #include "transport/TBufferTransports.h"
  5. #include "protocol/TProtocol.h"
  6. #include "protocol/TBinaryProtocol.h"
  7. #include "Cassandra.h"
  8. #include "thrift_wrapper.h"
  9. using namespace std;
  10. using namespace apache::thrift;
  11. using namespace apache::thrift::transport;
  12. using namespace apache::thrift::protocol;
  13. using namespace org::apache::cassandra;
  14. extern "C" int insert_wrap(char* host, int port, char* keyspace, char* column_family, char* key, char* column, char** value)
  15. {
  16. int ret = -1;
  17. try{
  18. boost::shared_ptr<TTransport> socket = boost::shared_ptr<TSocket>(new TSocket(host, port));
  19. boost::shared_ptr<TTransport> tr = boost::shared_ptr<TFramedTransport>(new TFramedTransport (socket));
  20. boost::shared_ptr<TProtocol> p = boost::shared_ptr<TBinaryProtocol>(new TBinaryProtocol(tr));
  21. CassandraClient cass(p);
  22. tr->open();
  23. cass.set_keyspace(keyspace);
  24. ColumnParent cparent;
  25. cparent.column_family = column_family;
  26. string key_str = key;
  27. Column c;
  28. c.__isset.value = true;
  29. // have to go through all of this just to get the timestamp in ms
  30. struct timeval td;
  31. gettimeofday(&td, NULL);
  32. int64_t ms = td.tv_sec;
  33. ms = ms * 1000;
  34. int64_t usec = td.tv_usec;
  35. usec = usec / 1000;
  36. ms += usec;
  37. c.timestamp = ms;
  38. c.__isset.timestamp = true;
  39. // insert the "name" column
  40. c.name = column;
  41. c.value = *value;
  42. cass.insert(key_str, cparent, c, ConsistencyLevel::ONE);
  43. //Closing connection
  44. tr->flush();
  45. tr->close();
  46. //Success
  47. ret = 1;
  48. }catch(TTransportException te){
  49. printf("Exception: %s [%d]\n", te.what(), te.getType());
  50. }catch(InvalidRequestException ire){
  51. printf("Exception: %s [%s]\n", ire.what(), ire.why.c_str());
  52. }catch(NotFoundException nfe){
  53. printf("Exception: %s\n", nfe.what());
  54. }catch (...) {
  55. printf("Unknown exception occurred\n");
  56. }
  57. return ret;
  58. }
  59. extern "C" int retrieve_wrap(char* host, int port, char* keyspace, char* column_family, char* key, char* column, char** value)
  60. {
  61. int ret = -1;
  62. try{
  63. boost::shared_ptr<TTransport> socket = boost::shared_ptr<TSocket>(new TSocket(host, port));
  64. boost::shared_ptr<TTransport> tr = boost::shared_ptr<TFramedTransport>(new TFramedTransport (socket));
  65. boost::shared_ptr<TProtocol> p = boost::shared_ptr<TBinaryProtocol>(new TBinaryProtocol(tr));
  66. CassandraClient cass(p);
  67. tr->open();
  68. cass.set_keyspace(keyspace);
  69. string key_str = key;
  70. Column c;
  71. c.__isset.value = true;
  72. // have to go through all of this just to get the timestamp in ms
  73. struct timeval td;
  74. gettimeofday(&td, NULL);
  75. int64_t ms = td.tv_sec;
  76. ms = ms * 1000;
  77. int64_t usec = td.tv_usec;
  78. usec = usec / 1000;
  79. ms += usec;
  80. c.timestamp = ms;
  81. c.__isset.timestamp = true;
  82. // get a single cell
  83. ColumnPath cp;
  84. cp.__isset.column = true; // this must be set of you'll get an error re: Padraig O'Sullivan
  85. cp.column = column;
  86. cp.column_family = column_family;
  87. cp.super_column = "";
  88. ColumnOrSuperColumn sc;
  89. cass.get(sc, key, cp, ConsistencyLevel::ONE);
  90. // Copying the value back. Caller needs to free it.
  91. string value_str (sc.column.value.c_str());
  92. *value = strdup(sc.column.value.c_str());
  93. //Closing connection
  94. tr->flush();
  95. tr->close();
  96. //Success
  97. ret = 1;
  98. }catch(TTransportException te){
  99. printf("Exception: %s [%d]\n", te.what(), te.getType());
  100. }catch(InvalidRequestException ire){
  101. printf("Exception: %s [%s]\n", ire.what(), ire.why.c_str());
  102. }catch(NotFoundException nfe){
  103. printf("Exception: %s\n", nfe.what());
  104. }catch (...) {
  105. printf("Unknown exception occurred\n");
  106. }
  107. return ret;
  108. }