使用Datastax Cassandra本机Java客户端管理不同一致性级别的最佳实践 [英] Best practice for managing different consistency levels using the Datastax Cassandra native java client

查看:112
本文介绍了使用Datastax Cassandra本机Java客户端管理不同一致性级别的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用CQL3,Cassandra一致性级别现在设置在会话级别。本机Java客户端的Datastax文档声明:

Using CQL3, the Cassandra consistency level is now set at the session level. The Datastax documentation for the native Java client states:


会话实例是线程安全的,通常每个应用程序只需要一个实例

Session instances are thread-safe and usually a single instance is all you need per application

但我很难看到单个Session实例如何处理多个一致性级别(例如使用QUORUM写入并使用ONE读取)。我看到了各处潜在的竞争条件。

But I struggle to see how a single Session instance can handle multiple consistency levels (for example writes with QUORUM and reads with ONE). I see potential race conditions all over the place.

一个显而易见的解决方案是为读写创建单独的会话,每个会话都设置了适当的一致性级别。但这并没有完全解决问题。如果一个类修改了两个会话之一的一致性级别怎么办?然后,Session实例的所有后续用户都会在不知不觉中使用新的CL。

An obvious solution would be to create separate sessions for reads and writes, each with the appropriate consistency level set. But this doesn't fully solve the problem. What if a class modified the consistency level for one of the two sessions? All subsequent users of the Session instance would then, unknowingly, be using the new CL.

所以,据我所知,最安全的选择是创建一个新的每次需要访问Cassandra时的会话实例,在创建时明确设置CL。

So, as far as I can see, the safest option is to create a new Session instance each time Cassandra needs to be accessed, with the CL explicitly set upon creation.

我不清楚这种方法是否会导致性能损失。例如, session = cluster.connect() session.execute(CONSISTENCY [cl])涉及到服务器的旅行?

What is not clear to me is whether this approach would entail a performance penalty. For example, would either session = cluster.connect() or session.execute("CONSISTENCY [cl]") involve a trip to the server?

我在这里遗漏了什么吗?有没有人有相关经验可以分享?谢谢。

Am I missing something here? Has anybody got any relevant experience to share? Thanks.

更新:
我看到 com.datastax.driver.core.Query 有一个方法用于设置一致性级别。所以也许最简单的选择是坚持使用单个Session实例并为每个查询设置CL。

UPDATE: I see com.datastax.driver.core.Query has a method for setting the consistency level. So perhaps the simplest option is stick with a single Session instance and set the CL for each query.

推荐答案


所以也许最简单的选择就是坚持使用单个Session实例并为每个查询设置CL。

So perhaps the simplest option is stick with a single Session instance and set the CL for each query.

不仅如此最简单的选项,它是 选项。

Not only is that the simplest option, it is the option.

在驱动程序的1.0.2版中,没有办法在其他地方设置一致性级别每个查询。没有每会话CL。从Java驱动程序2.0版开始,可以通过 Cluster.Builder.withQueryOptions() method

In version 1.0.2 of the driver there was no way to set the consistency level anywhere else that per-query. There was no per-Session CL. Since version 2.0 of the Java driver, the global default consistency level can be set for the Cluster through the Cluster.Builder.withQueryOptions() method.

尽管如此,我个人认为CL是每个查询的重要参数。所以我不会觉得为每个查询设置它是疯狂的,即使是为了文档而保持默认值为ONE(即说我确实知道这个查询的相关CL是什么,它是一个)。

Though, for what is worth, my personal opinion is that the CL is an important parameter of each query. So I wouldn't find it crazy to set it for each query even when it's to keep the default of ONE for documentation sake (i.e. to say "I do have though about what is the relevant CL for this query and it's ONE").

这篇关于使用Datastax Cassandra本机Java客户端管理不同一致性级别的最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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