Skip to content

Instantly share code, notes, and snippets.

@mpenick
Created February 1, 2017 21:42
Show Gist options
  • Select an option

  • Save mpenick/6a8386455a86a7629ff79f5f1a1cc908 to your computer and use it in GitHub Desktop.

Select an option

Save mpenick/6a8386455a86a7629ff79f5f1a1cc908 to your computer and use it in GitHub Desktop.
<?php
$cluster = Cassandra::cluster()->build();
$session = $cluster->connect();
$session->execute(new Cassandra\SimpleStatement(
"CREATE KEYSPACE IF NOT EXISTS simplex
WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' }"));
$session->execute(new Cassandra\SimpleStatement("USE simplex"));
$session->execute(new Cassandra\SimpleStatement(
"CREATE TABLE IF NOT EXISTS users (id int PRIMARY KEY, name text)"
));
$session->execute(new Cassandra\SimpleStatement(
"INSERT INTO users (id, name) VALUES (1, 'Michael')"
));
$session->execute(new Cassandra\SimpleStatement(
"INSERT INTO users (id, name) VALUES (2, 'Joe')"
));
$session->execute(new Cassandra\SimpleStatement(
"INSERT INTO users (id, name) VALUES (3, 'Adam')"
));
$session->execute(new Cassandra\SimpleStatement(
"INSERT INTO users (id, name) VALUES (4, 'Sam')"
));
$statement = 'SELECT * FROM simplex.users';
$statement = new Cassandra\SimpleStatement($statement);
$options = array('page_size' => 2);
$options = new Cassandra\ExecutionOptions($options);
# To start over at the first page keep track of the first page
$first = $session->execute($statement, $options);
$rows = $first;
for ($i = 0; $i < 10; $i++) {
$count = 0;
# Iterate over the pages
while (! $rows->isLastPage()) {
$count += $rows->count();
foreach ($rows as $row) { echo "{$row['id']} {$row['name']}\n"; }
$rows = $rows->nextPage();
}
$count += $rows->count();
print "count when iterating: ".$count."\n";
# Reset to the first page
print "restarting at the first page\n";
$rows = $first;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment