Cassandra (CQL) selects statement with 'where' does not work

I have been using Cassandra the past few days. For this, I use the PHPCassa library.

When I try to use the following code, it does not work 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); 

Without returning anything, I also tried the 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); 

He gave the correct answer, displayed all the lines. Please advise me how to use "WHERE" correctly.

Keyspace Information

 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 
+6
source share
3 answers

In cassandra, you can't just query a β€œtable” as usual. You need to set up secondary indexes for each column that you might want to query.

Let's say we have a table:

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

You can directly request a key:

 SELECT * FROM User WHERE key='phpqa'; 

But to do other WHERE queries, you'll need an extra index for the columns you want in the WHERE clause.

What can you do to make your request flexible in the way you want:

  • Secondary indices as described above.
  • Use composite columns as the key. This is a good idea if you only have 2-3 columns that you want to query, but read this article that details how and when to use composite keys, and here is a link on how to implement it in phpcassa .
+8
source

Add "name" and "age" as secondary indexes:

 CREATE INDEX name_key on User( name ); CREATE INDEX age_key on User( age ); 

Then you can use your select statements.

More details here .

+4
source

you use the reserved word as the column name:

http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

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

All Articles