【问题标题】:Cassandra (CQL) select statement with 'where' is not working带有“where”的 Cassandra (CQL) 选择语句不起作用
【发布时间】:2013-03-04 01:17:41
【问题描述】:

我最近几天在使用 Cassandra。我为此使用 PHPCassa 库。

当我尝试使用以下代码时,它无法正常工作。

 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);

它什么也没返回,我也尝试过以下查询

$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);

但是当我尝试时

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

给出正确答案,显示所有行。请告诉我,如何正确使用“WHERE”。

键空间详细信息

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

【问题讨论】:

  • 'DESCRIBE USER KEYSPACE' 的输出是什么?
  • “年龄”和“姓名”是否已编入索引?
  • 在此查询“SELECT * FROM User WHERE KEY='phpqa'”中,phpqa 是该行数据的键名。
  • 我们需要为此索引 KEY 吗?

标签: php cassandra cql phpcassa


【解决方案1】:

在 cassandra 中,您不能像往常一样只查询“表”。您需要为可能要查询的每一列设置二级索引。

假设我们有一张桌子:

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

可以直接在key上查询:

SELECT * FROM User WHERE key='phpqa';

但要执行其他 WHERE 查询,您需要对希望在 WHERE 子句中可用的列进行二级索引。

你可以做些什么来让你的查询以你想要的方式灵活:

  1. Secondary indexes 如上所述。
  2. 使用复合列作为键。如果您只有 2-3 列要查询,但阅读 this article 详细说明如何以及何时使用复合键,这是一个好主意,这里是如何在 phpcassa 中实现它的链接。

【讨论】:

  • 我们需要为此索引 KEY 吗?
  • @phpqa.in 假设你想在 WHERE 子句中有 Age 列(如SELECT * FROM user WHERE age=12 你需要索引 AGE 列。如果你想要 AGE 和 NAME 你需要索引两个列. 键列始终被索引。
  • 感谢您的帮助,在添加了年龄的二级索引后,现在它可以工作了,但是我无法做到
【解决方案2】:

添加“姓名”和“年龄”作为二级索引:

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

那么您应该可以使用您的select 语句。

阅读更多here

【讨论】:

  • 谢谢。我们需要为 KEY 添加单独的索引吗?
  • 所以这应该很顺利吧? SELECT * FROM User WHERE key='phpqa';但算命不是。
  • 不看数据很难知道。
【解决方案3】:

您正在使用保留字作为列名:

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)

【讨论】:

  • 帖子不是关于cassandra吗?不是 MySQL。
猜你喜欢
  • 2018-09-19
  • 1970-01-01
  • 2017-01-01
  • 1970-01-01
  • 2017-10-18
  • 1970-01-01
  • 2016-06-12
  • 2011-04-04
  • 1970-01-01
相关资源
最近更新 更多