本文介绍了如何设置cassandra读写一致性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到此文档。我知道cqlsh中有一个一致性命令,但是读写一致性之间没有区别。如何设置读写的不同一致性级别?

I can not find the documentation for this. I know there is a consistency command in cqlsh, but there is no distinction between read and write consistency. How would I set different consistency levels for read and write?

此外,还提到了默认一致性级别。该默认设置在哪里?

Furthermore, there is a mention of a "default" consistency level. Where is that default set? and is it for read or write?

推荐答案

我将如何为读写设置不同的一致性级别?

How would I set different consistency levels for read and write?

如果您只想更改当前会话的一致性级别,请使用 CONSISTENCY

If you just want to change consistency level for your current session, use CONSISTENCY.

如果要以编程方式更改一致性级别,请使用cassandra驱动程序作为客户端语言。

If you want to change the consistency level programamtically, use the cassandra driver for your client language.

由于可以为每个语句设置一致性级别,因此可以在每个语句上设置一致性级别,也可以使用PreparedStatements。如果您使用的是Java驱动程序,则可以为所有读写操作配置全局一致性级别(但不能 和 only 写入)。

Since the Consistency Level can be set per-statement, you can either set it on every statement, or use PreparedStatements. If you're using the Java driver, you can configure a global Consistency level for BOTH reads and writes (but not only reads and only writes).

默认的[一致性级别]设置在哪里?

如果要为读写设置不同的一致性级别,则必须在每个陈述的基础。使用 QueryOptions()。setConsistencyLevel 设置全局一致性级别(对于Java驱动程序)。它同时用于读写。

If you want to set up a different consistency level for reads than writes, you'll have to do it on a per-statement basis. Use QueryOptions().setConsistencyLevel to set a global consistency level (for the Java driver). It is for BOTH reads and writes.

要设置当前会话的一致性级别,请使用从cassandra外壳程序(CQLSH)命令

To set the consistency level for your current session, use the CONSISTENCY commandfrom the cassandra shell (CQLSH).

例如:

设置一致性以强制大多数节点做出响应:

CONSISTENCY QUORUM

Set CONSISTENCY to force the majority of the nodes to respond:CONSISTENCY QUORUM

要查看当前的一致性级别,只需运行 CONSISTENCY; 外壳:

To see your current consistency level, just run CONSISTENCY; from the shell:


ty @ cqlsh>一致性;
当前一致性级别为ONE。

对于编程客户端应用程序,请使用适当的驱动程序设置一致性级别。

For programming client applications, set the consistency level using an appropriate driver.

例如,要使用Java驱动程序设置每个插入一致性级别,请调用 QueryBuilder.insertInto setConsistencyLevel

To set a per-insert consistency level using the Java driver, for example, call QueryBuilder.insertInto with setConsistencyLevel.

例如:

PreparedStatement pstmt = session.prepare(
INSERT INTO product(sku,description)VALUES(?,?));
pstmt.setConsistencyLevel(ConsistencyLevel.QUORUM);

要使用Java驱动程序为读写设置 global 一致性级别,请执行以下操作像这样:

To set a global consistency level for reads AND writes using the Java driver, do something like:

QueryOptions qo = new QueryOptions()。setConsistencyLevel(ConsistencyLevel.ALL);


  • 由于Java驱动程序仅执行CQL语句,因此可以读取或写入CQL语句Cassandra,不可能为仅读取或仅写入全局配置一致性级别。为此,由于可以为每个语句设置一致性级别,因此可以在每个语句上设置一致性级别,也可以使用PreparedStatements。 中的更多信息。文件。

  • Since the Java driver only executes CQL statements, which can be either reads or writes to Cassandra, it is not possible to globally configure the Consistency Level for only reads or only writes. To do so, since the Consistency Level can be set per-statement, you can either set it on every statement, or use PreparedStatements. More info in the Queries and Results document.

更多有关读/写一致性级别的资源





    • 此处也说明了不同的一致性级别^

    其他有关一致性的资源:
    您可能还需要研究副本的放置策略和复制因子(这是其他形式的一致性)。
    我提供了以下链接,以作很好的衡量:

    Other resources on consistency:You may also want to look into your replica placement strategy and replication factor (which are other forms of consistency).I included links below for good measure:





    • Background on data replication in Cassandra
    • How to update replica strategy and replication factor using CREATE KEYSPACE
    • How consistency affects performance

    这篇关于如何设置cassandra读写一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:40