Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用IN和ORDER BY创建Cassandra CQL_Cassandra_Cql - Fatal编程技术网

使用IN和ORDER BY创建Cassandra CQL

使用IN和ORDER BY创建Cassandra CQL,cassandra,cql,Cassandra,Cql,我需要一个CQL来根据一组当前用户朋友(我在中使用)从表中获取所有行,并根据创建的日期对它们进行排序 我试着用key和clustering key玩游戏,但没有任何想法 这是我的卡桑德拉表: CREATE TABLE chat.news_feed( id_news_feed uuid, id_user_sent uuid, first_name text, last_name text, security i

我需要一个CQL来根据一组当前用户朋友(我在中使用)从表中获取所有行,并根据创建的日期对它们进行排序

我试着用key和clustering key玩游戏,但没有任何想法

这是我的卡桑德拉表:

CREATE TABLE chat.news_feed(
  id_news_feed                           uuid,
  id_user_sent         uuid,
  first_name text,
  last_name text,
  security int,
  news_feed text, 
  image blob,
  image_preview text,
  image_name text,
  image_length int,
  image_resolution text,
  is_image int,
  created_date        timestamp,
  PRIMARY KEY ((id_news_feed, id_user_sent), created_date))
    WITH CLUSTERING ORDER BY (created_date DESC) AND comment = 'List of all news feed by link id';
这是我的CQL(用Java编写):

我不能运行它,因为我的CQL的WHERE部分没有密钥

是否有任何方法可以让一组具有ORDERBY的用户创建所有行(我尝试以不同的方式创建表,但还没有结果)


谢谢大家!

与这里的关系数据库不同,您可能需要对表进行非规范化。首先,不能从单个表中有效地查询所有内容。此外,Cassandra不支持本地连接。我建议把你的桌子分成几张

让我们从朋友开始:当前用户id应该是主键的一部分,朋友应该作为集群列

CREATE TABLE chat.user_friends (
  user_id uuid,
  friend_id uuid,
  first_name text,
  last_name text,
  security int,
  PRIMARY KEY ((user_id), friend_id));
现在,您可以通过以下方式查询每个特定用户的好友:

SELECT * FROM chat.user_friends WHERE user_id = 'a001-...';

接下来,让我们来处理新闻提要:在将剩余列放入该表之前,我会考虑针对该表的所需查询。新闻提要需要通过用户ID在列表中的
进行过滤,同时可以按时间进行排序。因此,我们将创建的日期时间戳作为集群键,朋友的用户id作为分区键。请注意,时间戳将按用户id排序,而不是全局排序(您可以在客户端重新排序)。真正重要的是将news\u feed\u id隐藏在主键之外。此列仍然可能包含唯一的uuid,但只要我们不想查询此表以按id获取特定的新闻提要。为此,我们无论如何都需要单独的表(数据的非规范化)或物化视图(我将不在这个答案中介绍,但对于Cassandra 3.0中引入的某些类型的非规范化来说,这是一个非常好的解决方案)

以下是更新后的表格:

CREATE TABLE chat.news_feed(      
  id_user_sent uuid,
  first_name text,
  last_name text,
  security int,
  id_news_feed uuid,
  news_feed text, 
  image blob,
  image_preview text,
  image_name text,
  image_length int,
  image_resolution text,
  is_image int,
  created_date        timestamp,
  PRIMARY KEY ((id_user_sent), created_date))
    WITH CLUSTERING ORDER BY (created_date DESC) AND comment = 'List of all news feed by link id';
一些示例数据集:

cqlsh:ks_test> select * from news_feed ;

 id_user_sent                         | created_date                    | first_name | id_news_feed                         | image | image_length | image_name | image_preview | image_resolution | is_image | last_name | news_feed | security
--------------------------------------+---------------------------------+------------+--------------------------------------+-------+--------------+------------+---------------+------------------+----------+-----------+-----------+----------
 01b9b9e8-519c-4578-b747-77c8d9c4636b | 2017-02-23 00:00:00.000000+0000 |       null | fd25699c-78f1-4aee-913a-00263912fe18 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 9bd23d16-3be3-4e27-9a47-075b92203006 | 2017-02-21 00:00:00.000000+0000 |       null | e5d394d3-b67f-4def-8f1e-df781130ea22 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 6e05257d-9278-4353-b580-711e62ade8d4 | 2017-02-25 00:00:00.000000+0000 |       null | ec34c655-7251-4af8-9718-3475cad18b29 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 6e05257d-9278-4353-b580-711e62ade8d4 | 2017-02-22 00:00:00.000000+0000 |       null | 5342bbad-0b55-4f44-a2e9-9f285d16868f |  null |         null |       null |          null |             null |     null |      null |      null |     null
 6e05257d-9278-4353-b580-711e62ade8d4 | 2017-02-20 00:00:00.000000+0000 |       null | beea0c24-f9d6-487c-a968-c9e088180e73 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 63003200-91c0-47ba-9096-6ec1e35dc7a0 | 2017-02-21 00:00:00.000000+0000 |       null | a0fba627-d6a7-463c-a00c-dd0472ad10c5 |  null |         null |       null |          null |             null |     null |      null |      null |     null
过滤后的一个:

cqlsh:ks_test> select * from news_feed where id_user_sent in (01b9b9e8-519c-4578-b747-77c8d9c4636b, 6e05257d-9278-4353-b580-711e62ade8d4) and created_date >= '2017-02-22';

 id_user_sent                         | created_date                    | first_name | id_news_feed                         | image | image_length | image_name | image_preview | image_resolution | is_image | last_name | news_feed | security
--------------------------------------+---------------------------------+------------+--------------------------------------+-------+--------------+------------+---------------+------------------+----------+-----------+-----------+----------
 01b9b9e8-519c-4578-b747-77c8d9c4636b | 2017-02-25 00:00:00.000000+0000 |       null | 26dc0952-0636-438f-8a26-6a3fef4fb808 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 01b9b9e8-519c-4578-b747-77c8d9c4636b | 2017-02-23 00:00:00.000000+0000 |       null | fd25699c-78f1-4aee-913a-00263912fe18 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 6e05257d-9278-4353-b580-711e62ade8d4 | 2017-02-25 00:00:00.000000+0000 |       null | ec34c655-7251-4af8-9718-3475cad18b29 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 6e05257d-9278-4353-b580-711e62ade8d4 | 2017-02-22 00:00:00.000000+0000 |       null | 5342bbad-0b55-4f44-a2e9-9f285d16868f |  null |         null |       null |          null |             null |     null |      null |      null |     null

另外,正如您可能注意到的,我们去掉了
ALLOW FILTERING
子句。不要在任何应用程序中使用
ALLOW FILTERING
,因为它会带来显著的性能损失。这只可用于查找分散在不同分区中的一些小数据块。

好吧,我已经有了朋友关系(因为用户可能是朋友、订阅、忽略等)和用户表(用于存储基本数据),但我喜欢您的想法,从密钥中排除id_news_feed。感谢您的回复和伟大的想法!
cqlsh:ks_test> select * from news_feed ;

 id_user_sent                         | created_date                    | first_name | id_news_feed                         | image | image_length | image_name | image_preview | image_resolution | is_image | last_name | news_feed | security
--------------------------------------+---------------------------------+------------+--------------------------------------+-------+--------------+------------+---------------+------------------+----------+-----------+-----------+----------
 01b9b9e8-519c-4578-b747-77c8d9c4636b | 2017-02-23 00:00:00.000000+0000 |       null | fd25699c-78f1-4aee-913a-00263912fe18 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 9bd23d16-3be3-4e27-9a47-075b92203006 | 2017-02-21 00:00:00.000000+0000 |       null | e5d394d3-b67f-4def-8f1e-df781130ea22 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 6e05257d-9278-4353-b580-711e62ade8d4 | 2017-02-25 00:00:00.000000+0000 |       null | ec34c655-7251-4af8-9718-3475cad18b29 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 6e05257d-9278-4353-b580-711e62ade8d4 | 2017-02-22 00:00:00.000000+0000 |       null | 5342bbad-0b55-4f44-a2e9-9f285d16868f |  null |         null |       null |          null |             null |     null |      null |      null |     null
 6e05257d-9278-4353-b580-711e62ade8d4 | 2017-02-20 00:00:00.000000+0000 |       null | beea0c24-f9d6-487c-a968-c9e088180e73 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 63003200-91c0-47ba-9096-6ec1e35dc7a0 | 2017-02-21 00:00:00.000000+0000 |       null | a0fba627-d6a7-463c-a00c-dd0472ad10c5 |  null |         null |       null |          null |             null |     null |      null |      null |     null
cqlsh:ks_test> select * from news_feed where id_user_sent in (01b9b9e8-519c-4578-b747-77c8d9c4636b, 6e05257d-9278-4353-b580-711e62ade8d4) and created_date >= '2017-02-22';

 id_user_sent                         | created_date                    | first_name | id_news_feed                         | image | image_length | image_name | image_preview | image_resolution | is_image | last_name | news_feed | security
--------------------------------------+---------------------------------+------------+--------------------------------------+-------+--------------+------------+---------------+------------------+----------+-----------+-----------+----------
 01b9b9e8-519c-4578-b747-77c8d9c4636b | 2017-02-25 00:00:00.000000+0000 |       null | 26dc0952-0636-438f-8a26-6a3fef4fb808 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 01b9b9e8-519c-4578-b747-77c8d9c4636b | 2017-02-23 00:00:00.000000+0000 |       null | fd25699c-78f1-4aee-913a-00263912fe18 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 6e05257d-9278-4353-b580-711e62ade8d4 | 2017-02-25 00:00:00.000000+0000 |       null | ec34c655-7251-4af8-9718-3475cad18b29 |  null |         null |       null |          null |             null |     null |      null |      null |     null
 6e05257d-9278-4353-b580-711e62ade8d4 | 2017-02-22 00:00:00.000000+0000 |       null | 5342bbad-0b55-4f44-a2e9-9f285d16868f |  null |         null |       null |          null |             null |     null |      null |      null |     null