how to set cassandra read and write consistency

折月煮酒 提交于 2019-12-06 02:10:45
bechbd

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

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);

  • 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.

More resources on read/write consistency levels

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:

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).

  1. Default consistency is set in cassandra.conf file . It is for read and write .

  2. You can set consistency per query basis adding USING option in query .

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!