Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra (CQL) select statement with 'where' is not working

I am using Cassandra last few days. I am using PHPCassa library for that.

When I am trying to use the following code, Its not working correctly.

 require_once('phpcassa/connection.php');
 require_once "phpcassa/columnfamily.php";

 // Create new ConnectionPool like you normally would
 $pool = new ConnectionPool("newtest");

 // Retrieve a raw connection from the ConnectionPool
 $raw = $pool->get();

 $rows = $raw->client->execute_cql_query("SELECT * FROM User WHERE KEY='phpqa'", cassandra_Compression::NONE);

 echo "<pre>";
 print_r($rows);
 echo "<pre>";

// Return the connection to the pool so it may be used by other callers. Otherwise,
// the connection will be unavailable for use.
$pool->return_connection($raw);
unset($raw);

Its returning nothing, I have also tried following queries

$rows = $raw->client->execute_cql_query("SELECT * FROM User WHERE age='32'", cassandra_Compression::NONE);
$rows = $raw->client->execute_cql_query("SELECT * FROM User WHERE name='jack'", cassandra_Compression::NONE);

But When I tried

 $rows = $raw->client->execute_cql_query("SELECT * FROM User", cassandra_Compression::NONE);

Its given the correct answer, displayed all the rows. Please advise me, how to use 'WHERE' properly.

Keyspace Details

Strategy Class:     org.apache.cassandra.locator.SimpleStrategy
Strategy Options:   None
Replication Factor: 1

Ring

   Start Token: 6064078270600954295
   End Token: 6064078270600954295
   Endpoints: 127.0.0.1
like image 702
phpqa.in Avatar asked Mar 15 '13 10:03

phpqa.in


1 Answers

In cassandra you cant just query a 'table' as you would normally. You need to set up secondary indexes for every column you might want to query.

Say we have a table:

 key      | User |   Age 
----------+----------------
 phpqa    | Jack |   20    

You can query directly on the key:

SELECT * FROM User WHERE key='phpqa';

But to carry out other WHERE queries you'd need a secondary index on the columns you want to have available in the WHERE clause.

What can you do to make your querying flexible in the way that you desire:

  1. Secondary indexes as described above.
  2. Use composite columns as your key. Its a good idea if you only have 2-3 columns you want to query, but have a read through this article detailing how and when to use composite keys, and here is a link in how to implement it in phpcassa.
like image 134
Lyuben Todorov Avatar answered Sep 20 '22 12:09

Lyuben Todorov