Permalink
Browse files

added setMultiple to CassandraColumnFamily

  • Loading branch information...
1 parent 9d90d1d commit fe62d909d44fb15a5eec218d74c0e754541fe2f5 @willowandcreek willowandcreek committed Aug 13, 2012
Showing with 94 additions and 0 deletions.
  1. +48 −0 Cassandra.php
  2. +46 −0 example.php
View
48 Cassandra.php
@@ -2562,6 +2562,54 @@ public function set(
}
/**
+ * Inserts multiple new rows or updates existing ones.
+ *
+ * If a key already exists with some columns and you update it, any columns
+ * not listed in the update statement will not be changed or deleted.
+ *
+ * If not set, default consistency level set in the constructor is used.
+ *
+ * You generally do not need to provide a timestamp, it is generated for
+ * you.
+ *
+ * You may optionally provide the time-to-live period in seconds after which
+ * the entry will appear deleted.
+ *
+ * @param array $dataMap Array of keys with their associated column names and their values
+ * @param integer $consistency Consistency level to use
+ * @param integer $timestamp Optional timestamp to use.
+ * @param integer $timeToLiveSeconds Optional time-to-live period
+ * @throws Exception If something goes wrong
+ */
+ public function setMultiple(
+ array $dataMap,
+ $consistency = null,
+ $timestamp = null,
+ $timeToLiveSeconds = null
+ ) {
+ if ($timestamp === null) {
+ $timestamp = CassandraUtil::getTimestamp();
+ }
+
+ if ($consistency === null) {
+ $consistency = $this->defaultWriteConsistency;
+ }
+
+ $mutationMap = array();
+ foreach ($dataMap as $key => $columns) {
+ $mutationMap[$key] = array(
+ $this->name => $this->createColumnMutations(
+ $columns,
+ $timestamp,
+ $timeToLiveSeconds
+ )
+ );
+ }
+
+ $this->cassandra->call('batch_mutate', $mutationMap, $consistency);
+ }
+
+ /**
* Updates counter column by some amount defaulting to one.
*
* @param string $key Key name
View
46 example.php
@@ -270,6 +270,52 @@
$tallinn = $cassandra->get('cities.Estonia.Tallinn:population,size');
echo 'Super-column cities.Estonia.Tallinn: <pre>'.print_r($tallinn, true).'</pre><hr/>';
+// we can also insert multiple rows and columns
+$cassandra->cf('user')->setMultiple(array(
+ 'sam' => array(
+ 'email' => 'sam@email.com',
+ 'name' => 'Sam J',
+ 'age' => 26
+ ),
+ 'rick' => array(
+ 'email' => 'rick@email.com',
+ 'name' => 'Rick P',
+ 'age' => 48
+ )
+));
+
+$samAndRick = $cassandra->cf('user')->getMultiple(array('sam', 'rick'));
+echo 'Users "sam" and "rick": <pre>'.print_r($samAndRick, true).'</pre><hr/>';
+
+// we can also insert multiple entries into super column families
+$cassandra->cf('cities')->setMultiple(array(
+ 'SomeCountryX' => array(
+ 'CityA' => array(
+ 'population' => '23432',
+ 'comment' => 'Weird city name',
+ 'size' => 'medium'
+ ),
+ 'CityB' => array(
+ 'population' => '2332',
+ 'comment' => 'City of bees?',
+ 'size' => 'small'
+ )
+ ),
+ 'SomeCountryY' => array(
+ 'CityC' => array(
+ 'population' => '2352343',
+ 'comment' => 'Capital of Y',
+ 'size' => 'massive'
+ )
+ )
+));
+
+$countryX = $cassandra->cf('cities')->getAll('SomeCountryX');
+echo 'Super-column cities.SomeCountryX: <pre>'.print_r($countryX, true).'</pre><hr/>';
+
+$countryY = $cassandra->cf('cities')->getAll('SomeCountryY');
+echo 'Super-column cities.SomeCountryY: <pre>'.print_r($countryY, true).'</pre><hr/>';
+
/*
// you can delete all the data in a column family using "truncate"

0 comments on commit fe62d90

Please sign in to comment.