通过Java中的主键列表查找cassandra [英] cassandra lookup by list of primary keys in java

查看:84
本文介绍了通过Java中的主键列表查找cassandra的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一项功能,要求通过主键列表查找Cassandra。

I am implementing a feature which requires looking up Cassandra by a list of primary keys.

下面是一个示例数据,其中id是主键

Below is an example data where id is primary key

mytable
id          column1
1           423
2           542
3           678
4           45534
5           435634
6           2435
7           678
8           4564
9           546

我大多数人通过ID查询查询,但在某些特殊情况下,我想获取ID列表的数据。
我当前正在做的事情如下:

Most of my queries a lookup by id, but for some special cases I would like to get data for a list of ids. The way I am currently doing is a follows:


public Object fetchFromCassandraForId(int id);

int ids[] = {1, 3, 5, 7, 9};
List<Object> results;
for(int id: ids) {
  results.add(fetchFromCassandraForId(id));
}

这导致向cassandra发出多个网络调用,是否有可能以某种方式进行批处理,因此我想知道cassandra是否支持通过ID列表进行快速查找

This results in issuing multiple network call to cassandra, Is it possible to batch this somehow, therefore i would like to know if cassandra supports fast lookup by list of ids

select coulmn1 from mytable where id in (1, 3, 5, 7, 9);


任何帮助或指针将不胜感激?

? Any help or pointers would be appreciated?

推荐答案

如果 id 是完整的主键,然后Cassandra支持这一点,尽管从性能的角度上不建议这样做:

If the id is the full primary key, then Cassandra supports this, although it's not recommended from performance point of view:


  • 请求发送到协调节点
  • request is sent to coordinator node
  • coordinator node finds a replica for each of the id, and send individual request to them
  • wait for results from every node, collect them to result set & send back

结果:


  • 所有子查询都需要等待对于最慢的副本

  • 您还有一个从协调器到副本的网络希望

  • 您需要对协调器节点施加更大的压力,因为它需要保持导致内存中出现

如果您对每个 id 进行大量并行,异步请求来自应用程序的值,则您:

If you do a lot of parallel, asynchronous requests for each of the id values from application, then you:


  • 避免额外的跃点-如果您使用带有令牌感知负载平衡的准备好的语句,那么查询将直接发送到副本

  • 您可能会在获取结果时开始处理结果,而不是等待所有事情

因此发送并行异步请求可能比使用 IN ...

So sending parallel asynchronous requests could be faster than sending one request with IN...

这篇关于通过Java中的主键列表查找cassandra的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆