PHP and Cassandra

33,572 views

Published on

A very quick introduction to Cassandra database plus accessing it from PHP. My slides from my 5 minute "lightening" talk at PHP London on 1/7/2010.

Published in: Technology
3 Comments
9 Likes
Statistics
Notes
No Downloads
Views
Total views
33,572
On SlideShare
0
From Embeds
0
Number of Embeds
3,601
Actions
Shares
0
Downloads
345
Comments
3
Likes
9
Embeds 0
No embeds

No notes for slide

PHP and Cassandra

  1. 1. A (very) quick introduction to using <br />PHP and Cassandra<br />@davegardnerisme<br />
  2. 2. What is Cassandra?<br /><ul><li> Highly scalable distributed database
  3. 3. Brings together Dynamo and Bigtable
  4. 4. Schema-less
  5. 5. #nosql!</li></li></ul><li>Why Cassandra?<br /><ul><li> Horizontally scalable – RW increase linearly
  6. 6. Fault tolerant – no single point of failure
  7. 7. Hadoop integration for scalable Map Reduce
  8. 8. Good support via community (plus professional support)</li></li></ul><li>Data model<br /><ul><li> Store your data in the way you want to query it – denormalise
  9. 9. Some people say Cassandra is a 4 or 5 level hash (1)</li></ul>[KeySpace][ColumnFamily][Key][Column][KeySpace][ColumnFamily][Key][SuperColumn][SubColumn]<br />
  10. 10. Configuring – storage-conf.xml<br /><ColumnFamilyName="Standard1"<br />CompareWith="BytesType"/><br /><ColumnFamilyName="Standard2" <br />CompareWith="UTF8Type"/><br /><ColumnFamilyName="Super1"<br />ColumnType="Super"<br />CompareWith="BytesType"<br />CompareSubcolumnsWith="BytesType" /><br />Good example within an easily understandableproblem domain: Twissandra! (2)<br />
  11. 11. PHP<br /><ul><li> Access the core API via Thrift (3)
  12. 12. Higher level libraries do exist; PHPCassa (4) and Pandra(5)
  13. 13. Compile Thrift which will generate the PHP libraries for you (6)
  14. 14. Native PHP Thrift extension recommended</li></li></ul><li>// SOME CODE!<br />// connect<br />$socket = new TSocket('192.168.1.206', 9160);<br />$transport = new TBufferedTransport($socket, 1024, 1024);<br />$protocol = new TBinaryProtocolAccelerated($transport);<br />$client = new CassandraClient($protocol);<br />$transport->open();<br />// fetch single column from single row<br />$columnPath= new cassandra_ColumnPath();<br />$columnPath->column_family= 'UrlsVisited';<br />$columnPath->column = $url;<br />$userUrl= $client->get(<br />$keyspace, // a bit like the database<br />$userId, // the row key<br />$columnPath,//identifies columns we want<br />$consistencyLevelOne);<br />
  15. 15. // Inserting multiple columns for a single row<br />// (bit like populating one row of MySQL)<br />$key ='UniqueRowKey';<br />$columnFamily='ResponsePersonality';<br />$mutationMap=array(<br />$key=>array($columnFamily=>array())<br />);<br />// add our first column:<br />$column =new cassandra_Column(array(<br />'name'=>'howMuchWork',<br />'value'=> 'quiteABit',<br />'timestamp'=> time()<br />));<br />$columnOrSupercolumn=new cassandra_ColumnOrSuperColumn(<br />array('column'=>$column));<br />$mutationMap[$key][$columnFamily][] = new cassandra_Mutation(array('column_or_supercolumn'=>$columnOrSupercolumn));<br />
  16. 16. // add our second column!:<br />$column =new cassandra_Column(array(<br />'name'=>'nextColumnName',<br />'value'=> 'wow',<br />'timestamp'=> time()<br />));<br />$columnOrSupercolumn=new cassandra_ColumnOrSuperColumn(<br />array('column'=>$column));<br />$mutationMap[$key][$columnFamily][] = new cassandra_Mutation(array('column_or_supercolumn'=>$columnOrSupercolumn));<br />// repeat with other columns ...<br />// finally we call batch_mutate to add!<br />$client->batch_mutate(<br />$keyspace,<br />$mutationMap,<br />$consistencyLevelZero);<br />
  17. 17. Finally… Hadoop integration<br /><ul><li> Support for creating Hadoop jobs in Java (7)
  18. 18. Support for PIG (higher level language) (7)
  19. 19. Cassandra 0.7 will include output support (8)
  20. 20. No support for Hive yet! (9)(SQL-like syntax for creating Map Reduce jobs)</li></li></ul><li>References/links<br />Cassandra is four or five level hash:https://www.cloudkick.com/blog/2010/mar/02/4_months_with_cassandra/<br />Twissandra:http://www.rackspacecloud.com/blog/2010/05/12/cassandra-by-example/<br />Thrift API:http://wiki.apache.org/cassandra/API<br />PHPCassa:http://github.com/hoan/phpcassa<br />Pandra:http://github.com/mjpearson/Pandra<br />
  21. 21. References/links<br />Using Cassandra with PHP (installing Thrift)https://wiki.fourkitchens.com/display/PF/Using+Cassandra+with+PHP<br />Hadoop Support in Cassandra:http://wiki.apache.org/cassandra/HadoopSupport<br />Output support in Cassandra:https://issues.apache.org/jira/browse/CASSANDRA-1101<br />Hive support (feature request!):https://issues.apache.org/jira/browse/CASSANDRA-913<br />

×