Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. <?php
  2. class Iterator_Cassandra
  3. {
  4.  
  5. protected $initialRows;
  6.  
  7. public function __construct(Cassandra\Rows $rows) {
  8. $this->initialRows = $rows;
  9. }
  10.  
  11. public function count() {
  12. // Paginate and reset
  13. $startMem = memory_get_usage();
  14. $count = $this->initialRows->count();
  15. while ($this->initialRows = $this->initialRows->nextPage()) {
  16. if ($count % 1000 == 0) {
  17. $endMem = memory_get_usage();
  18. $memUsage = $endMem - $startMem;
  19. print "Memory usage = Delta: $memUsage, Start: $startMem, End: $endMem\n";
  20. }
  21. $count += $this->initialRows->count();
  22. }
  23.  
  24. return (int)$count;
  25. }
  26. }
  27.  
  28.  
  29. $cluster = Cassandra::cluster()->build();
  30. $session = $cluster->connect();
  31.  
  32. $session->execute(new Cassandra\SimpleStatement(
  33. "CREATE KEYSPACE IF NOT EXISTS totalsocial
  34. WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' }"));
  35.  
  36. $session->execute(new Cassandra\SimpleStatement(
  37. "CREATE TYPE IF NOT EXISTS totalsocial.field_totalsocial_score (
  38. \"v\" float,
  39. \"m\" float,
  40. \"b\" float
  41. )"
  42. ));
  43.  
  44. $session->execute(new Cassandra\SimpleStatement(
  45. "CREATE TYPE IF NOT EXISTS totalsocial.field_totalsocial_sentiment (
  46. \"n\" float,
  47. \"p\" float,
  48. \"ne\" float,
  49. \"m\" float
  50. );"
  51. ));
  52.  
  53. $session->execute(new Cassandra\SimpleStatement(
  54. "CREATE TABLE IF NOT EXISTS totalsocial.\"totalsocial_report_talktrack_week\" (
  55. \"i\" text,
  56. \"iv\" text,
  57. \"sd\" timestamp,
  58. \"ed\" timestamp,
  59. \"ms\" double,
  60. \"is\" double,
  61. \"tm\" double,
  62. \"ti\" double,
  63. \"ts\" double,
  64. \"sv\" frozen<field_totalsocial_score>,
  65. \"st\" frozen<field_totalsocial_sentiment>,
  66. \"rs\" double,
  67. \"ss\" frozen<field_totalsocial_score>,
  68. \"mmr\" double,
  69. \"mmb\" double,
  70. \"rbs\" double,
  71. \"sbs\" frozen<field_totalsocial_score>,
  72. \"cs\" double,
  73. \"tc\" double,
  74. \"pc\" double,
  75. \"us\" double,
  76. \"tu\" double,
  77. \"pu\" double,
  78. \"ri\" double,
  79. \"si\" frozen<field_totalsocial_score>,
  80. PRIMARY KEY (\"i\", \"iv\", \"sd\")
  81. );"
  82. ));
  83.  
  84. $tableName = "totalsocial.totalsocial_report_talktrack_week";
  85.  
  86. $scoreType = Cassandra\Type::userType(
  87. "v", Cassandra\Type::float(),
  88. "m", Cassandra\Type::float(),
  89. "b", Cassandra\Type::float()
  90. );
  91.  
  92. $sentimentType = Cassandra\Type::userType(
  93. "n", Cassandra\Type::float(),
  94. "p", Cassandra\Type::float(),
  95. "ne", Cassandra\Type::float(),
  96. "m", Cassandra\Type::float()
  97. );
  98.  
  99. $query = "SELECT * FROM $tableName LIMIT 1";
  100. $statement = new Cassandra\SimpleStatement($query);
  101. $rows = $session->execute($statement);
  102.  
  103. if ($rows->count() === 0) {
  104. print "Populating table...";
  105. $query = "INSERT INTO $tableName
  106. (i, iv, sd, ed, ms,
  107. \"is\", tm, ti, ts, sv,
  108. st, rs, ss, mmr, mmb,
  109. rbs, sbs, cs, tc, pc,
  110. us, tu, pu, ri, si) VALUES
  111. (?, ?, ?, ?, ?,
  112. ?, ?, ?, ?, ?,
  113. ?, ?, ?, ?, ?,
  114. ?, ?, ?, ?, ?,
  115. ?, ?, ?, ?, ?)";
  116. for ($i = 0; $i < 100000; $i++) {
  117. $statement = new Cassandra\SimpleStatement($query);
  118.  
  119. $i = $iv = "$i";
  120. $sd = $ed = new Cassandra\Timestamp();
  121. $ms = $is = $tm = $ti = $ts = 1.0;
  122. $sv = $scoreType->create(
  123. "v", new Cassandra\Float(2.0),
  124. "m", new Cassandra\Float(3.0),
  125. "b", new Cassandra\Float(4.0)
  126. );
  127. $st = $sentimentType->create(
  128. "n", new Cassandra\Float(5.0),
  129. "p", new Cassandra\Float(6.0),
  130. "ne", new Cassandra\Float(7.0),
  131. "m", new Cassandra\Float(8.0)
  132. );
  133. $rs = 9.0;
  134. $ss = $scoreType->create(
  135. "v", new Cassandra\Float(10.0),
  136. "m", new Cassandra\Float(11.0),
  137. "b", new Cassandra\Float(12.0)
  138. );
  139. $mmr = $mmb = $rbs = 13.0;
  140. $sbs = $scoreType->create(
  141. "v", new Cassandra\Float(14.0),
  142. "m", new Cassandra\Float(15.0),
  143. "b", new Cassandra\Float(16.0)
  144. );
  145. $cs = $tc = $pc = $us = $tu = $pu = $ri = 17.0;
  146. $si = $scoreType->create(
  147. "v", new Cassandra\Float(18.0),
  148. "m", new Cassandra\Float(19.0),
  149. "b", new Cassandra\Float(20.0)
  150. );
  151.  
  152. $options = array('arguments' =>
  153. array(
  154. $i, $iv, $sd, $ed, $ms,
  155. $is, $tm, $ti, $ts, $sv,
  156. $st, $rs, $ss, $mmr, $mmb,
  157. $rbs, $sbs, $cs, $tc, $pc,
  158. $us, $tu, $pu, $ri, $si
  159. ));
  160.  
  161. $options = new Cassandra\ExecutionOptions($options);
  162. $session->execute($statement, $options);
  163. }
  164. print "Done.\n";
  165. }
  166.  
  167. print "Paging table...\n";
  168. $query = "SELECT * FROM $tableName";
  169. $statement = new Cassandra\SimpleStatement($query);
  170. $options = array('page_size' => 10);
  171. $options = new Cassandra\ExecutionOptions($options);
  172.  
  173. $startMem = memory_get_usage();
  174. $rows = $session->execute($statement, $options);
  175. $iterator = new Iterator_Cassandra($rows);
  176. $count = $iterator->count();
  177. print "Done.\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement