如何设置cassandra读写一致性

7z5jn7bk  于 2022-12-12  发布在  Cassandra
关注(0)|答案(5)|浏览(221)

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?

niknxzdl

niknxzdl1#

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

If you just want to change consistency level for your current session, use CONSISTENCY .
If you want to change the consistency level programamtically, use the cassandra driver for your client language.
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).

Where is that default [consistency level] set? and is it for read or write?

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.

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

For example:
Set CONSISTENCY to force the majority of the nodes to respond: CONSISTENCY QUORUM
To see your current consistency level, just run CONSISTENCY; from the shell:
ty@cqlsh> consistency; Current consistency level is ONE.

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

To set a per-insert consistency level using the Java driver, for example, call QueryBuilder.insertInto with setConsistencyLevel .
For example:
PreparedStatement pstmt = session.prepare( "INSERT INTO product (sku, description) VALUES (?, ?)"); pstmt.setConsistencyLevel(ConsistencyLevel.QUORUM);
To set a global consistency level for reads AND writes using the Java driver, do something like:
QueryOptions qo = new QueryOptions().setConsistencyLevel(ConsistencyLevel.ALL);

zpqajqem

zpqajqem2#

By default, the Consistency Level is ONE for all R/W Operations.
Setting CL is done on a per query (read or upsert) basis by adding the CONSISTENCY XXXX syntax on the query as seen here:
https://cassandra.apache.org/doc/latest/cql/dml.html#insert
and
https://cassandra.apache.org/doc/4.0/tools/cqlsh.html

jdg4fx2g

jdg4fx2g3#

The consistency level is not set anymore with a CQL USING clause. See CASSANDRA-4734 for an explanation.
Instead, with CQLSH, you should use the CONSISTENCY command: see here for details.
Note that the default consistency level changed in all DataStax drivers from ONE to LOCAL_ONE : see the python driver documentation for details - however, in spite of using the python driver behind the scenes, CQLSH still has a default consistency of ONE ).

x6492ojm

x6492ojm4#

不再使用PreparedStatement设置一致性级别。该API已更改。现在必须使用BatchStatementBuilder
下面是可以正常工作的示例代码。

BatchStatementBuilder builder = BatchStatement.builder(DefaultBatchType.UNLOGGED);
        builder.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM);
        PreparedStatement preparedStatement = session.prepare("INSERT INTO \"ScottKeySpace16\".\"Movie\" (title, year, plot) values (:k0, :k1, :k2)");
        builder.addStatement(preparedStatement.boundStatementBuilder()
            .setString("k0", "Movie44")
            .setInt("k1", 2022)
            .setString("k2", "This is a very funny movie")
            .build());

        BatchStatement batchStatement = builder.build();
        session.execute(batchStatement);
iqjalb3h

iqjalb3h5#

1.在cassandr.conf文件中设置了默认一致性。它用于读写。
1.通过在查询中添加USING选项,可以按查询基础设置一致性

相关问题