PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/example.php

https://github.com/furqanrydhan/Cassandra-PHP-Client-Library
PHP | 280 lines | 152 code | 44 blank | 84 comment | 0 complexity | 124055b6a301eeb9752d6bc39cd579a1 MD5 | raw file
  1. <?php
  2. // show all the errors
  3. error_reporting(E_ALL);
  4. // the only file that needs including into your project
  5. require_once 'Cassandra.php';
  6. // list of seed servers to randomly connect to
  7. // all the parameters are optional and default to given values
  8. $servers = array(
  9. array(
  10. 'host' => '127.0.0.1',
  11. 'port' => 9160,
  12. 'use-framed-transport' => true,
  13. 'send-timeout-ms' => 1000,
  14. 'receive-timeout-ms' => 1000
  15. )
  16. );
  17. // create a named singleton, the second parameter name defaults to "main"
  18. // you can have several named singletons with different server pools
  19. $cassandra = Cassandra::createInstance($servers);
  20. // at any point in code, you can get the named singleton instance, the name
  21. // again defaults to "main" so no need to define it if using single instance
  22. $cassandra2 = Cassandra::getInstance();
  23. // drop the example keyspace and ignore errors should it not exist
  24. try {
  25. $cassandra->dropKeyspace('CassandraExample');
  26. } catch (Exception $e) {}
  27. // create a new keyspace, accepts extra parameters for replication options
  28. // normally you don't do it every time
  29. $cassandra->createKeyspace('CassandraExample');
  30. // start using the created keyspace
  31. $cassandra->useKeyspace('CassandraExample');
  32. // if a request fails, it will be retried for this many times, each time backing
  33. // down for a bit longer period to prevent floods; defaults to 5
  34. $cassandra->setMaxCallRetries(5);
  35. // create a standard column family with given column metadata
  36. $cassandra->createStandardColumnFamily(
  37. 'CassandraExample', // keyspace name
  38. 'user', // the column-family name
  39. array( // list of columns with metadata
  40. array(
  41. 'name' => 'name',
  42. 'type' => Cassandra::TYPE_UTF8,
  43. 'index-type' => Cassandra::INDEX_KEYS, // create secondary index
  44. 'index-name' => 'NameIdx'
  45. ),
  46. array(
  47. 'name' => 'email',
  48. 'type' => Cassandra::TYPE_UTF8
  49. ),
  50. array(
  51. 'name' => 'age',
  52. 'type' => Cassandra::TYPE_INTEGER,
  53. 'index-type' => Cassandra::INDEX_KEYS,
  54. 'index-name' => 'AgeIdx'
  55. )
  56. )
  57. // actually accepts more parameters with reasonable defaults
  58. );
  59. // create a standard column family with given column metadata
  60. $cassandra->createStandardColumnFamily(
  61. 'CassandraExample',
  62. 'counter',
  63. array(),
  64. Cassandra::TYPE_UTF8,
  65. Cassandra::TYPE_COUNTER
  66. );
  67. // create a super column family
  68. $cassandra->createSuperColumnFamily(
  69. 'CassandraExample',
  70. 'cities',
  71. array(
  72. array(
  73. 'name' => 'population',
  74. 'type' => Cassandra::TYPE_INTEGER
  75. ),
  76. array(
  77. 'name' => 'comment',
  78. 'type' => Cassandra::TYPE_UTF8
  79. )
  80. ),
  81. // see the definition for these additional optional parameters
  82. Cassandra::TYPE_UTF8,
  83. Cassandra::TYPE_UTF8,
  84. Cassandra::TYPE_UTF8,
  85. 'Capitals supercolumn test',
  86. 1000,
  87. 1000,
  88. 0.5
  89. );
  90. // lets fetch and display the schema of created keyspace
  91. $schema = $cassandra->getKeyspaceSchema('CassandraExample');
  92. echo 'Schema: <pre>'.print_r($schema, true).'</pre><hr/>';
  93. /*
  94. // should we need to, we can access the low-level client directly
  95. $version = $cassandra->getConnection()->getClient()->describe_version();
  96. echo 'Version directly: <pre>'.print_r($version, true).'</pre><hr/>';
  97. */
  98. // if implemented, use the wrapped methods as these are smarter - can retry etc
  99. $version = $cassandra->getVersion();
  100. echo 'Version through wrapper: <pre>'.print_r($version, true).'</pre><hr/>';
  101. // cluster is a pool of connections
  102. $cluster = $cassandra->getCluster();
  103. echo 'Cluster: <pre>'.print_r($cluster, true).'</pre><hr/>';
  104. // you can ask the cluster for a connection to a random seed server from pool
  105. $connection = $cluster->getConnection();
  106. echo 'Connection: <pre>'.print_r($connection, true).'</pre><hr/>';
  107. // access column family, using the singleton syntax
  108. // there is shorter "cf" methid that is an alias to "columnFamily"
  109. $userColumnFamily = Cassandra::getInstance()->columnFamily('user');
  110. echo 'Column family "user": <pre>'.print_r($userColumnFamily, true).'</pre><hr/>';
  111. // lets insert some test data using the convinience method "set" of Cassandra
  112. // the syntax is COLUMN_FAMILY_NAME.KEY_NAME
  113. $cassandra->set(
  114. 'user.john',
  115. array(
  116. 'email' => 'john@smith.com',
  117. 'name' => 'John Smith',
  118. 'age' => 34
  119. )
  120. );
  121. // when inserting data, it's ok if key name contains ".", no need to escape them
  122. $cassandra->set(
  123. 'user.jane.doe',
  124. array(
  125. 'email' => 'jane@doe.com',
  126. 'name' => 'Jane Doe',
  127. 'age' => 24
  128. )
  129. );
  130. // longer way of inserting data, first getting the column family
  131. $cassandra->cf('user')->set(
  132. 'chuck', // key name
  133. array( // column names and values
  134. 'email' => 'chuck@norris.com',
  135. 'name' => 'Chuck Norris',
  136. 'age' => 24
  137. ),
  138. Cassandra::CONSISTENCY_QUORUM // optional consistency to use
  139. // also accepts optional custom timestamp and time to live
  140. );
  141. // lets fetch all the information about user john
  142. $john = $cassandra->get('user.john');
  143. echo 'User "john": <pre>'.print_r($john, true).'</pre><hr/>';
  144. // since the jane key "jane.doe" includes a ".", we have to escape it
  145. $jane = $cassandra->get('user.'.Cassandra::escape('jane.doe'));
  146. echo 'User "jane.doe": <pre>'.print_r($jane, true).'</pre><hr/>';
  147. // there is some syntatic sugar on the query of Cassandra::get() allowing you
  148. // to fetch specific columns, ranges of them, limit amount etc. for example,
  149. // lets only fetch columns name and age
  150. $chuck = $cassandra->get('user.chuck:name,age');
  151. echo 'User "chuck", name and age: <pre>'.print_r($chuck, true).'</pre><hr/>';
  152. // fetch all solumns from age to name (gets all columns in-between too)
  153. $chuck2 = $cassandra->get('user.chuck:age-name');
  154. echo 'User "chuck", columns ago to name: <pre>'.print_r($chuck2, true).'</pre><hr/>';
  155. // the range columns do not need to exist, we can get character ranges
  156. $chuck3 = $cassandra->get('user.chuck:a-z');
  157. echo 'User "chuck", columns a-z: <pre>'.print_r($chuck3, true).'</pre><hr/>';
  158. // when performing range queries, we can also limit the number of columns
  159. // returned (2); also the method accepts consistency level as second parameter
  160. $chuck4 = $cassandra->get('user.chuck:a-z|2', Cassandra::CONSISTENCY_ALL);
  161. echo 'User "chuck", columns a-z, limited to 2 columns: <pre>'.print_r($chuck4, true).'</pre><hr/>';
  162. // the Cassandra::get() is a convinience method proxying to lower level
  163. // CassandraColumnFamily::get(), no need to worry about escaping with this.
  164. // column family has additional methods getAll(), getColumns(), getColumnRange()
  165. // that all map to lower level get() calls with more appopriate parameters
  166. $jane2 = $cassandra->cf('user')->get('jane.doe');
  167. echo 'User "jane.doe", lower level api: <pre>'.print_r($jane2, true).'</pre><hr/>';
  168. // we defined a secondary index on "age" column of "user" column family so we
  169. // can use CassandraColumnFamily::getWhere() to fetch users of specific age.
  170. // this returns an iterator that you can go over with foreach or use the
  171. // getAll() method that fetches all the data and returns an array
  172. $aged24 = $cassandra->cf('user')->getWhere(array('age' => 24));
  173. echo 'Users at age 24: <pre>'.print_r($aged24->getAll(), true).'</pre><hr/>';
  174. // if we know we are going to need to values of several keys, we can request
  175. // them in a single query for better performance
  176. $chuckAndJohn = $cassandra->cf('user')->getMultiple(array('chuck', 'john'));
  177. echo 'Users "chuck" and "john": <pre>'.print_r($chuckAndJohn, true).'</pre><hr/>';
  178. /* Uncomment this when using order preserving partitioner
  179. // we can fetch a range of keys but this is predictable only if using an
  180. // order preserving partitioner, Cassandra defaults to random one
  181. // again as there may be more results than it's reasonable to fetch in a single
  182. // query, an iterator is returned that can make several smaller range queries
  183. // as the data is iterated
  184. $usersAZ = $cassandra->cf('user')->getKeyRange('a', 'z');
  185. echo 'Users with keys in range a-z: <pre>'.print_r($usersAZ->getAll(), true).'</pre><hr/>';
  186. */
  187. // counters example
  188. /*$cassandra->set(
  189. 'counter.test',
  190. array(
  191. 'first' => 1,
  192. 'second' => 10
  193. )
  194. );*/
  195. $cassandra->cf('counter')->increment('test', 'first');
  196. $testCounter = $cassandra->get('counter.test');
  197. echo 'Test counter after increment: <pre>'.print_r($testCounter, true).'</pre><hr/>';
  198. $cassandra->cf('counter')->updateCounter('test', 'first', 10);
  199. $testCounter = $cassandra->get('counter.test');
  200. echo 'Test counter after second increment by 10: <pre>'.print_r($testCounter, true).'</pre><hr/>';
  201. // find the number of columns a key has, we could also request for ranges
  202. $chuckColumnCount = $cassandra->cf('user')->getColumnCount('chuck');
  203. echo 'User "chuck" column count: <pre>'.print_r($chuckColumnCount, true).'</pre><hr/>';
  204. // we can find column counts for several keys at once
  205. $chuckJaneColumnCounts = $cassandra->cf('user')->getColumnCounts(array('chuck', 'jane.doe'));
  206. echo 'User "chuck" and "jane.doe" column counts: <pre>'.print_r($chuckJaneColumnCounts, true).'</pre><hr/>';
  207. // setting supercolumn values is similar to normal column families
  208. $cassandra->set(
  209. 'cities.Estonia',
  210. array(
  211. 'Tallinn' => array(
  212. 'population' => '411980',
  213. 'comment' => 'Capital of Estonia',
  214. 'size' => 'big'
  215. ),
  216. 'Tartu' => array(
  217. 'population' => '98589',
  218. 'comment' => 'City of good thoughts',
  219. 'size' => 'medium'
  220. )
  221. )
  222. );
  223. // fetch all columns of Tartu in Estonia of cities
  224. $tartu = $cassandra->cf('cities')->getAll('Estonia', 'Tartu');
  225. echo 'Super-column cities.Estonia.Tartu: <pre>'.print_r($tartu, true).'</pre><hr/>';
  226. // we could also use the higher level Cassandra::get() to fetch supercolumn info
  227. // we can still use the additional filters of columns
  228. $tallinn = $cassandra->get('cities.Estonia.Tallinn:population,size');
  229. echo 'Super-column cities.Estonia.Tallinn: <pre>'.print_r($tallinn, true).'</pre><hr/>';
  230. /*
  231. // you can delete all the data in a column family using "truncate"
  232. $cassandra->truncate('user');
  233. // you may choose to drop an entire keyspace
  234. $cassandra->dropKeyspace('CassandraExample');
  235. */