Posts

Showing posts with the label cassandra

Cassandra Composite Types - A Overview [with CQL & Cassandra-Cli examples]

Just felt like sharing what is composite type in cassandra and How can we make use of it After so many discussions in Stackoverflow and PHPCassa forums, I hope I have got a clear picture over the topic What are composite types? Composite types are like structures in C or C++ For ex: The way we define a linked list [basic] struct LLNode { int data; char name[20]; struct LLNode *next; } Which means every member of this struct will have data of type int, name of type char array and a next pointer of type LLnode The struct is a composite of basic datatypes[not exactly in this case] Also you can't initialize value to any of these attributes when you define a struct The same way Cassandra Composite Type is a dataType derived from existing basic supported dataTypes . Why do we need this? Cassandra initially had and still has the concept of super columns. The basic use of them is maintaining an inverted index . Consider a data model in cassandra ColumnFamily: UserMast...

Cassandra PHPCassa & Composite Types

This post is updated inorder to support phpcassa 1.0.a.1 Cassandra Composite Type using PHPCassa phpcassa 1.0.a.1 uses namespaces in PHP which is supported in PHP 5 >= 5.3.0 Make sure you have the relavant package. The script mentioned below is the copy of PHPCassa Composite Example I will explain it step by step (1) Creating Keyspace using PHPCassa         Name => "Keyspace1"         Replication Factor => 1         Placement Strategy => Simple Strategy (2) Creating Column Family with Composite Keys using PHPCassa         Name => "Composites"         Column Comparator => CompositeType of LongType, AsciiType (Ex: 1:example)         Row Key Validation => CompositeType of AsciiType, LongType (Ex: example:1)     ...

CQL(Cassandra Query Language) Reference

Hi, I think now I'm eligible to publish a blog post regd CQL. I wish this blog to be a tutorial rather a Syntax provider for various CQL queries Points to remember 1. CQL doesn't support Composite Types[likely to chage] 2. CQL doesn't support Super columns 3. Counter values are returned in native format [atleast in php/using cqlsh] Why should I prefer CQL? 1. Readability 2. Ease of use 3. SQL like 4. Stable Support: PHP => My Posts using PHPCassa Python => Refer Here and Download it here Java => JDBC Driver and Example is here Ruby => This might help Creating a Keyspace: cqlsh> CREATE KEYSPACE sample WITH strategy_class = 'org.apache.cassandra.locator.SimpleStrategy' ... AND strategy_options:replication_factor = 2; Note: : => Option Specifier Use a Keyspace: cqlsh> USE sample; Note: Don't forget to USE before use 'Sample' and 'sample' are different. Create Column Family: cqlsh...

Cassandra CQL PHP Part 2

I have explained the way to execute CQL Queries from PHP via PHPCassa in my previous blogpost here  and tutorial on cql queries here It was difficult for me to remember the syntax of update and select CQL queries for Cassandra. Hence I wrote wrappers for them which are listed below Update Counter Query: /* * $counterMap => Associative array of CounterColumn names and corresponding value for given $key * Ex: UPDATE TestCounterFamily SET 'counter'='counter'+1, 'counter2'='counter2'+3 WHERE KEY='test' */ function generateUpdateCounterQuery($columnFamily, $counterMap, $key) { $colArgs = ""; foreach($counterMap as $counter => $value) { $colArgs=$colArgs."'".$counter."'='".$counter."'+".$value.", "; } $colArgs = rtrim($colArgs,", "); return "UPDATE ".$columnFamily." SET ".$colArgs." WHERE KEY='".$key."...

Cassandra CQL PHP

After lots of googling I figured out how to access counter columns value via PHPCassa + CQL. Though it might not be efficient, this is all I was able to figure out after a long struggle :) First Connecting to and Selection from Cassandra Instance via PHP using PHPCassa $pool = new ConnectionPool("Stats",$servers); $raw = $pool->get(); $rows = $raw->client->execute_cql_query("SELECT 'a' FROM Impressions WHERE KEY='xxx'", cassandra_Compression::NONE); To Return Connection $pool->return_connection($this->raw); To Update Counter value via PHPCassa $pool = new ConnectionPool("Stats",$servers); $raw = $pool->get(); $rows = $raw->client->execute_cql_query("UPDATE TestCounter SET 'counter'='counter'+1 WHERE KEY='xxx'", cassandra_Compression::NONE); You can find a wrapper here So, once you are done with fetch successfully. This is the result you may get object(cassandra_CqlResult)#...