Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 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 get_time_low(int64_t ts) {
  30. return ts >> 96L;
  31. }
  32.  
  33. int64_t get_time_mid(int64_t ts) {
  34. return (ts >> 80L) & 0xffff;
  35. }
  36.  
  37. int64_t get_time_hi_version(int64_t ts) {
  38. return (ts >> 64L) & 0xffff;
  39. }
  40.  
  41. int64_t get_time(int64_t ts) {
  42. return (((get_time_hi_version(ts) & 0x0fffL) << 48L) |
  43. (get_time_mid(ts) << 32L) | get_time_low(ts));
  44. }
  45.  
  46. int uuid_to_time(string uuid) {
  47. for ( int i = 0; i < uuid.length(); i++)
  48. {
  49. if (uuid[i] =='-')
  50. {
  51. uuid.replace(i,1,"");
  52. i += 1;
  53. }
  54. }
  55.  
  56. string tsFromUUID = uuid.substr(13, 3) + uuid.substr(8, 4) + uuid.substr(0, 8);
  57.  
  58. cout << "Timestamp in hex: " << tsFromUUID << endl;
  59. int64_t x;
  60. std::stringstream ss;
  61. ss << std::hex << tsFromUUID;
  62. ss >> x;
  63.  
  64. cout << "UUID No Hyphens: " << uuid << endl;
  65. cout << "Which should be: " << x << endl;
  66.  
  67. int64_t perhapsx = (perhapsx - 122192928000000000)/10000;
  68.  
  69. cout << "Perhaps: " << perhapsx << endl;
  70.  
  71. int64_t timex = get_time(perhapsx);
  72. //timex = (timex - 122192928000000000)/10000;
  73.  
  74. cout << "Maybe this: " << timex << endl;
  75. }
  76.  
  77. /*
  78. *** FOR REFERENCE ***
  79.  
  80.  
  81. def get_time_low(self):
  82. return self.int >> 96L
  83.  
  84. def get_time_mid(self):
  85. return (self.int >> 80L) & 0xffff
  86.  
  87. def get_time_hi_version(self):
  88. return (self.int >> 64L) & 0xffff
  89.  
  90. def get_time(self):
  91. return (((self.time_hi_version & 0x0fffL) << 48L) |
  92. (self.time_mid << 32L) | self.time_low)
  93.  
  94. def convert_uuid_to_time(uuid_arg):
  95. """
  96. Converts a version 1 UUID to a timestamp with the same precision
  97. as :meth:`time.time()` returns. This is useful for examining the
  98. results of queries returning v1 UUIDs.
  99.  
  100. :param uuid_arg: a version 1 UUID
  101.  
  102. """
  103. ts = uuid_arg.get_time()
  104. return (ts - 0x01b21dd213814000L)/1e7
  105. */
  106.  
  107. int64_t createTimestamp()
  108. {
  109. struct timeval tv;
  110. gettimeofday(&tv, NULL);
  111. return (int64_t) tv.tv_sec * 1000000 + (int64_t) tv.tv_usec;
  112. }
  113.  
  114.  
  115. int main()
  116. {
  117. try {
  118. boost::shared_ptr<TSocket> socket(new TSocket(host, port));
  119. boost::shared_ptr<TTransport> transport(new TFramedTransport(socket));
  120. boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
  121.  
  122. CassandraClient client(protocol);
  123. transport->open();
  124.  
  125. client.set_keyspace("Crawldata");
  126.  
  127. cout << "selected" << endl;
  128.  
  129. KeyRange kr;
  130. kr.start_key = "fetched_201011091330";
  131. kr.end_key = "fetched_201011091330";
  132. SlicePredicate sp;
  133. //could set sp.column_names here if we cared about column names
  134. //Set .start if we want to not get the whole thing, sames goes for .end
  135. //sr.start = <<CURRENT_TIMESTAMP_AS_UUID>>
  136. //SliceRange *sr = new SliceRange();
  137. SliceRange sr;
  138. sr.start = "";
  139. sr.count = 1000000;
  140. sp.slice_range = sr;
  141.  
  142. sp.__isset.slice_range = true;
  143. kr.__isset.start_key = true;
  144. kr.__isset.end_key = true;
  145.  
  146. ColumnParent cp;
  147. cp.column_family = "FetchedURLs";
  148.  
  149. ConsistencyLevel cl = ONE;
  150.  
  151. vector<KeySlice> ks;
  152.  
  153. client.get_range_slices(ks, cp, sp, kr, cl);
  154.  
  155. cout << "Got this many keys: " << ks.size() << endl;
  156.  
  157. for (int i = 0; i < ks.size(); i++) {
  158. vector<ColumnOrSuperColumn> cols = ks[i].columns;
  159. cout << "This many columns: " << cols.size() << endl;
  160. }
  161.  
  162. uuid_to_time(string("96a6d87e-ec2f-11df-9dc2-90e6baebb7ee"));
  163.  
  164. transport->close();
  165.  
  166. } catch (org::apache::cassandra::InvalidRequestException &ire) {
  167. cout << "InvalidRequestException: " << ire.why << endl;
  168. } catch (apache::thrift::transport::TTransportException &tte) {
  169. cout << "TTransportException: " << tte.what() << endl;
  170. } catch (TException &te) {
  171. cout << "TException: " << te.what() << endl;
  172. }
  173. return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement