Skip to content

Instantly share code, notes, and snippets.

@mpenick
Created June 29, 2016 22:09
Show Gist options
  • Select an option

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

Select an option

Save mpenick/cc2d684d326ea4cc1f38bb98a88e87c8 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 examples
WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' }"));
$session->execute(new Cassandra\SimpleStatement(
"CREATE TABLE IF NOT EXISTS examples.test (key int PRIMARY KEY, value text)"));
$query = "SELECT * FROM examples.test LIMIT 1";
$statement = new Cassandra\SimpleStatement($query);
$rows = $session->execute($statement);
if ($rows->count() === 0) {
print "Populating table...";
$query = "INSERT INTO examples.test (key, value) VALUES (?, ?)";
for ($i = 0; $i < 100000; $i++) {
$statement = new Cassandra\SimpleStatement($query);
$options = array('arguments' => array($i, str_pad("$i", 256)));
$options = new Cassandra\ExecutionOptions($options);
$session->execute($statement, $options);
}
print "Done.\n";
}
print "Paging table...\n";
$query = "SELECT * FROM examples.test";
$statement = new Cassandra\SimpleStatement($query);
$options = array('page_size' => 10);
$options = new Cassandra\ExecutionOptions($options);
$startMem = memory_get_usage();
$rows = $session->execute($statement, $options);
$count = 0;
while ($rows = $rows->nextPage()) {
if ($count % 1000 == 0) {
$endMem = memory_get_usage();
$memUsage = $endMem - $startMem;
print "Memory usage = Delta: $memUsage, Start: $startMem, End: $endMem\n";
}
$count += $rows->count();
}
print "Done.\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment