Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #include <string.h>
  2. #include <sstream>
  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include <set>
  6. #include <vector>
  7. #include <tr1/memory>
  8. #include <string>
  9. #include <stdio.h>
  10.  
  11. #include <transport/TSocket.h>
  12. #include <transport/TBufferTransports.h>
  13. #include <protocol/TBinaryProtocol.h>
  14.  
  15. #include "gclocal/Cassandra.h"
  16.  
  17. using namespace apache::thrift;
  18. using namespace apache::thrift::protocol;
  19. using namespace apache::thrift::transport;
  20.  
  21. using namespace std;
  22. using namespace org::apache::cassandra;
  23. using namespace boost;
  24.  
  25. //Tried both localhost and 127.0.0.1... same result for both :(
  26. const string host("localhost");
  27. const int port= 9160;
  28.  
  29. int64_t uuid_to_time(string uuid) {
  30. cout << "UUID Sent: " << uuid << endl;
  31. for ( int i = 0; i < uuid.length(); i++)
  32. {
  33. if (uuid[i] =='-')
  34. {
  35. uuid.replace(i,1,"");
  36. i += 1;
  37. }
  38. }
  39.  
  40. string tsFromUUID = uuid.substr(13, 3) + uuid.substr(8, 4) + uuid.substr(0, 8);
  41.  
  42. //cout << "UUID No Hyphens: " << uuid << endl;
  43.  
  44. //cout << "Timestamp in hex: " << tsFromUUID << endl;
  45. int64_t x;
  46. std::stringstream ss;
  47. ss << std::hex << tsFromUUID;
  48. ss >> x;
  49.  
  50. //cout << "Which should be: " << x << endl;
  51.  
  52. int64_t perhapsx = (x - 122192928000000000)/10000;
  53.  
  54. cout << "Timestamp: " << perhapsx << endl;
  55.  
  56. return perhapsx;
  57. }
  58.  
  59. int64_t createTimestamp()
  60. {
  61. struct timeval tv;
  62. gettimeofday(&tv, NULL);
  63. return (int64_t) tv.tv_sec * 1000000 + (int64_t) tv.tv_usec;
  64. }
  65.  
  66.  
  67. int main()
  68. {
  69. try {
  70. boost::shared_ptr<TSocket> socket(new TSocket(host, port));
  71. boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));
  72. boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
  73.  
  74. CassandraClient client(protocol);
  75. transport->open();
  76.  
  77. client.set_keyspace("Crawldata");
  78.  
  79. cout << "selected" << endl;
  80.  
  81. KeyRange kr;
  82. kr.start_key = "fetched_201011091330";
  83. kr.end_key = "fetched_201011091330";
  84. SlicePredicate sp;
  85. //could set sp.column_names here if we cared about column names
  86. //Set .start if we want to not get the whole thing, sames goes for .end
  87. //sr.start = <<CURRENT_TIMESTAMP_AS_UUID>>
  88. //SliceRange *sr = new SliceRange();
  89. SliceRange sr;
  90. sr.start = "";
  91. sr.count = 1000000;
  92. sp.slice_range = sr;
  93.  
  94. sp.__isset.slice_range = true;
  95. kr.__isset.start_key = true;
  96. kr.__isset.end_key = true;
  97.  
  98. ColumnParent cp;
  99. cp.column_family = "FetchedURLs";
  100.  
  101. ConsistencyLevel cl = ONE;
  102.  
  103. vector<KeySlice> ks;
  104.  
  105. client.get_range_slices(ks, cp, sp, kr, cl);
  106.  
  107. cout << "Got this many keys: " << ks.size() << endl;
  108.  
  109. for (int i = 0; i < ks.size(); i++) {
  110. vector<ColumnOrSuperColumn> cols = ks[i].columns;
  111. cout << "This many columns: " << cols.size() << endl;
  112. }
  113.  
  114. int64_t ts64 = uuid_to_time(string("96a6d87e-ec2f-11df-9dc2-90e6baebb7ee"));
  115.  
  116. cout << "ts64: " << ts64 << endl;
  117.  
  118. transport->close();
  119.  
  120. } catch (org::apache::cassandra::InvalidRequestException &ire) {
  121. cout << "InvalidRequestException: " << ire.why << endl;
  122. } catch (apache::thrift::transport::TTransportException &tte) {
  123. cout << "TTransportException: " << tte.what() << endl;
  124. } catch (TException &te) {
  125. cout << "TException: " << te.what() << endl;
  126. }
  127. return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement