· 9 years ago · Feb 01, 2017, 10:22 PM
1<?php
2
3$cluster = Cassandra::cluster()->build();
4$session = $cluster->connect();
5
6$session->execute(new Cassandra\SimpleStatement(
7 "CREATE KEYSPACE IF NOT EXISTS simplex
8 WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' }"));
9
10$session->execute(new Cassandra\SimpleStatement("USE simplex"));
11
12$session->execute(new Cassandra\SimpleStatement(
13 "CREATE TABLE IF NOT EXISTS users (id int PRIMARY KEY, name text)"
14));
15
16$session->execute(new Cassandra\SimpleStatement(
17 "INSERT INTO users (id, name) VALUES (1, 'Michael')"
18));
19
20$session->execute(new Cassandra\SimpleStatement(
21 "INSERT INTO users (id, name) VALUES (2, 'Joe')"
22));
23
24$session->execute(new Cassandra\SimpleStatement(
25 "INSERT INTO users (id, name) VALUES (3, 'Adam')"
26));
27
28$session->execute(new Cassandra\SimpleStatement(
29 "INSERT INTO users (id, name) VALUES (4, 'Sam')"
30));
31
32$statement = 'SELECT * FROM simplex.users';
33$statement = new Cassandra\SimpleStatement($statement);
34$options = array('page_size' => 2);
35$options = new Cassandra\ExecutionOptions($options);
36
37
38# To start over at the first page keep track of the first page
39$first = $session->execute($statement, $options);
40$rows = $first;
41
42for ($i = 0; $i < 10; $i++) {
43 $count = 0;
44 # Iterate over the pages
45 while (! $rows->isLastPage()) {
46 $count += $rows->count();
47 foreach ($rows as $row) { echo "{$row['id']} {$row['name']}\n"; }
48 $rows = $rows->nextPage();
49 }
50 $count += $rows->count();
51 print "count when iterating: ".$count."\n";
52
53 # Reset to the first page
54 print "restarting at the first page\n";
55 $rows = $first;
56}