【问题标题】:Paging Resultsets in Cassandra with compound primary keys - Missing out on rows使用复合主键在 Cassandra 中分页结果集 - 缺少行
【发布时间】:2014-06-30 18:52:41
【问题描述】:

所以,我最初的问题是使用 token() 函数来翻阅 Cassandra 1.2.9 中的大型数据集,如下所述:Paging large resultsets in Cassandra with CQL3 with varchar keys

接受的答案使选择与令牌和块大小一起工作,但另一个问题出现了。

我的表格在 cqlsh 中是这样的:

key           | column1               | value
---------------+-----------------------+-------
  85.166.4.140 |       county_finnmark |     4
  85.166.4.140 |       county_id_20020 |     4
  85.166.4.140 |     municipality_alta |     2
  85.166.4.140 | municipality_id_20441 |     2
 93.89.124.241 |        county_hedmark |    24
 93.89.124.241 |       county_id_20005 |    24

主键是 key 和 column1 的组合。在 CLI 中,相同的数据如下所示:

get ip['85.166.4.140'];
=> (counter=county_finnmark, value=4)
=> (counter=county_id_20020, value=4)
=> (counter=municipality_alta, value=2)
=> (counter=municipality_id_20441, value=2)
Returned 4 results.

问题

当使用限制为 100 的 cql 时,返回的结果可能会停在一条记录的中间,如下所示:

key           | column1               | value
---------------+-----------------------+-------
  85.166.4.140 |       county_finnmark |     4
  85.166.4.140 |       county_id_20020 |     4

将这些留给“行”(列):

  85.166.4.140 |     municipality_alta |     2
  85.166.4.140 | municipality_id_20441 |     2

现在,当我对下一页使用 token() 函数时,会跳过这两行:

select * from ip where token(key) > token('85.166.4.140') limit 10;

结果:

key           | column1                | value
---------------+------------------------+-------
 93.89.124.241 |         county_hedmark |    24
 93.89.124.241 |        county_id_20005 |    24
 95.169.53.204 |        county_id_20006 |     2
 95.169.53.204 |         county_oppland |     2

因此,前一个 IP 地址的最后两个结果没有任何痕迹。

问题

如何在不跳过 cql 行的情况下使用 token() 进行分页?比如:

select * from ip where token(key) > token(key:column1) limit 10;

【问题讨论】:

    标签: cassandra cql3


    【解决方案1】:

    好的,所以我使用这篇文章中的信息来制定解决方案: http://www.datastax.com/dev/blog/cql3-table-support-in-hadoop-pig-and-hive (“CQL3 分页”部分)。

    首先,我执行这个cql:

    select * from ip limit 5000;
    

    从结果集中的最后一行,我得到键(即“85.166.4.140”)和 column1 中的值(即“county_id_20020”)。

    然后我创建一个准备好的语句来评估

    select * from ip where token(key) = token('85.166.4.140') and column1 > 'county_id_20020' ALLOW FILTERING;
    

    (我猜它在不使用 token() 函数的情况下也可以工作,因为现在检查 equal:)

    select * from ip where key = '85.166.4.140' and column1 > 'county_id_20020' ALLOW FILTERING;
    

    结果集现在包含此 IP 的剩余 X 行(列)。然后该方法返回所有行,并且对该方法的下一次调用包括最后使用的键('85.166.4.140')。使用这个键,我可以执行以下选择:

    select * from ip where token(key) > token('85.166.4.140') limit 5000;
    

    这给了我接下来的 5000 行,从(包括)第一个 IP“85.166.4.140”之后。

    现在,分页中没有丢失任何列。

    更新

    Cassandra 2.0 引入了由客户端处理的自动分页。 更多信息在这里:http://www.datastax.com/dev/blog/client-side-improvements-in-cassandra-2-0

    (注意 setFetchSize 是可选的,不是分页工作所必需的)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-29
      • 2015-05-12
      • 2015-03-24
      • 2019-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多