Merge branch 'doc_in_tree' into trunk

* doc_in_tree:
  Modify build file to include html doc in artifacts
  Add 'report bug' section
  Fix minor errors in CQL doc
  Finish fixing the CQL doc
  Don't track auto-generated file
  Reorganize document
  Automatically generate docs for cassandra.yaml
  Fix CQL doc (incomplete)
  Add Metrics/Monitoring docs
  Add Change Data Capture documentation
  Docs for Memtable and SSTable architecture
  Add doc on compaction
  Add initial version of security section
  Fill in Replication, Tuneable Consistency sections
  Add docs for cqlsh
  Add snitch and range movements section on Operations
  Add initial in-tree documentation (very incomplete so far)
This commit is contained in:
Sylvain Lebresne 2016-06-27 20:33:21 +02:00
commit c7b9401bf8
59 changed files with 8816 additions and 85 deletions

3
.gitignore vendored
View File

@ -72,3 +72,6 @@ lib/jsr223/jython/cachedir
lib/jsr223/scala/*.jar
/.ant-targets-build.xml
# Generated files from the documentation
doc/source/configuration/cassandra_config_file.rst

View File

@ -67,6 +67,8 @@
<property name="test.microbench.src" value="${test.dir}/microbench"/>
<property name="dist.dir" value="${build.dir}/dist"/>
<property name="tmp.dir" value="${java.io.tmpdir}"/>
<property name="doc.dir" value="${basedir}/doc"/>
<property name="source.version" value="1.8"/>
<property name="target.version" value="1.8"/>
@ -249,6 +251,17 @@
</wikitext-to-html>
</target>
<target name="gen-doc" depends="maven-ant-tasks-init" description="Generate documentation">
<exec executable="make" osfamily="unix" dir="${doc.dir}">
<arg value="html"/>
</exec>
<exec executable="cmd" osfamily="dos" dir="${doc.dir}">
<arg value="/c"/>
<arg value="make.bat"/>
<arg value="html"/>
</exec>
</target>
<!--
Generates Java sources for tokenization support from jflex
grammar files
@ -1009,7 +1022,7 @@
</target>
<!-- creates release tarballs -->
<target name="artifacts" depends="jar,javadoc"
<target name="artifacts" depends="jar,javadoc,gen-doc"
description="Create Cassandra release artifacts">
<mkdir dir="${dist.dir}"/>
<!-- fix the control linefeed so that builds on windows works on linux -->
@ -1029,9 +1042,15 @@
</copy>
<copy todir="${dist.dir}/doc">
<fileset dir="doc">
<exclude name="cql3/CQL.textile"/>
<include name="cql3/CQL.html" />
<include name="cql3/CQL.css" />
<include name="SASI.md" />
</fileset>
</copy>
<copy todir="${dist.dir}/doc/html">
<fileset dir="doc" />
<globmapper from="build/html/*" to="*"/>
</copy>
<copy todir="${dist.dir}/bin">
<fileset dir="bin"/>
</copy>

View File

@ -35,20 +35,22 @@ num_tokens: 256
# Only supported with the Murmur3Partitioner.
# allocate_tokens_for_keyspace: KEYSPACE
# initial_token allows you to specify tokens manually. While you can use # it with
# initial_token allows you to specify tokens manually. While you can use it with
# vnodes (num_tokens > 1, above) -- in which case you should provide a
# comma-separated list -- it's primarily used when adding nodes # to legacy clusters
# comma-separated list -- it's primarily used when adding nodes to legacy clusters
# that do not have vnodes enabled.
# initial_token:
# See http://wiki.apache.org/cassandra/HintedHandoff
# May either be "true" or "false" to enable globally
hinted_handoff_enabled: true
# When hinted_handoff_enabled is true, a black list of data centers that will not
# perform hinted handoff
#hinted_handoff_disabled_datacenters:
# hinted_handoff_disabled_datacenters:
# - DC1
# - DC2
# this defines the maximum amount of time a dead host will have hints
# generated. After it has been dead this long, new hints for it will not be
# created until it has been seen alive and gone down again.
@ -204,26 +206,44 @@ cdc_enabled: false
# $CASSANDRA_HOME/data/cdc_raw.
# cdc_raw_directory: /var/lib/cassandra/cdc_raw
# policy for data disk failures:
# die: shut down gossip and client transports and kill the JVM for any fs errors or
# single-sstable errors, so the node can be replaced.
# stop_paranoid: shut down gossip and client transports even for single-sstable errors,
# kill the JVM for errors during startup.
# stop: shut down gossip and client transports, leaving the node effectively dead, but
# can still be inspected via JMX, kill the JVM for errors during startup.
# best_effort: stop using the failed disk and respond to requests based on
# remaining available sstables. This means you WILL see obsolete
# data at CL.ONE!
# ignore: ignore fatal errors and let requests fail, as in pre-1.2 Cassandra
# Policy for data disk failures:
#
# die
# shut down gossip and client transports and kill the JVM for any fs errors or
# single-sstable errors, so the node can be replaced.
#
# stop_paranoid
# shut down gossip and client transports even for single-sstable errors,
# kill the JVM for errors during startup.
#
# stop
# shut down gossip and client transports, leaving the node effectively dead, but
# can still be inspected via JMX, kill the JVM for errors during startup.
#
# best_effort
# stop using the failed disk and respond to requests based on
# remaining available sstables. This means you WILL see obsolete
# data at CL.ONE!
#
# ignore
# ignore fatal errors and let requests fail, as in pre-1.2 Cassandra
disk_failure_policy: stop
# policy for commit disk failures:
# die: shut down gossip and Thrift and kill the JVM, so the node can be replaced.
# stop: shut down gossip and Thrift, leaving the node effectively dead, but
# can still be inspected via JMX.
# stop_commit: shutdown the commit log, letting writes collect but
# continuing to service reads, as in pre-2.0.5 Cassandra
# ignore: ignore fatal errors and let the batches fail
# Policy for commit disk failures:
#
# die
# shut down gossip and Thrift and kill the JVM, so the node can be replaced.
#
# stop
# shut down gossip and Thrift, leaving the node effectively dead, but
# can still be inspected via JMX.
#
# stop_commit
# shutdown the commit log, letting writes collect but
# continuing to service reads, as in pre-2.0.5 Cassandra
#
# ignore
# ignore fatal errors and let the batches fail
commit_failure_policy: stop
# Maximum size of the native protocol prepared statement cache
@ -283,11 +303,14 @@ key_cache_save_period: 14400
# Disabled by default, meaning all keys are going to be saved
# key_cache_keys_to_save: 100
# Row cache implementation class name.
# Available implementations:
# org.apache.cassandra.cache.OHCProvider Fully off-heap row cache implementation (default).
# org.apache.cassandra.cache.SerializingCacheProvider This is the row cache implementation availabile
# in previous releases of Cassandra.
# Row cache implementation class name. Available implementations:
#
# org.apache.cassandra.cache.OHCProvider
# Fully off-heap row cache implementation (default).
#
# org.apache.cassandra.cache.SerializingCacheProvider
# This is the row cache implementation availabile
# in previous releases of Cassandra.
# row_cache_class_name: org.apache.cassandra.cache.OHCProvider
# Maximum size of the row cache in memory.
@ -382,7 +405,7 @@ commitlog_segment_size_in_mb: 32
# Compression to apply to the commit log. If omitted, the commit log
# will be written uncompressed. LZ4, Snappy, and Deflate compressors
# are supported.
#commitlog_compression:
# commitlog_compression:
# - class_name: LZ4Compressor
# parameters:
# -
@ -459,9 +482,15 @@ concurrent_materialized_view_writes: 32
# Specify the way Cassandra allocates and manages memtable memory.
# Options are:
# heap_buffers: on heap nio buffers
# offheap_buffers: off heap (direct) nio buffers
# offheap_objects: off heap objects
#
# heap_buffers
# on heap nio buffers
#
# offheap_buffers
# off heap (direct) nio buffers
#
# offheap_objects
# off heap objects
memtable_allocation_type: heap_buffers
# Total space to use for commit logs on disk.
@ -534,8 +563,7 @@ ssl_storage_port: 7001
# Address or interface to bind to and tell other Cassandra nodes to connect to.
# You _must_ change this if you want multiple nodes to be able to communicate!
#
# Set listen_address OR listen_interface, not both. Interfaces must correspond
# to a single address, IP aliasing is not supported.
# Set listen_address OR listen_interface, not both.
#
# Leaving it blank leaves it up to InetAddress.getLocalHost(). This
# will always do the Right Thing _if_ the node is properly configured
@ -544,12 +572,16 @@ ssl_storage_port: 7001
#
# Setting listen_address to 0.0.0.0 is always wrong.
#
listen_address: localhost
# Set listen_address OR listen_interface, not both. Interfaces must correspond
# to a single address, IP aliasing is not supported.
# listen_interface: eth0
# If you choose to specify the interface by name and the interface has an ipv4 and an ipv6 address
# you can specify which should be chosen using listen_interface_prefer_ipv6. If false the first ipv4
# address will be used. If true the first ipv6 address will be used. Defaults to false preferring
# ipv4. If there is only one address it will be selected regardless of ipv4/ipv6.
listen_address: localhost
# listen_interface: eth0
# listen_interface_prefer_ipv6: false
# Address to broadcast to other Cassandra nodes
@ -608,8 +640,7 @@ start_rpc: false
# The address or interface to bind the Thrift RPC service and native transport
# server to.
#
# Set rpc_address OR rpc_interface, not both. Interfaces must correspond
# to a single address, IP aliasing is not supported.
# Set rpc_address OR rpc_interface, not both.
#
# Leaving rpc_address blank has the same effect as on listen_address
# (i.e. it will be based on the configured hostname of the node).
@ -618,13 +649,16 @@ start_rpc: false
# set broadcast_rpc_address to a value other than 0.0.0.0.
#
# For security reasons, you should not expose this port to the internet. Firewall it if needed.
#
rpc_address: localhost
# Set rpc_address OR rpc_interface, not both. Interfaces must correspond
# to a single address, IP aliasing is not supported.
# rpc_interface: eth1
# If you choose to specify the interface by name and the interface has an ipv4 and an ipv6 address
# you can specify which should be chosen using rpc_interface_prefer_ipv6. If false the first ipv4
# address will be used. If true the first ipv6 address will be used. Defaults to false preferring
# ipv4. If there is only one address it will be selected regardless of ipv4/ipv6.
rpc_address: localhost
# rpc_interface: eth1
# rpc_interface_prefer_ipv6: false
# port for Thrift to listen for clients on
@ -641,16 +675,18 @@ rpc_keepalive: true
# Cassandra provides two out-of-the-box options for the RPC Server:
#
# sync -> One thread per thrift connection. For a very large number of clients, memory
# will be your limiting factor. On a 64 bit JVM, 180KB is the minimum stack size
# per thread, and that will correspond to your use of virtual memory (but physical memory
# may be limited depending on use of stack space).
# sync
# One thread per thrift connection. For a very large number of clients, memory
# will be your limiting factor. On a 64 bit JVM, 180KB is the minimum stack size
# per thread, and that will correspond to your use of virtual memory (but physical memory
# may be limited depending on use of stack space).
#
# hsha -> Stands for "half synchronous, half asynchronous." All thrift clients are handled
# asynchronously using a small number of threads that does not vary with the amount
# of thrift clients (and thus scales well to many clients). The rpc requests are still
# synchronous (one thread per active request). If hsha is selected then it is essential
# that rpc_max_threads is changed from the default value of unlimited.
# hsha
# Stands for "half synchronous, half asynchronous." All thrift clients are handled
# asynchronously using a small number of threads that does not vary with the amount
# of thrift clients (and thus scales well to many clients). The rpc requests are still
# synchronous (one thread per active request). If hsha is selected then it is essential
# that rpc_max_threads is changed from the default value of unlimited.
#
# The default is sync because on Windows hsha is about 30% slower. On Linux,
# sync/hsha performance is about the same, with hsha of course using less memory.
@ -679,13 +715,17 @@ rpc_server_type: sync
# Uncomment to set socket buffer size for internode communication
# Note that when setting this, the buffer size is limited by net.core.wmem_max
# and when not setting it it is defined by net.ipv4.tcp_wmem
# See:
# See also:
# /proc/sys/net/core/wmem_max
# /proc/sys/net/core/rmem_max
# /proc/sys/net/ipv4/tcp_wmem
# /proc/sys/net/ipv4/tcp_wmem
# and: man tcp
# and 'man tcp'
# internode_send_buff_size_in_bytes:
# Uncomment to set socket buffer size for internode communication
# Note that when setting this, the buffer size is limited by net.core.wmem_max
# and when not setting it it is defined by net.ipv4.tcp_wmem
# internode_recv_buff_size_in_bytes:
# Frame size for thrift (maximum message length).
@ -712,13 +752,15 @@ auto_snapshot: true
# Granularity of the collation index of rows within a partition.
# Increase if your rows are large, or if you have a very large
# number of rows per partition. The competing goals are these:
# 1) a smaller granularity means more index entries are generated
# and looking up rows withing the partition by collation column
# is faster
# 2) but, Cassandra will keep the collation index in memory for hot
# rows (as part of the key cache), so a larger granularity means
# you can cache more hot rows
#
# - a smaller granularity means more index entries are generated
# and looking up rows withing the partition by collation column
# is faster
# - but, Cassandra will keep the collation index in memory for hot
# rows (as part of the key cache), so a larger granularity means
# you can cache more hot rows
column_index_size_in_kb: 64
# Per sstable indexed key cache entries (the collation index in memory
# mentioned above) exceeding this size will not be held on heap.
# This means that only partition information is held on heap and the
@ -814,6 +856,7 @@ cross_node_timeout: false
# endpoint_snitch -- Set this to a class that implements
# IEndpointSnitch. The snitch has two functions:
#
# - it teaches Cassandra enough about your network topology to route
# requests efficiently
# - it allows Cassandra to spread replicas around your cluster to avoid
@ -829,34 +872,40 @@ cross_node_timeout: false
# IF THE RACK A REPLICA IS PLACED IN CHANGES AFTER THE REPLICA HAS BEEN
# ADDED TO A RING, THE NODE MUST BE DECOMMISSIONED AND REBOOTSTRAPPED.
#
# Out of the box, Cassandra provides
# - SimpleSnitch:
# Out of the box, Cassandra provides:
#
# SimpleSnitch:
# Treats Strategy order as proximity. This can improve cache
# locality when disabling read repair. Only appropriate for
# single-datacenter deployments.
# - GossipingPropertyFileSnitch
#
# GossipingPropertyFileSnitch
# This should be your go-to snitch for production use. The rack
# and datacenter for the local node are defined in
# cassandra-rackdc.properties and propagated to other nodes via
# gossip. If cassandra-topology.properties exists, it is used as a
# fallback, allowing migration from the PropertyFileSnitch.
# - PropertyFileSnitch:
#
# PropertyFileSnitch:
# Proximity is determined by rack and data center, which are
# explicitly configured in cassandra-topology.properties.
# - Ec2Snitch:
#
# Ec2Snitch:
# Appropriate for EC2 deployments in a single Region. Loads Region
# and Availability Zone information from the EC2 API. The Region is
# treated as the datacenter, and the Availability Zone as the rack.
# Only private IPs are used, so this will not work across multiple
# Regions.
# - Ec2MultiRegionSnitch:
#
# Ec2MultiRegionSnitch:
# Uses public IPs as broadcast_address to allow cross-region
# connectivity. (Thus, you should set seed addresses to the public
# IP as well.) You will need to open the storage_port or
# ssl_storage_port on the public IP firewall. (For intra-Region
# traffic, Cassandra will switch to the private IP after
# establishing a connection.)
# - RackInferringSnitch:
#
# RackInferringSnitch:
# Proximity is determined by rack and data center, which are
# assumed to correspond to the 3rd and 2nd octet of each node's IP
# address, respectively. Unless this happens to match your
@ -896,20 +945,26 @@ dynamic_snitch_badness_threshold: 0.1
request_scheduler: org.apache.cassandra.scheduler.NoScheduler
# Scheduler Options vary based on the type of scheduler
# NoScheduler - Has no options
#
# NoScheduler
# Has no options
#
# RoundRobin
# - throttle_limit -- The throttle_limit is the number of in-flight
# requests per client. Requests beyond
# that limit are queued up until
# running requests can complete.
# The value of 80 here is twice the number of
# concurrent_reads + concurrent_writes.
# - default_weight -- default_weight is optional and allows for
# overriding the default which is 1.
# - weights -- Weights are optional and will default to 1 or the
# overridden default_weight. The weight translates into how
# many requests are handled during each turn of the
# RoundRobin, based on the scheduler id.
# throttle_limit
# The throttle_limit is the number of in-flight
# requests per client. Requests beyond
# that limit are queued up until
# running requests can complete.
# The value of 80 here is twice the number of
# concurrent_reads + concurrent_writes.
# default_weight
# default_weight is optional and allows for
# overriding the default which is 1.
# weights
# Weights are optional and will default to 1 or the
# overridden default_weight. The weight translates into how
# many requests are handled during each turn of the
# RoundRobin, based on the scheduler id.
#
# request_scheduler_options:
# throttle_limit: 80
@ -931,7 +986,7 @@ request_scheduler: org.apache.cassandra.scheduler.NoScheduler
# FIPS compliant settings can be configured at JVM level and should not
# involve changing encryption settings here:
# https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/FIPS.html
# NOTE: No custom encryption options are enabled at the moment
# *NOTE* No custom encryption options are enabled at the moment
# The available internode options are : all, none, dc, rack
#
# If set to dc cassandra will encrypt the traffic between the DCs
@ -974,9 +1029,16 @@ client_encryption_options:
# internode_compression controls whether traffic between nodes is
# compressed.
# can be: all - all traffic is compressed
# dc - traffic between different datacenters is compressed
# none - nothing is compressed.
# Can be:
#
# all
# all traffic is compressed
#
# dc
# traffic between different datacenters is compressed
#
# none
# nothing is compressed.
internode_compression: dc
# Enable or disable tcp_nodelay for inter-dc communication.

256
doc/Makefile Normal file
View File

@ -0,0 +1,256 @@
# Makefile for Sphinx documentation
#
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = build
# Internal variables.
PAPEROPT_a4 = -D latex_paper_size=a4
PAPEROPT_letter = -D latex_paper_size=letter
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
# the i18n builder cannot share the environment and doctrees with the others
I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) source
YAML_DOC_INPUT=../conf/cassandra.yaml
YAML_DOC_OUTPUT=source/configuration/cassandra_config_file.rst
MAKE_CASSANDRA_YAML = python convert_yaml_to_rst.py $(YAML_DOC_INPUT) $(YAML_DOC_OUTPUT)
.PHONY: help
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " html to make standalone HTML files"
@echo " dirhtml to make HTML files named index.html in directories"
@echo " singlehtml to make a single large HTML file"
@echo " pickle to make pickle files"
@echo " json to make JSON files"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " qthelp to make HTML files and a qthelp project"
@echo " applehelp to make an Apple Help Book"
@echo " devhelp to make HTML files and a Devhelp project"
@echo " epub to make an epub"
@echo " epub3 to make an epub3"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " latexpdf to make LaTeX files and run them through pdflatex"
@echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx"
@echo " text to make text files"
@echo " man to make manual pages"
@echo " texinfo to make Texinfo files"
@echo " info to make Texinfo files and run them through makeinfo"
@echo " gettext to make PO message catalogs"
@echo " changes to make an overview of all changed/added/deprecated items"
@echo " xml to make Docutils-native XML files"
@echo " pseudoxml to make pseudoxml-XML files for display purposes"
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
@echo " coverage to run coverage check of the documentation (if enabled)"
@echo " dummy to check syntax errors of document sources"
.PHONY: clean
clean:
rm -rf $(BUILDDIR)/*
rm $(YAML_DOC_OUTPUT)
.PHONY: html
html:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
.PHONY: dirhtml
dirhtml:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
.PHONY: singlehtml
singlehtml:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
@echo
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
.PHONY: pickle
pickle:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
@echo
@echo "Build finished; now you can process the pickle files."
.PHONY: json
json:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
@echo
@echo "Build finished; now you can process the JSON files."
.PHONY: htmlhelp
htmlhelp:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
@echo
@echo "Build finished; now you can run HTML Help Workshop with the" \
".hhp project file in $(BUILDDIR)/htmlhelp."
.PHONY: qthelp
qthelp:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
@echo
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/ApacheCassandraDocumentation.qhcp"
@echo "To view the help file:"
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/ApacheCassandraDocumentation.qhc"
.PHONY: applehelp
applehelp:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp
@echo
@echo "Build finished. The help book is in $(BUILDDIR)/applehelp."
@echo "N.B. You won't be able to view it unless you put it in" \
"~/Library/Documentation/Help or install it in your application" \
"bundle."
.PHONY: devhelp
devhelp:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp
@echo
@echo "Build finished."
@echo "To view the help file:"
@echo "# mkdir -p $$HOME/.local/share/devhelp/ApacheCassandraDocumentation"
@echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/ApacheCassandraDocumentation"
@echo "# devhelp"
.PHONY: epub
epub:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
@echo
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
.PHONY: epub3
epub3:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b epub3 $(ALLSPHINXOPTS) $(BUILDDIR)/epub3
@echo
@echo "Build finished. The epub3 file is in $(BUILDDIR)/epub3."
.PHONY: latex
latex:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
@echo "Run \`make' in that directory to run these through (pdf)latex" \
"(use \`make latexpdf' here to do that automatically)."
.PHONY: latexpdf
latexpdf:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through pdflatex..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
.PHONY: latexpdfja
latexpdfja:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo "Running LaTeX files through platex and dvipdfmx..."
$(MAKE) -C $(BUILDDIR)/latex all-pdf-ja
@echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex."
.PHONY: text
text:
$(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text
@echo
@echo "Build finished. The text files are in $(BUILDDIR)/text."
.PHONY: man
man:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man
@echo
@echo "Build finished. The manual pages are in $(BUILDDIR)/man."
.PHONY: texinfo
texinfo:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo
@echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo."
@echo "Run \`make' in that directory to run these through makeinfo" \
"(use \`make info' here to do that automatically)."
.PHONY: info
info:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo
@echo "Running Texinfo files through makeinfo..."
make -C $(BUILDDIR)/texinfo info
@echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo."
.PHONY: gettext
gettext:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale
@echo
@echo "Build finished. The message catalogs are in $(BUILDDIR)/locale."
.PHONY: changes
changes:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
@echo
@echo "The overview file is in $(BUILDDIR)/changes."
.PHONY: linkcheck
linkcheck:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
@echo
@echo "Link check complete; look for any errors in the above output " \
"or in $(BUILDDIR)/linkcheck/output.txt."
.PHONY: doctest
doctest:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
@echo "Testing of doctests in the sources finished, look at the " \
"results in $(BUILDDIR)/doctest/output.txt."
.PHONY: coverage
coverage:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage
@echo "Testing of coverage in the sources finished, look at the " \
"results in $(BUILDDIR)/coverage/python.txt."
.PHONY: xml
xml:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml
@echo
@echo "Build finished. The XML files are in $(BUILDDIR)/xml."
.PHONY: pseudoxml
pseudoxml:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml
@echo
@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
.PHONY: dummy
dummy:
$(MAKE_CASSANDRA_YAML)
$(SPHINXBUILD) -b dummy $(ALLSPHINXOPTS) $(BUILDDIR)/dummy
@echo
@echo "Build finished. Dummy builder generates no files."

31
doc/README.md Normal file
View File

@ -0,0 +1,31 @@
Apache Cassandra documentation directory
========================================
This directory contains the documentation maintained in-tree for Apache
Cassandra. This directory contains the following documents:
- The source of the official Cassandra documentation, in the `source/`
subdirectory. See below for more details on how to edit/build that
documentation.
- The specification(s) for the supported versions of native transport protocol.
- Additional documentation on the SASI implementation (`SASI.md`). TODO: we
should probably move the first half of that documentation to the general
documentation, and the implementation explanation parts into the wiki.
Official documentation
----------------------
The source for the official documentation for Apache Cassandra can be found in
the `source` subdirectory. The documentation uses [sphinx](http://www.sphinx-doc.org/)
and is thus written in [reStructuredText](http://docutils.sourceforge.net/rst.html).
To build the HTML documentation, you will need to first install sphinx and the
[sphinx ReadTheDocs theme](the https://pypi.python.org/pypi/sphinx_rtd_theme), which
on unix you can do with:
```
pip install sphinx sphinx_rtd_theme
```
The documentation can then be built from this directory by calling `make html`
(or `make.bat html` on windows). Alternatively, the top-level `ant gen-doc`
target can be used.

144
doc/convert_yaml_to_rst.py Normal file
View File

@ -0,0 +1,144 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
A script to convert cassandra.yaml into ReStructuredText for
the online documentation.
Usage:
convert_yaml_to_rest.py conf/cassandra.yaml docs/source/conf.rst
"""
import sys
import re
# Detects options, whether commented or uncommented.
# Group 1 will be non-empty if the option is commented out.
# Group 2 will contain the option name.
# Group 3 will contain the default value, if one exists.
option_re = re.compile(r"^(# ?)?([a-z0-9_]+): ?([^/].*)")
# Detects normal comment lines.
commented_re = re.compile(r"^# ?(.*)")
# A set of option names that have complex values (i.e. lists or dicts).
# This list is hardcoded because there did not seem to be another
# good way to reliably detect this case, especially considering
# that these can be commented out (making it useless to use a yaml parser).
COMPLEX_OPTIONS = (
'seed_provider',
'request_scheduler_options',
'data_file_directories',
'commitlog_compression',
'hints_compression',
'server_encryption_options',
'client_encryption_options',
'transparent_data_encryption_options',
'hinted_handoff_disabled_datacenters'
)
def convert(yaml_file, dest_file):
with open(yaml_file, 'r') as f:
# Trim off the boilerplate header
lines = f.readlines()[7:]
with open(dest_file, 'w') as outfile:
outfile.write("Cassandra Configuration File\n")
outfile.write("============================\n")
# since comments preceed an option, this holds all of the comment
# lines we've seen since the last option
comments_since_last_option = []
line_iter = iter(lines)
while True:
try:
line = next(line_iter)
except StopIteration:
break
match = option_re.match(line)
if match:
option_name = match.group(2)
is_commented = bool(match.group(1))
is_complex = option_name in COMPLEX_OPTIONS
complex_option = read_complex_option(line_iter) if is_complex else None
write_section_header(option_name, outfile)
write_comments(comments_since_last_option, is_commented, outfile)
if is_complex:
write_complex_option(complex_option, outfile)
else:
maybe_write_default_value(match, outfile)
comments_since_last_option = []
else:
comment_match = commented_re.match(line)
if comment_match:
comments_since_last_option.append(comment_match.group(1))
elif line == "\n":
comments_since_last_option.append('')
def write_section_header(option_name, outfile):
outfile.write("\n")
outfile.write("``%s``\n" % (option_name,))
outfile.write("-" * (len(option_name) + 4) + "\n")
def write_comments(comment_lines, is_commented, outfile):
if is_commented:
outfile.write("*This option is commented out by default.*\n")
for comment in comment_lines:
if "SAFETY THRESHOLDS" not in comment_lines:
outfile.write(comment + "\n")
def maybe_write_default_value(option_match, outfile):
default_value = option_match.group(3)
if default_value and default_value != "\n":
outfile.write("\n*Default Value:* %s\n" % (default_value,))
def read_complex_option(line_iter):
option_lines = []
try:
while True:
line = next(line_iter)
if line == '\n':
return option_lines
else:
option_lines.append(line)
except StopIteration:
return option_lines
def write_complex_option(lines, outfile):
outfile.write("\n*Default Value (complex option)*::\n\n")
for line in lines:
outfile.write((" " * 4) + line)
if __name__ == '__main__':
if len(sys.argv) != 3:
print >> sys.stderr, "Usage: %s <yaml source file> <rst dest file>" % (sys.argv[0],)
sys.exit(1)
yaml_file = sys.argv[1]
dest_file = sys.argv[2]
convert(yaml_file, dest_file)

View File

@ -1676,7 +1676,9 @@ Show any permissions granted to @carlos@ or any of @carlos@'s roles, limited to
h2(#types). Data Types
CQL supports a rich set of data types for columns defined in a table, including collection types. On top of those native and collection types, users can also provide custom types (through a JAVA class extending @AbstractType@ loadable by Cassandra). The syntax of types is thus:
CQL supports a rich set of data types for columns defined in a table, including collection types. On top of those native
and collection types, users can also provide custom types (through a JAVA class extending @AbstractType@ loadable by
Cassandra). The syntax of types is thus:
bc(syntax)..
<type> ::= <native-type>

281
doc/make.bat Normal file
View File

@ -0,0 +1,281 @@
@ECHO OFF
REM Command file for Sphinx documentation
if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set BUILDDIR=build
set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% .
set I18NSPHINXOPTS=%SPHINXOPTS% .
if NOT "%PAPER%" == "" (
set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS%
set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS%
)
if "%1" == "" goto help
if "%1" == "help" (
:help
echo.Please use `make ^<target^>` where ^<target^> is one of
echo. html to make standalone HTML files
echo. dirhtml to make HTML files named index.html in directories
echo. singlehtml to make a single large HTML file
echo. pickle to make pickle files
echo. json to make JSON files
echo. htmlhelp to make HTML files and a HTML help project
echo. qthelp to make HTML files and a qthelp project
echo. devhelp to make HTML files and a Devhelp project
echo. epub to make an epub
echo. epub3 to make an epub3
echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter
echo. text to make text files
echo. man to make manual pages
echo. texinfo to make Texinfo files
echo. gettext to make PO message catalogs
echo. changes to make an overview over all changed/added/deprecated items
echo. xml to make Docutils-native XML files
echo. pseudoxml to make pseudoxml-XML files for display purposes
echo. linkcheck to check all external links for integrity
echo. doctest to run all doctests embedded in the documentation if enabled
echo. coverage to run coverage check of the documentation if enabled
echo. dummy to check syntax errors of document sources
goto end
)
if "%1" == "clean" (
for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i
del /q /s %BUILDDIR%\*
goto end
)
REM Check if sphinx-build is available and fallback to Python version if any
%SPHINXBUILD% 1>NUL 2>NUL
if errorlevel 9009 goto sphinx_python
goto sphinx_ok
:sphinx_python
set SPHINXBUILD=python -m sphinx.__init__
%SPHINXBUILD% 2> nul
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.http://sphinx-doc.org/
exit /b 1
)
:sphinx_ok
if "%1" == "html" (
%SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The HTML pages are in %BUILDDIR%/html.
goto end
)
if "%1" == "dirhtml" (
%SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml.
goto end
)
if "%1" == "singlehtml" (
%SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml.
goto end
)
if "%1" == "pickle" (
%SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle
if errorlevel 1 exit /b 1
echo.
echo.Build finished; now you can process the pickle files.
goto end
)
if "%1" == "json" (
%SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json
if errorlevel 1 exit /b 1
echo.
echo.Build finished; now you can process the JSON files.
goto end
)
if "%1" == "htmlhelp" (
%SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp
if errorlevel 1 exit /b 1
echo.
echo.Build finished; now you can run HTML Help Workshop with the ^
.hhp project file in %BUILDDIR%/htmlhelp.
goto end
)
if "%1" == "qthelp" (
%SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp
if errorlevel 1 exit /b 1
echo.
echo.Build finished; now you can run "qcollectiongenerator" with the ^
.qhcp project file in %BUILDDIR%/qthelp, like this:
echo.^> qcollectiongenerator %BUILDDIR%\qthelp\Foo.qhcp
echo.To view the help file:
echo.^> assistant -collectionFile %BUILDDIR%\qthelp\Foo.ghc
goto end
)
if "%1" == "devhelp" (
%SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp
if errorlevel 1 exit /b 1
echo.
echo.Build finished.
goto end
)
if "%1" == "epub" (
%SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The epub file is in %BUILDDIR%/epub.
goto end
)
if "%1" == "epub3" (
%SPHINXBUILD% -b epub3 %ALLSPHINXOPTS% %BUILDDIR%/epub3
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The epub3 file is in %BUILDDIR%/epub3.
goto end
)
if "%1" == "latex" (
%SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
if errorlevel 1 exit /b 1
echo.
echo.Build finished; the LaTeX files are in %BUILDDIR%/latex.
goto end
)
if "%1" == "latexpdf" (
%SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
cd %BUILDDIR%/latex
make all-pdf
cd %~dp0
echo.
echo.Build finished; the PDF files are in %BUILDDIR%/latex.
goto end
)
if "%1" == "latexpdfja" (
%SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
cd %BUILDDIR%/latex
make all-pdf-ja
cd %~dp0
echo.
echo.Build finished; the PDF files are in %BUILDDIR%/latex.
goto end
)
if "%1" == "text" (
%SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The text files are in %BUILDDIR%/text.
goto end
)
if "%1" == "man" (
%SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The manual pages are in %BUILDDIR%/man.
goto end
)
if "%1" == "texinfo" (
%SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo.
goto end
)
if "%1" == "gettext" (
%SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The message catalogs are in %BUILDDIR%/locale.
goto end
)
if "%1" == "changes" (
%SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes
if errorlevel 1 exit /b 1
echo.
echo.The overview file is in %BUILDDIR%/changes.
goto end
)
if "%1" == "linkcheck" (
%SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck
if errorlevel 1 exit /b 1
echo.
echo.Link check complete; look for any errors in the above output ^
or in %BUILDDIR%/linkcheck/output.txt.
goto end
)
if "%1" == "doctest" (
%SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest
if errorlevel 1 exit /b 1
echo.
echo.Testing of doctests in the sources finished, look at the ^
results in %BUILDDIR%/doctest/output.txt.
goto end
)
if "%1" == "coverage" (
%SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage
if errorlevel 1 exit /b 1
echo.
echo.Testing of coverage in the sources finished, look at the ^
results in %BUILDDIR%/coverage/python.txt.
goto end
)
if "%1" == "xml" (
%SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The XML files are in %BUILDDIR%/xml.
goto end
)
if "%1" == "pseudoxml" (
%SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml
if errorlevel 1 exit /b 1
echo.
echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml.
goto end
)
if "%1" == "dummy" (
%SPHINXBUILD% -b dummy %ALLSPHINXOPTS% %BUILDDIR%/dummy
if errorlevel 1 exit /b 1
echo.
echo.Build finished. Dummy builder generates no files.
goto end
)
:end

View File

@ -0,0 +1,43 @@
div:not(.highlight) > pre {
background: #fff;
border: 1px solid #e1e4e5;
color: #404040;
margin: 1px 0 24px 0;
overflow-x: auto;
padding: 12px 12px;
font-size: 12px;
}
a.reference.internal code.literal {
border: none;
font-size: 12px;
color: #2980B9;
padding: 0;
background: none;
}
a.reference.internal:visited code.literal {
color: #9B59B6;
padding: 0;
background: none;
}
/* override table width restrictions */
.wy-table-responsive table td, .wy-table-responsive table th {
white-space: normal;
}
.wy-table-responsive {
margin-bottom: 24px;
max-width: 100%;
overflow: visible;
}
table.contentstable {
margin: 0;
}
td.rightcolumn {
padding-left: 30px;
}

View File

@ -0,0 +1,33 @@
{% extends "defindex.html" %}
{% block tables %}
<p><strong>{% trans %}Main documentation parts:{% endtrans %}</strong></p>
<table class="contentstable" align="center"><tr>
<td width="50%">
<p class="biglink"><a class="biglink" href="{{ pathto("getting_started/index") }}">{% trans %}Getting started{% endtrans %}</a><br/>
<span class="linkdescr">{% trans %}Newbie friendly starting point{% endtrans %}</span></p>
<p class="biglink"><a class="biglink" href="{{ pathto("architecture/index") }}">{% trans %}Cassandra Architecture{% endtrans %}</a><br/>
<span class="linkdescr">{% trans %}Cassandra's big picture{% endtrans %}</span></p>
<p class="biglink"><a class="biglink" href="{{ pathto("data_modeling/index") }}">{% trans %}Data Modeling{% endtrans %}</a><br/>
<span class="linkdescr">{% trans %}Or how to make square pegs fit round holes{% endtrans %}</span></p>
<p class="biglink"><a class="biglink" href="{{ pathto("cql/index") }}">{% trans %}Cassandra Query Language{% endtrans %}</a><br/>
<span class="linkdescr">{% trans %}CQL reference documentation{% endtrans %}</span></p>
<p class="biglink"><a class="biglink" href="{{ pathto("configuration/index") }}">{% trans %}Configuration{% endtrans %}</a><br/>
<span class="linkdescr">{% trans %}Cassandra's handles and knobs{% endtrans %}</span></p>
</td><td width="50%" class="rightcolumn">
<p class="biglink"><a class="biglink" href="{{ pathto("operating/index") }}">{% trans %}Operating Cassandra{% endtrans %}</a><br/>
<span class="linkdescr">{% trans %}The operator's corner{% endtrans %}</span></p>
<p class="biglink"><a class="biglink" href="{{ pathto("tooling/index") }}">{% trans %}Cassandra's Tools{% endtrans %}</a><br/>
<span class="linkdescr">{% trans %}cqlsh, nodetool, ...{% endtrans %}</span></p>
<p class="biglink"><a class="biglink" href="{{ pathto("troubleshooting/index") }}">{% trans %}Troubleshooting{% endtrans %}</a><br/>
<span class="linkdescr">{% trans %}What to look for when you have a problem{% endtrans %}</span></p>
<p class="biglink"><a class="biglink" href="{{ pathto("faq/index") }}">{% trans %}FAQs{% endtrans %}</a><br/>
<span class="linkdescr">{% trans %}Frequently Asked Questions (with answers!){% endtrans %}</span></p>
</td></tr>
</table>
<p><strong>{% trans %}Meta informations:{% endtrans %}</strong></p>
<p class="biglink"><a class="biglink" href="{{ pathto("bugs") }}">{% trans %}Reporting bugs{% endtrans %}</a></p>
<p class="biglink"><a class="biglink" href="{{ pathto("contactus") }}">{% trans %}Contact us{% endtrans %}</a></p>
{% endblock %}

View File

@ -0,0 +1,137 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Dynamo
------
Gossip
^^^^^^
.. todo:: todo
Failure Detection
^^^^^^^^^^^^^^^^^
.. todo:: todo
Token Ring/Ranges
^^^^^^^^^^^^^^^^^
.. todo:: todo
.. _replication-strategy:
Replication
^^^^^^^^^^^
The replication strategy of a keyspace determines which nodes are replicas for a given token range. The two main
replication strategies are :ref:`simple-strategy` and :ref:`network-topology-strategy`.
.. _simple-strategy:
SimpleStrategy
~~~~~~~~~~~~~~
SimpleStrategy allows a single integer ``replication_factor`` to be defined. This determines the number of nodes that
should contain a copy of each row. For example, if ``replication_factor`` is 3, then three different nodes should store
a copy of each row.
SimpleStrategy treats all nodes identically, ignoring any configured datacenters or racks. To determine the replicas
for a token range, Cassandra iterates through the tokens in the ring, starting with the token range of interest. For
each token, it checks whether the owning node has been added to the set of replicas, and if it has not, it is added to
the set. This process continues until ``replication_factor`` distinct nodes have been added to the set of replicas.
.. _network-topology-strategy:
NetworkTopologyStrategy
~~~~~~~~~~~~~~~~~~~~~~~
NetworkTopologyStrategy allows a replication factor to be specified for each datacenter in the cluster. Even if your
cluster only uses a single datacenter, NetworkTopologyStrategy should be prefered over SimpleStrategy to make it easier
to add new physical or virtual datacenters to the cluster later.
In addition to allowing the replication factor to be specified per-DC, NetworkTopologyStrategy also attempts to choose
replicas within a datacenter from different racks. If the number of racks is greater than or equal to the replication
factor for the DC, each replica will be chosen from a different rack. Otherwise, each rack will hold at least one
replica, but some racks may hold more than one. Note that this rack-aware behavior has some potentially `surprising
implications <https://issues.apache.org/jira/browse/CASSANDRA-3810>`_. For example, if there are not an even number of
nodes in each rack, the data load on the smallest rack may be much higher. Similarly, if a single node is bootstrapped
into a new rack, it will be considered a replica for the entire ring. For this reason, many operators choose to
configure all nodes on a single "rack".
Tunable Consistency
^^^^^^^^^^^^^^^^^^^
Cassandra supports a per-operation tradeoff between consistency and availability through *Consistency Levels*.
Essentially, an operation's consistency level specifies how many of the replicas need to respond to the coordinator in
order to consider the operation a success.
The following consistency levels are available:
``ONE``
Only a single replica must respond.
``TWO``
Two replicas must respond.
``THREE``
Three replicas must respond.
``QUORUM``
A majority (n/2 + 1) of the replicas must respond.
``ALL``
All of the replicas must respond.
``LOCAL_QUORUM``
A majority of the replicas in the local datacenter (whichever datacenter the coordinator is in) must respond.
``EACH_QUORUM``
A majority of the replicas in each datacenter must respond.
``LOCAL_ONE``
Only a single replica must respond. In a multi-datacenter cluster, this also gaurantees that read requests are not
sent to replicas in a remote datacenter.
``ANY``
A single replica may respond, or the coordinator may store a hint. If a hint is stored, the coordinator will later
attempt to replay the hint and deliver the mutation to the replicas. This consistency level is only accepted for
write operations.
Write operations are always sent to all replicas, regardless of consistency level. The consistency level simply
controls how many responses the coordinator waits for before responding to the client.
For read operations, the coordinator generally only issues read commands to enough replicas to satisfy the consistency
level. There are a couple of exceptions to this:
- Speculative retry may issue a redundant read request to an extra replica if the other replicas have not responded
within a specified time window.
- Based on ``read_repair_chance`` and ``dclocal_read_repair_chance`` (part of a table's schema), read requests may be
randomly sent to all replicas in order to repair potentially inconsistent data.
Picking Consistency Levels
~~~~~~~~~~~~~~~~~~~~~~~~~~
It is common to pick read and write consistency levels that are high enough to overlap, resulting in "strong"
consistency. This is typically expressed as ``W + R > RF``, where ``W`` is the write consistency level, ``R`` is the
read consistency level, and ``RF`` is the replication factor. For example, if ``RF = 3``, a ``QUORUM`` request will
require responses from at least two of the three replicas. If ``QUORUM`` is used for both writes and reads, at least
one of the replicas is guaranteed to participate in *both* the write and the read request, which in turn guarantees that
the latest write will be read. In a multi-datacenter environment, ``LOCAL_QUORUM`` can be used to provide a weaker but
still useful guarantee: reads are guaranteed to see the latest write from within the same datacenter.
If this type of strong consistency isn't required, lower consistency levels like ``ONE`` may be used to improve
throughput, latency, and availability.

View File

@ -0,0 +1,20 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Guarantees
----------
.. todo:: todo

View File

@ -0,0 +1,29 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Architecture
============
This section describes the general architecture of Apache Cassandra.
.. toctree::
:maxdepth: 2
overview
dynamo
storage_engine
guarantees

View File

@ -0,0 +1,20 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Overview
--------
.. todo:: todo

View File

@ -0,0 +1,82 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Storage Engine
--------------
.. _commit-log:
CommitLog
^^^^^^^^^
.. todo:: todo
.. _memtables:
Memtables
^^^^^^^^^
Memtables are in-memory structures where Cassandra buffers writes. In general, there is one active memtable per table.
Eventually, memtables are flushed onto disk and become immutable `SSTables`_. This can be triggered in several
ways:
- The memory usage of the memtables exceeds the configured threshold (see ``memtable_cleanup_threshold``)
- The :ref:`commit-log` approaches its maximum size, and forces memtable flushes in order to allow commitlog segments to
be freed
Memtables may be stored entirely on-heap or partially off-heap, depending on ``memtable_allocation_type``.
SSTables
^^^^^^^^
SSTables are the immutable data files that Cassandra uses for persisting data on disk.
As SSTables are flushed to disk from :ref:`memtables` or are streamed from other nodes, Cassandra triggers compactions
which combine multiple SSTables into one. Once the new SSTable has been written, the old SSTables can be removed.
Each SSTable is comprised of multiple components stored in separate files:
``Data.db``
The actual data, i.e. the contents of rows.
``Index.db``
An index from partition keys to positions in the ``Data.db`` file. For wide partitions, this may also include an
index to rows within a partition.
``Summary.db``
A sampling of (by default) every 128th entry in the ``Index.db`` file.
``Filter.db``
A Bloom Filter of the partition keys in the SSTable.
``CompressionInfo.db``
Metadata about the offsets and lengths of compression chunks in the ``Data.db`` file.
``Statistics.db``
Stores metadata about the SSTable, including information about timestamps, tombstones, clustering keys, compaction,
repair, compression, TTLs, and more.
``Digest.crc32``
A CRC-32 digest of the ``Data.db`` file.
``TOC.txt``
A plain text list of the component files for the SSTable.
Within the ``Data.db`` file, rows are organized by partition. These partitions are sorted in token order (i.e. by a
hash of the partition key when the default partitioner, ``Murmur3Partition``, is used). Within a partition, rows are
stored in the order of their clustering keys.
SSTables can be optionally compressed using block-based compression.

31
doc/source/bugs.rst Normal file
View File

@ -0,0 +1,31 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Reporting bugs and contributing
===============================
If you encounter a problem with Cassandra, the first places to ask for help are the :ref:`user mailing list
<mailing-lists>` and the ``#cassandra`` :ref:`IRC channel <irc-channels>`.
If, after having asked for help, you suspect that you have found a bug in Cassandra, you should report it by opening a
ticket through the `Apache Cassandra JIRA <https://issues.apache.org/jira/browse/CASSANDRA>`__. Please provide as much
details as you can on your problem, and don't forget to indicate which version of Cassandra you are running and on which
environment.
If you would like to contribute, please check `the section on contributing
<https://wiki.apache.org/cassandra/HowToContribute>`__ on the Cassandra wiki. Please note that the source of this
documentation is part of the Cassandra git repository and hence contributions to the documentation should follow the
same path.

432
doc/source/conf.py Normal file
View File

@ -0,0 +1,432 @@
# -*- coding: utf-8 -*-
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Apache Cassandra Documentation documentation build configuration file
#
# This file is execfile()d with the current directory set to its containing
# dir.
import re
# Finds out the version (so we don't have to manually edit that file every
# time we change the version)
cassandra_build_file = '../../build.xml'
with open(cassandra_build_file) as f:
m = re.search("name=\"base\.version\" value=\"([^\"]+)\"", f.read())
if not m or m.lastindex != 1:
raise RuntimeException("Problem finding version in build.xml file, this shouldn't happen.")
cassandra_version = m.group(1)
# -- General configuration ------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
# needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.todo',
'sphinx.ext.mathjax',
'sphinx.ext.ifconfig',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
source_suffix = ['.rst']
# The encoding of source files.
#
# source_encoding = 'utf-8-sig'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = u'Apache Cassandra'
copyright = u'2016, The Apache Cassandra team'
author = u'The Apache Cassandra team'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
version = cassandra_version
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#
# today = ''
#
# Else, today_fmt is used as the format for a strftime call.
#
# today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This patterns also effect to html_static_path and html_extra_path
exclude_patterns = []
# The reST default role (used for this markup: `text`) to use for all
# documents.
#
# default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
#
# add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#
# add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#
# show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# A list of ignored prefixes for module index sorting.
# modindex_common_prefix = []
# If true, keep warnings as "system message" paragraphs in the built documents.
# keep_warnings = False
# If true, `todo` and `todoList` produce output, else they produce nothing.
todo_include_todos = True
# -- Options for HTML output ----------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'
html_context = { 'extra_css_files': [ '_static/extra.css' ] }
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = ['.']
# The name for this set of Sphinx documents.
# "<project> v<release> documentation" by default.
#
html_title = u'Apache Cassandra Documentation v%s' % version
# A shorter title for the navigation bar. Default is the same as html_title.
#
# html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#
# html_logo = None
# The name of an image file (relative to this directory) to use as a favicon of
# the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#
# html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# Add any extra paths that contain custom files (such as robots.txt or
# .htaccess) here, relative to this directory. These files are copied
# directly to the root of the documentation.
#
# html_extra_path = []
# If not None, a 'Last updated on:' timestamp is inserted at every page
# bottom, using the given strftime format.
# The empty string is equivalent to '%b %d, %Y'.
#
# html_last_updated_fmt = None
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#
# html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
#
# html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
#
html_additional_pages = {
'index': 'indexcontent.html'
}
# If false, no module index is generated.
#
# html_domain_indices = True
# If false, no index is generated.
#
# html_use_index = True
# If true, the index is split into individual pages for each letter.
#
# html_split_index = False
# If true, links to the reST sources are added to the pages.
#
# html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#
# html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#
# html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
#
# html_use_opensearch = ''
# This is the file name suffix for HTML files (e.g. ".xhtml").
# html_file_suffix = None
# Language to be used for generating the HTML full-text search index.
# Sphinx supports the following languages:
# 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja'
# 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr', 'zh'
#
# html_search_language = 'en'
# A dictionary with options for the search language support, empty by default.
# 'ja' uses this config value.
# 'zh' user can custom change `jieba` dictionary path.
#
# html_search_options = {'type': 'default'}
# The name of a javascript file (relative to the configuration directory) that
# implements a search results scorer. If empty, the default will be used.
#
# html_search_scorer = 'scorer.js'
# Output file base name for HTML help builder.
htmlhelp_basename = 'ApacheCassandraDocumentationdoc'
# -- Options for LaTeX output ---------------------------------------------
latex_elements = {
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',
# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',
# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',
# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
}
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'ApacheCassandra.tex', u'Apache Cassandra Documentation',
u'The Apache Cassandra team', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
#
# latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#
# latex_use_parts = False
# If true, show page references after internal links.
#
# latex_show_pagerefs = False
# If true, show URL addresses after external links.
#
# latex_show_urls = False
# Documents to append as an appendix to all manuals.
#
# latex_appendices = []
# If false, no module index is generated.
#
# latex_domain_indices = True
# -- Options for manual page output ---------------------------------------
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'apachecassandra', u'Apache Cassandra Documentation',
[author], 1)
]
# If true, show URL addresses after external links.
#
# man_show_urls = False
# -- Options for Texinfo output -------------------------------------------
# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'ApacheCassandra', u'Apache Cassandra Documentation',
author, 'ApacheCassandraDocumentation', 'One line description of project.',
'Miscellaneous'),
]
# Documents to append as an appendix to all manuals.
#
# texinfo_appendices = []
# If false, no module index is generated.
#
# texinfo_domain_indices = True
# How to display URL addresses: 'footnote', 'no', or 'inline'.
#
# texinfo_show_urls = 'footnote'
# If true, do not generate a @detailmenu in the "Top" node's menu.
#
# texinfo_no_detailmenu = False
# -- Options for Epub output ----------------------------------------------
# Bibliographic Dublin Core info.
epub_title = project
epub_author = author
epub_publisher = author
epub_copyright = copyright
# The basename for the epub file. It defaults to the project name.
# epub_basename = project
# The HTML theme for the epub output. Since the default themes are not
# optimized for small screen space, using the same theme for HTML and epub
# output is usually not wise. This defaults to 'epub', a theme designed to save
# visual space.
#
# epub_theme = 'epub'
# The language of the text. It defaults to the language option
# or 'en' if the language is not set.
#
# epub_language = ''
# The scheme of the identifier. Typical schemes are ISBN or URL.
# epub_scheme = ''
# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#
# epub_identifier = ''
# A unique identification for the text.
#
# epub_uid = ''
# A tuple containing the cover image and cover page html template filenames.
#
# epub_cover = ()
# A sequence of (type, uri, title) tuples for the guide element of content.opf.
#
# epub_guide = ()
# HTML files that should be inserted before the pages created by sphinx.
# The format is a list of tuples containing the path and title.
#
# epub_pre_files = []
# HTML files that should be inserted after the pages created by sphinx.
# The format is a list of tuples containing the path and title.
#
# epub_post_files = []
# A list of files that should not be packed into the epub file.
epub_exclude_files = ['search.html']
# The depth of the table of contents in toc.ncx.
#
# epub_tocdepth = 3
# Allow duplicate toc entries.
#
# epub_tocdup = True
# Choose between 'default' and 'includehidden'.
#
# epub_tocscope = 'default'
# Fix unsupported image types using the Pillow.
#
# epub_fix_images = False
# Scale large images.
#
# epub_max_image_width = 0
# How to display URL addresses: 'footnote', 'no', or 'inline'.
#
# epub_show_urls = 'inline'
# If false, no index is generated.
#
# epub_use_index = True

View File

@ -0,0 +1,25 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Configuring Cassandra
=====================
This section describes how to configure Apache Cassandra.
.. toctree::
:maxdepth: 1
cassandra_config_file

53
doc/source/contactus.rst Normal file
View File

@ -0,0 +1,53 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Contact us
==========
You can get in touch with the Cassandra community either via the mailing lists or the freenode IRC channels.
.. _mailing-lists:
Mailing lists
-------------
The following mailing lists are available:
- `Users <http://www.mail-archive.com/user@cassandra.apache.org/>`__ General discussion list for users - `Subscribe
<user-subscribe@cassandra.apache.org>`__
- `Developers <http://www.mail-archive.com/dev@cassandra.apache.org/>`__ Development related discussion - `Subscribe
<dev-subscribe@cassandra.apache.org>`__
- `Commits <http://www.mail-archive.com/commits@cassandra.apache.org/>`__ Commit notification source repository -
`Subscribe <commits-subscribe@cassandra.apache.org>`__
- `Client Libraries <http://www.mail-archive.com/client-dev@cassandra.apache.org/>`__ Discussion related to the
development of idiomatic client APIs - `Subscribe <client-dev-subscribe@cassandra.apache.org>`__
Subscribe by sending an email to the email address in the Subscribe links above. Follow the instructions in the welcome
email to confirm your subscription. Make sure to keep the welcome email as it contains instructions on how to
unsubscribe.
.. _irc-channels:
IRC
---
To chat with developers or users in real-time, join our channels on `IRC freenode <http://webchat.freenode.net/>`__. The
following channels are available:
- ``#cassandra`` - for user questions and general discussions.
- ``#cassandra-dev`` - strictly for questions or discussions related to Cassandra development.
- ``#cassandra-builds`` - results of automated test builds.

View File

@ -0,0 +1,308 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
Appendices
----------
.. _appendix-A:
Appendix A: CQL Keywords
~~~~~~~~~~~~~~~~~~~~~~~~
CQL distinguishes between *reserved* and *non-reserved* keywords.
Reserved keywords cannot be used as identifier, they are truly reserved
for the language (but one can enclose a reserved keyword by
double-quotes to use it as an identifier). Non-reserved keywords however
only have a specific meaning in certain context but can used as
identifier otherwise. The only *raison dêtre* of these non-reserved
keywords is convenience: some keyword are non-reserved when it was
always easy for the parser to decide whether they were used as keywords
or not.
+--------------------+-------------+
| Keyword | Reserved? |
+====================+=============+
| ``ADD`` | yes |
+--------------------+-------------+
| ``AGGREGATE`` | no |
+--------------------+-------------+
| ``ALL`` | no |
+--------------------+-------------+
| ``ALLOW`` | yes |
+--------------------+-------------+
| ``ALTER`` | yes |
+--------------------+-------------+
| ``AND`` | yes |
+--------------------+-------------+
| ``APPLY`` | yes |
+--------------------+-------------+
| ``AS`` | no |
+--------------------+-------------+
| ``ASC`` | yes |
+--------------------+-------------+
| ``ASCII`` | no |
+--------------------+-------------+
| ``AUTHORIZE`` | yes |
+--------------------+-------------+
| ``BATCH`` | yes |
+--------------------+-------------+
| ``BEGIN`` | yes |
+--------------------+-------------+
| ``BIGINT`` | no |
+--------------------+-------------+
| ``BLOB`` | no |
+--------------------+-------------+
| ``BOOLEAN`` | no |
+--------------------+-------------+
| ``BY`` | yes |
+--------------------+-------------+
| ``CALLED`` | no |
+--------------------+-------------+
| ``CLUSTERING`` | no |
+--------------------+-------------+
| ``COLUMNFAMILY`` | yes |
+--------------------+-------------+
| ``COMPACT`` | no |
+--------------------+-------------+
| ``CONTAINS`` | no |
+--------------------+-------------+
| ``COUNT`` | no |
+--------------------+-------------+
| ``COUNTER`` | no |
+--------------------+-------------+
| ``CREATE`` | yes |
+--------------------+-------------+
| ``CUSTOM`` | no |
+--------------------+-------------+
| ``DATE`` | no |
+--------------------+-------------+
| ``DECIMAL`` | no |
+--------------------+-------------+
| ``DELETE`` | yes |
+--------------------+-------------+
| ``DESC`` | yes |
+--------------------+-------------+
| ``DESCRIBE`` | yes |
+--------------------+-------------+
| ``DISTINCT`` | no |
+--------------------+-------------+
| ``DOUBLE`` | no |
+--------------------+-------------+
| ``DROP`` | yes |
+--------------------+-------------+
| ``ENTRIES`` | yes |
+--------------------+-------------+
| ``EXECUTE`` | yes |
+--------------------+-------------+
| ``EXISTS`` | no |
+--------------------+-------------+
| ``FILTERING`` | no |
+--------------------+-------------+
| ``FINALFUNC`` | no |
+--------------------+-------------+
| ``FLOAT`` | no |
+--------------------+-------------+
| ``FROM`` | yes |
+--------------------+-------------+
| ``FROZEN`` | no |
+--------------------+-------------+
| ``FULL`` | yes |
+--------------------+-------------+
| ``FUNCTION`` | no |
+--------------------+-------------+
| ``FUNCTIONS`` | no |
+--------------------+-------------+
| ``GRANT`` | yes |
+--------------------+-------------+
| ``IF`` | yes |
+--------------------+-------------+
| ``IN`` | yes |
+--------------------+-------------+
| ``INDEX`` | yes |
+--------------------+-------------+
| ``INET`` | no |
+--------------------+-------------+
| ``INFINITY`` | yes |
+--------------------+-------------+
| ``INITCOND`` | no |
+--------------------+-------------+
| ``INPUT`` | no |
+--------------------+-------------+
| ``INSERT`` | yes |
+--------------------+-------------+
| ``INT`` | no |
+--------------------+-------------+
| ``INTO`` | yes |
+--------------------+-------------+
| ``JSON`` | no |
+--------------------+-------------+
| ``KEY`` | no |
+--------------------+-------------+
| ``KEYS`` | no |
+--------------------+-------------+
| ``KEYSPACE`` | yes |
+--------------------+-------------+
| ``KEYSPACES`` | no |
+--------------------+-------------+
| ``LANGUAGE`` | no |
+--------------------+-------------+
| ``LIMIT`` | yes |
+--------------------+-------------+
| ``LIST`` | no |
+--------------------+-------------+
| ``LOGIN`` | no |
+--------------------+-------------+
| ``MAP`` | no |
+--------------------+-------------+
| ``MODIFY`` | yes |
+--------------------+-------------+
| ``NAN`` | yes |
+--------------------+-------------+
| ``NOLOGIN`` | no |
+--------------------+-------------+
| ``NORECURSIVE`` | yes |
+--------------------+-------------+
| ``NOSUPERUSER`` | no |
+--------------------+-------------+
| ``NOT`` | yes |
+--------------------+-------------+
| ``NULL`` | yes |
+--------------------+-------------+
| ``OF`` | yes |
+--------------------+-------------+
| ``ON`` | yes |
+--------------------+-------------+
| ``OPTIONS`` | no |
+--------------------+-------------+
| ``OR`` | yes |
+--------------------+-------------+
| ``ORDER`` | yes |
+--------------------+-------------+
| ``PASSWORD`` | no |
+--------------------+-------------+
| ``PERMISSION`` | no |
+--------------------+-------------+
| ``PERMISSIONS`` | no |
+--------------------+-------------+
| ``PRIMARY`` | yes |
+--------------------+-------------+
| ``RENAME`` | yes |
+--------------------+-------------+
| ``REPLACE`` | yes |
+--------------------+-------------+
| ``RETURNS`` | no |
+--------------------+-------------+
| ``REVOKE`` | yes |
+--------------------+-------------+
| ``ROLE`` | no |
+--------------------+-------------+
| ``ROLES`` | no |
+--------------------+-------------+
| ``SCHEMA`` | yes |
+--------------------+-------------+
| ``SELECT`` | yes |
+--------------------+-------------+
| ``SET`` | yes |
+--------------------+-------------+
| ``SFUNC`` | no |
+--------------------+-------------+
| ``SMALLINT`` | no |
+--------------------+-------------+
| ``STATIC`` | no |
+--------------------+-------------+
| ``STORAGE`` | no |
+--------------------+-------------+
| ``STYPE`` | no |
+--------------------+-------------+
| ``SUPERUSER`` | no |
+--------------------+-------------+
| ``TABLE`` | yes |
+--------------------+-------------+
| ``TEXT`` | no |
+--------------------+-------------+
| ``TIME`` | no |
+--------------------+-------------+
| ``TIMESTAMP`` | no |
+--------------------+-------------+
| ``TIMEUUID`` | no |
+--------------------+-------------+
| ``TINYINT`` | no |
+--------------------+-------------+
| ``TO`` | yes |
+--------------------+-------------+
| ``TOKEN`` | yes |
+--------------------+-------------+
| ``TRIGGER`` | no |
+--------------------+-------------+
| ``TRUNCATE`` | yes |
+--------------------+-------------+
| ``TTL`` | no |
+--------------------+-------------+
| ``TUPLE`` | no |
+--------------------+-------------+
| ``TYPE`` | no |
+--------------------+-------------+
| ``UNLOGGED`` | yes |
+--------------------+-------------+
| ``UPDATE`` | yes |
+--------------------+-------------+
| ``USE`` | yes |
+--------------------+-------------+
| ``USER`` | no |
+--------------------+-------------+
| ``USERS`` | no |
+--------------------+-------------+
| ``USING`` | yes |
+--------------------+-------------+
| ``UUID`` | no |
+--------------------+-------------+
| ``VALUES`` | no |
+--------------------+-------------+
| ``VARCHAR`` | no |
+--------------------+-------------+
| ``VARINT`` | no |
+--------------------+-------------+
| ``WHERE`` | yes |
+--------------------+-------------+
| ``WITH`` | yes |
+--------------------+-------------+
| ``WRITETIME`` | no |
+--------------------+-------------+
Appendix B: CQL Reserved Types
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The following type names are not currently used by CQL, but are reserved
for potential future use. User-defined types may not use reserved type
names as their name.
+-----------------+
| type |
+=================+
| ``bitstring`` |
+-----------------+
| ``byte`` |
+-----------------+
| ``complex`` |
+-----------------+
| ``enum`` |
+-----------------+
| ``interval`` |
+-----------------+
| ``macaddr`` |
+-----------------+

189
doc/source/cql/changes.rst Normal file
View File

@ -0,0 +1,189 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
Changes
-------
The following describes the changes in each version of CQL.
3.4.2
^^^^^
- If a table has a non zero ``default_time_to_live``, then explicitly specifying a TTL of 0 in an ``INSERT`` or
``UPDATE`` statement will result in the new writes not having any expiration (that is, an explicit TTL of 0 cancels
the ``default_time_to_live``). This wasn't the case before and the ``default_time_to_live`` was applied even though a
TTL had been explicitly set.
- ``ALTER TABLE`` ``ADD`` and ``DROP`` now allow multiple columns to be added/removed.
- New ``PER PARTITION LIMIT`` option for ``SELECT`` statements (see `CASSANDRA-7017
<https://issues.apache.org/jira/browse/CASSANDRA-7017)>`__.
- :ref:`User-defined functions <cql-functions>` can now instantiate ``UDTValue`` and ``TupleValue`` instances via the
new ``UDFContext`` interface (see `CASSANDRA-10818 <https://issues.apache.org/jira/browse/CASSANDRA-10818)>`__.
- :ref:`User-defined types <udts>` may now be stored in a non-frozen form, allowing individual fields to be updated and
deleted in ``UPDATE`` statements and ``DELETE`` statements, respectively. (`CASSANDRA-7423
<https://issues.apache.org/jira/browse/CASSANDRA-7423)>`__).
3.4.1
^^^^^
- Adds ``CAST`` functions.
3.4.0
^^^^^
- Support for :ref:`materialized views <materialized-views>`.
- ``DELETE`` support for inequality expressions and ``IN`` restrictions on any primary key columns.
- ``UPDATE`` support for ``IN`` restrictions on any primary key columns.
3.3.1
^^^^^
- The syntax ``TRUNCATE TABLE X`` is now accepted as an alias for ``TRUNCATE X``.
3.3.0
^^^^^
- :ref:`User-defined functions and aggregates <cql-functions>` are now supported.
- Allows double-dollar enclosed strings literals as an alternative to single-quote enclosed strings.
- Introduces Roles to supersede user based authentication and access control
- New ``date``, ``time``, ``tinyint`` and ``smallint`` :ref:`data types <data-types>` have been added.
- :ref:`JSON support <cql-json>` has been added
- Adds new time conversion functions and deprecate ``dateOf`` and ``unixTimestampOf``.
3.2.0
^^^^^
- :ref:`User-defined types <udts>` supported.
- ``CREATE INDEX`` now supports indexing collection columns, including indexing the keys of map collections through the
``keys()`` function
- Indexes on collections may be queried using the new ``CONTAINS`` and ``CONTAINS KEY`` operators
- :ref:`Tuple types <tuples>` were added to hold fixed-length sets of typed positional fields.
- ``DROP INDEX`` now supports optionally specifying a keyspace.
3.1.7
^^^^^
- ``SELECT`` statements now support selecting multiple rows in a single partition using an ``IN`` clause on combinations
of clustering columns.
- ``IF NOT EXISTS`` and ``IF EXISTS`` syntax is now supported by ``CREATE USER`` and ``DROP USER`` statements,
respectively.
3.1.6
^^^^^
- A new ``uuid()`` method has been added.
- Support for ``DELETE ... IF EXISTS`` syntax.
3.1.5
^^^^^
- It is now possible to group clustering columns in a relation, see :ref:`WHERE <where-clause>` clauses.
- Added support for :ref:`static columns <static-columns>`.
3.1.4
^^^^^
- ``CREATE INDEX`` now allows specifying options when creating CUSTOM indexes.
3.1.3
^^^^^
- Millisecond precision formats have been added to the :ref:`timestamp <timestamps>` parser.
3.1.2
^^^^^
- ``NaN`` and ``Infinity`` has been added as valid float constants. They are now reserved keywords. In the unlikely case
you we using them as a column identifier (or keyspace/table one), you will now need to double quote them.
3.1.1
^^^^^
- ``SELECT`` statement now allows listing the partition keys (using the ``DISTINCT`` modifier). See `CASSANDRA-4536
<https://issues.apache.org/jira/browse/CASSANDRA-4536>`__.
- The syntax ``c IN ?`` is now supported in ``WHERE`` clauses. In that case, the value expected for the bind variable
will be a list of whatever type ``c`` is.
- It is now possible to use named bind variables (using ``:name`` instead of ``?``).
3.1.0
^^^^^
- ``ALTER TABLE`` ``DROP`` option added.
- ``SELECT`` statement now supports aliases in select clause. Aliases in WHERE and ORDER BY clauses are not supported.
- ``CREATE`` statements for ``KEYSPACE``, ``TABLE`` and ``INDEX`` now supports an ``IF NOT EXISTS`` condition.
Similarly, ``DROP`` statements support a ``IF EXISTS`` condition.
- ``INSERT`` statements optionally supports a ``IF NOT EXISTS`` condition and ``UPDATE`` supports ``IF`` conditions.
3.0.5
^^^^^
- ``SELECT``, ``UPDATE``, and ``DELETE`` statements now allow empty ``IN`` relations (see `CASSANDRA-5626
<https://issues.apache.org/jira/browse/CASSANDRA-5626)>`__.
3.0.4
^^^^^
- Updated the syntax for custom :ref:`secondary indexes <secondary-indexes>`.
- Non-equal condition on the partition key are now never supported, even for ordering partitioner as this was not
correct (the order was **not** the one of the type of the partition key). Instead, the ``token`` method should always
be used for range queries on the partition key (see :ref:`WHERE clauses <where-clause>`).
3.0.3
^^^^^
- Support for custom :ref:`secondary indexes <secondary-indexes>` has been added.
3.0.2
^^^^^
- Type validation for the :ref:`constants <constants>` has been fixed. For instance, the implementation used to allow
``'2'`` as a valid value for an ``int`` column (interpreting it has the equivalent of ``2``), or ``42`` as a valid
``blob`` value (in which case ``42`` was interpreted as an hexadecimal representation of the blob). This is no longer
the case, type validation of constants is now more strict. See the :ref:`data types <data-types>` section for details
on which constant is allowed for which type.
- The type validation fixed of the previous point has lead to the introduction of blobs constants to allow the input of
blobs. Do note that while the input of blobs as strings constant is still supported by this version (to allow smoother
transition to blob constant), it is now deprecated and will be removed by a future version. If you were using strings
as blobs, you should thus update your client code ASAP to switch blob constants.
- A number of functions to convert native types to blobs have also been introduced. Furthermore the token function is
now also allowed in select clauses. See the :ref:`section on functions <cql-functions>` for details.
3.0.1
^^^^^
- Date strings (and timestamps) are no longer accepted as valid ``timeuuid`` values. Doing so was a bug in the sense
that date string are not valid ``timeuuid``, and it was thus resulting in `confusing behaviors
<https://issues.apache.org/jira/browse/CASSANDRA-4936>`__. However, the following new methods have been added to help
working with ``timeuuid``: ``now``, ``minTimeuuid``, ``maxTimeuuid`` ,
``dateOf`` and ``unixTimestampOf``.
- Float constants now support the exponent notation. In other words, ``4.2E10`` is now a valid floating point value.
Versioning
^^^^^^^^^^
Versioning of the CQL language adheres to the `Semantic Versioning <http://semver.org>`__ guidelines. Versions take the
form X.Y.Z where X, Y, and Z are integer values representing major, minor, and patch level respectively. There is no
correlation between Cassandra release versions and the CQL language version.
========= =============================================================================================================
version description
========= =============================================================================================================
Major The major version *must* be bumped when backward incompatible changes are introduced. This should rarely
occur.
Minor Minor version increments occur when new, but backward compatible, functionality is introduced.
Patch The patch version is incremented when bugs are fixed.
========= =============================================================================================================

677
doc/source/cql/ddl.rst Normal file
View File

@ -0,0 +1,677 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
.. _data-definition:
Data Definition
---------------
CQL stores data in *tables*, whose schema defines the layout of said data in the table, and those tables are grouped in
*keyspaces*. A keyspace defines a number of options that applies to all the tables it contains, most prominently of
which is the :ref:`replication strategy <replication-strategy>` used by the keyspace. It is generally encouraged to use
one keyspace by *application*, and thus many cluster may define only one keyspace.
This section describes the statements used to create, modify, and remove those keyspace and tables.
Common definitions
^^^^^^^^^^^^^^^^^^
The names of the keyspaces and tables are defined by the following grammar:
.. productionlist::
keyspace_name: `name`
table_name: [ `keyspace_name` '.' ] `name`
name: `unquoted_name` | `quoted_name`
unquoted_name: re('[a-zA-Z_0-9]{1, 48}')
quoted_name: '"' `unquoted_name` '"'
Both keyspace and table name should be comprised of only alphanumeric characters, cannot be empty and are limited in
size to 48 characters (that limit exists mostly to avoid filenames (which may include the keyspace and table name) to go
over the limits of certain file systems). By default, keyspace and table names are case insensitive (``myTable`` is
equivalent to ``mytable``) but case sensitivity can be forced by using double-quotes (``"myTable"`` is different from
``mytable``).
Further, a table is always part of a keyspace and a table name can be provided fully-qualified by the keyspace it is
part of. If is is not fully-qualified, the table is assumed to be in the *current* keyspace (see :ref:`USE statement
<use-statement>`).
Further, the valid names for columns is simply defined as:
.. productionlist::
column_name: `identifier`
We also define the notion of statement options for use in the following section:
.. productionlist::
options: `option` ( AND `option` )*
option: `identifier` '=' ( `identifier` | `constant` | `map_literal` )
.. _create-keyspace-statement:
CREATE KEYSPACE
^^^^^^^^^^^^^^^
A keyspace is created using a ``CREATE KEYSPACE`` statement:
.. productionlist::
create_keyspace_statement: CREATE KEYSPACE [ IF NOT EXISTS ] `keyspace_name` WITH `options`
For instance::
CREATE KEYSPACE Excelsior
WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 3};
CREATE KEYSPACE Excalibur
WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1' : 1, 'DC2' : 3}
AND durable_writes = false;
The supported ``options`` are:
=================== ========== =========== ========= ===================================================================
name kind mandatory default description
=================== ========== =========== ========= ===================================================================
``replication`` *map* yes The replication strategy and options to use for the keyspace (see
details below).
``durable_writes`` *simple* no true Whether to use the commit log for updates on this keyspace
(disable this option at your own risk!).
=================== ========== =========== ========= ===================================================================
The ``replication`` property is mandatory and must at least contains the ``'class'`` sub-option which defines the
:ref:`replication strategy <replication-strategy>` class to use. The rest of the sub-options depends on what replication
strategy is used. By default, Cassandra support the following ``'class'``:
- ``'SimpleStrategy'``: A simple strategy that defines a replication factor for the whole cluster. The only sub-options
supported is ``'replication_factor'`` to define that replication factor and is mandatory.
- ``'NetworkTopologyStrategy'``: A replication strategy that allows to set the replication factor independently for
each data-center. The rest of the sub-options are key-value pairs where a key is a data-center name and its value is
the associated replication factor.
Attempting to create a keyspace that already exists will return an error unless the ``IF NOT EXISTS`` option is used. If
it is used, the statement will be a no-op if the keyspace already exists.
.. _use-statement:
USE
^^^
The ``USE`` statement allows to change the *current* keyspace (for the *connection* on which it is executed). A number
of objects in CQL are bound to a keyspace (tables, user-defined types, functions, ...) and the current keyspace is the
default keyspace used when those objects are referred without a fully-qualified name (that is, without being prefixed a
keyspace name). A ``USE`` statement simply takes the keyspace to use as current as argument:
.. productionlist::
use_statement: USE `keyspace_name`
.. _alter-keyspace-statement:
ALTER KEYSPACE
^^^^^^^^^^^^^^
An ``ALTER KEYSPACE`` statement allows to modify the options of a keyspace:
.. productionlist::
alter_keyspace_statement: ALTER KEYSPACE `keyspace_name` WITH `options`
For instance::
ALTER KEYSPACE Excelsior
WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 4};
The supported options are the same than for :ref:`creating a keyspace <create-keyspace-statement>`.
.. _drop-keyspace-statement:
DROP KEYSPACE
^^^^^^^^^^^^^
Dropping a keyspace can be done using the ``DROP KEYSPACE`` statement:
.. productionlist::
drop_keyspace_statement: DROP KEYSPACE [ IF EXISTS ] `keyspace_name`
For instance::
DROP KEYSPACE Excelsior;
Dropping a keyspace results in the immediate, irreversible removal of that keyspace, including all the tables, UTD and
functions in it, and all the data contained in those tables.
If the keyspace does not exists, the statement will return an error, unless ``IF EXISTS`` is used in which case the
operation is a no-op.
.. _create-table-statement:
CREATE TABLE
^^^^^^^^^^^^
Creating a new table uses the ``CREATE TABLE`` statement:
.. productionlist::
create_table_statement: CREATE TABLE [ IF NOT EXISTS ] `table_name`
: '('
: `column_definition`
: ( ',' `column_definition` )*
: [ ',' PRIMARY KEY '(' `primary_key` ')' ]
: ')' [ WITH `table_options` ]
column_definition: `column_name` `cql_type` [ STATIC ] [ PRIMARY KEY]
primary_key: `partition_key` [ ',' `clustering_columns` ]
partition_key: `column_name`
: | '(' `column_name` ( ',' `column_name` )* ')'
clustering_columns: `column_name` ( ',' `column_name` )*
table_options: COMPACT STORAGE [ AND `table_options` ]
: | CLUSTERING ORDER BY '(' `clustering_order` ')' [ AND `table_options` ]
: | `options`
clustering_order: `column_name` (ASC | DESC) ( ',' `column_name` (ASC | DESC) )*
For instance::
CREATE TABLE monkeySpecies (
species text PRIMARY KEY,
common_name text,
population varint,
average_size int
) WITH comment='Important biological records'
AND read_repair_chance = 1.0;
CREATE TABLE timeline (
userid uuid,
posted_month int,
posted_time uuid,
body text,
posted_by text,
PRIMARY KEY (userid, posted_month, posted_time)
) WITH compaction = { 'class' : 'LeveledCompactionStrategy' };
CREATE TABLE loads (
machine inet,
cpu int,
mtime timeuuid,
load float,
PRIMARY KEY ((machine, cpu), mtime)
) WITH CLUSTERING ORDER BY (mtime DESC);
A CQL table has a name and is composed of a set of *rows*. Creating a table amounts to defining which :ref:`columns
<column-definition>` the rows will be composed, which of those columns compose the :ref:`primary key <primary-key>`, as
well as optional :ref:`options <create-table-options>` for the table.
Attempting to create an already existing table will return an error unless the ``IF NOT EXISTS`` directive is used. If
it is used, the statement will be a no-op if the table already exists.
.. _column-definition:
Column definitions
~~~~~~~~~~~~~~~~~~
Every rows in a CQL table has a set of predefined columns defined at the time of the table creation (or added later
using an :ref:`alter statement<alter-table-statement>`).
A :token:`column_definition` is primarily comprised of the name of the column defined and it's :ref:`type <data-types>`,
which restrict which values are accepted for that column. Additionally, a column definition can have the following
modifiers:
``STATIC``
it declares the column as being a :ref:`static column <static-columns>`.
``PRIMARY KEY``
it declares the column as being the sole component of the :ref:`primary key <primary-key>` of the table.
.. _static-columns:
Static columns
``````````````
Some columns can be declared as ``STATIC`` in a table definition. A column that is static will be “shared” by all the
rows belonging to the same partition (having the same :ref:`partition key <partition-key>`). For instance::
CREATE TABLE t (
pk int,
t int,
v text,
s text static,
PRIMARY KEY (pk, t)
);
INSERT INTO t (pk, t, v, s) VALUES (0, 0, 'val0', 'static0');
INSERT INTO t (pk, t, v, s) VALUES (0, 1, 'val1', 'static1');
SELECT * FROM t;
pk | t | v | s
----+---+--------+-----------
0 | 0 | 'val0' | 'static1'
0 | 1 | 'val1' | 'static1'
As can be seen, the ``s`` value is the same (``static1``) for both of the row in the partition (the partition key in
that example being ``pk``, both rows are in that same partition): the 2nd insertion has overridden the value for ``s``.
The use of static columns as the following restrictions:
- tables with the ``COMPACT STORAGE`` option (see below) cannot use them.
- a table without clustering columns cannot have static columns (in a table without clustering columns, every partition
has only one row, and so every column is inherently static).
- only non ``PRIMARY KEY`` columns can be static.
.. _primary-key:
The Primary key
~~~~~~~~~~~~~~~
Within a table, a row is uniquely identified by its ``PRIMARY KEY``, and hence all table **must** define a PRIMARY KEY
(and only one). A ``PRIMARY KEY`` definition is composed of one or more of the columns defined in the table.
Syntactically, the primary key is defined the keywords ``PRIMARY KEY`` followed by comma-separated list of the column
names composing it within parenthesis, but if the primary key has only one column, one can alternatively follow that
column definition by the ``PRIMARY KEY`` keywords. The order of the columns in the primary key definition matter.
A CQL primary key is composed of 2 parts:
- the :ref:`partition key <partition-key>` part. It is the first component of the primary key definition. It can be a
single column or, using additional parenthesis, can be multiple columns. A table always have at least a partition key,
the smallest possible table definition is::
CREATE TABLE t (k text PRIMARY KEY);
- the :ref:`clustering columns <clustering-columns>`. Those are the columns after the first component of the primary key
definition, and the order of those columns define the *clustering order*.
Some example of primary key definition are:
- ``PRIMARY KEY (a)``: ``a`` is the partition key and there is no clustering columns.
- ``PRIMARY KEY (a, b, c)`` : ``a`` is the partition key and ``b`` and ``c`` are the clustering columns.
- ``PRIMARY KEY ((a, b), c)`` : ``a`` and ``b`` compose the partition key (this is often called a *composite* partition
key) and ``c`` is the clustering column.
.. _partition-key:
The partition key
`````````````````
Within a table, CQL defines the notion of a *partition*. A partition is simply the set of rows that share the same value
for their partition key. Note that if the partition key is composed of multiple columns, then rows belong to the same
partition only they have the same values for all those partition key column. So for instance, given the following table
definition and content::
CREATE TABLE t (
a int,
b int,
c int,
d int,
PRIMARY KEY ((a, b), c, d)
);
SELECT * FROM t;
a | b | c | d
---+---+---+---
0 | 0 | 0 | 0 // row 1
0 | 0 | 1 | 1 // row 2
0 | 1 | 2 | 2 // row 3
0 | 1 | 3 | 3 // row 4
1 | 1 | 4 | 4 // row 5
``row 1`` and ``row 2`` are in the same partition, ``row 3`` and ``row 4`` are also in the same partition (but a
different one) and ``row 5`` is in yet another partition.
Note that a table always has a partition key, and that if the table has no :ref:`clustering columns
<clustering-columns>`, then every partition of that table is only comprised of a single row (since the primary key
uniquely identifies rows and the primary key is equal to the partition key if there is no clustering columns).
The most important property of partition is that all the rows belonging to the same partition are guarantee to be stored
on the same set of replica nodes. In other words, the partition key of a table defines which of the rows will be
localized together in the Cluster, and it is thus important to choose your partition key wisely so that rows that needs
to be fetch together are in the same partition (so that querying those rows together require contacting a minimum of
nodes).
Please note however that there is a flip-side to this guarantee: as all rows sharing a partition key are guaranteed to
be stored on the same set of replica node, a partition key that groups too much data can create a hotspot.
Another useful property of a partition is that when writing data, all the updates belonging to a single partition are
done *atomically* and in *isolation*, which is not the case across partitions.
The proper choice of the partition key and clustering columns for a table is probably one of the most important aspect
of data modeling in Cassandra, and it largely impact which queries can be performed, and how efficiently they are.
.. _clustering-columns:
The clustering columns
``````````````````````
The clustering columns of a table defines the clustering order for the partition of that table. For a given
:ref:`partition <partition-key>`, all the rows are physically ordered inside Cassandra by that clustering order. For
instance, given::
CREATE TABLE t (
a int,
b int,
c int,
PRIMARY KEY (a, c, d)
);
SELECT * FROM t;
a | b | c
---+---+---
0 | 0 | 4 // row 1
0 | 1 | 9 // row 2
0 | 2 | 2 // row 3
0 | 3 | 3 // row 4
then the rows (which all belong to the same partition) are all stored internally in the order of the values of their
``b`` column (the order they are displayed above). So where the partition key of the table allows to group rows on the
same replica set, the clustering columns controls how those rows are stored on the replica. That sorting allows the
retrieval of a range of rows within a partition (for instance, in the example above, ``SELECT * FROM t WHERE a = 0 AND b
> 1 and b <= 3``) very efficient.
.. _create-table-options:
Table options
~~~~~~~~~~~~~
A CQL table has a number of options that can be set at creation (and, for most of them, :ref:`altered
<alter-table-statement>` later). These options are specified after the ``WITH`` keyword.
Amongst those options, two important ones cannot be changed after creation and influence which queries can be done
against the table: the ``COMPACT STORAGE`` option and the ``CLUSTERING ORDER`` option. Those, as well as the other
options of a table are described in the following sections.
.. _compact-tables:
Compact tables
``````````````
.. warning:: Since Cassandra 3.0, compact tables have the exact same layout internally than non compact ones (for the
same schema obviously), and declaring a table compact **only** creates artificial limitations on the table definition
and usage that are necessary to ensure backward compatibility with the deprecated Thrift API. And as ``COMPACT
STORAGE`` cannot, as of Cassandra |version|, be removed, it is strongly discouraged to create new table with the
``COMPACT STORAGE`` option.
A *compact* table is one defined with the ``COMPACT STORAGE`` option. This option is mainly targeted towards backward
compatibility for definitions created before CQL version 3 (see `www.datastax.com/dev/blog/thrift-to-cql3
<http://www.datastax.com/dev/blog/thrift-to-cql3>`__ for more details) and shouldn't be used for new tables. Declaring a
table with this option creates limitations for the table which are largely arbitrary but necessary for backward
compatibility with the (deprecated) Thrift API. Amongst those limitation:
- a compact table cannot use collections nor static columns.
- if a compact table has at least one clustering column, then it must have *exactly* one column outside of the primary
key ones. This imply you cannot add or remove columns after creation in particular.
- a compact table is limited in the indexes it can create, and no materialized view can be created on it.
.. _clustering-order:
Reversing the clustering order
``````````````````````````````
The clustering order of a table is defined by the :ref:`clustering columns <clustering-columns>` of that table. By
default, that ordering is based on natural order of those clustering order, but the ``CLUSTERING ORDER`` allows to
change that clustering order to use the *reverse* natural order for some (potentially all) of the columns.
The ``CLUSTERING ORDER`` option takes the comma-separated list of the clustering column, each with a ``ASC`` (for
*ascendant*, e.g. the natural order) or ``DESC`` (for *descendant*, e.g. the reverse natural order). Note in particular
that the default (if the ``CLUSTERING ORDER`` option is not used) is strictly equivalent to using the option with all
clustering columns using the ``ASC`` modifier.
Note that this option is basically a hint for the storage engine to change the order in which it stores the row but it
has 3 visible consequences:
# it limits which ``ORDER BY`` clause are allowed for :ref:`selects <select-statement>` on that table. You can only
order results by the clustering order or the reverse clustering order. Meaning that if a table has 2 clustering column
``a`` and ``b`` and you defined ``WITH CLUSTERING ORDER (a DESC, b ASC)``, then in queries you will be allowed to use
``ORDER BY (a DESC, b ASC)`` and (reverse clustering order) ``ORDER BY (a ASC, b DESC)`` but **not** ``ORDER BY (a
ASC, b ASC)`` (nor ``ORDER BY (a DESC, b DESC)``).
# it also change the default order of results when queried (if no ``ORDER BY`` is provided). Results are always returned
in clustering order (within a partition).
# it has a small performance impact on some queries as queries in reverse clustering order are slower than the one in
forward clustering order. In practice, this means that if you plan on querying mostly in the reverse natural order of
your columns (which is common with time series for instance where you often want data from the newest to the oldest),
it is an optimization to declare a descending clustering order.
.. _create-table-general-options:
Other table options
```````````````````
.. todo:: review (misses cdc if nothing else) and link to proper categories when appropriate (compaction for instance)
A table supports the following options:
+--------------------------------+----------+-------------+-----------------------------------------------------------+
| option | kind | default | description |
+================================+==========+=============+===========================================================+
| ``comment`` | *simple* | none | A free-form, human-readable comment. |
+--------------------------------+----------+-------------+-----------------------------------------------------------+
| ``read_repair_chance`` | *simple* | 0.1 | The probability with which to query extra nodes (e.g. |
| | | | more nodes than required by the consistency level) for |
| | | | the purpose of read repairs. |
+--------------------------------+----------+-------------+-----------------------------------------------------------+
| ``dclocal_read_repair_chance`` | *simple* | 0 | The probability with which to query extra nodes (e.g. |
| | | | more nodes than required by the consistency level) |
| | | | belonging to the same data center than the read |
| | | | coordinator for the purpose of read repairs. |
+--------------------------------+----------+-------------+-----------------------------------------------------------+
| ``gc_grace_seconds`` | *simple* | 864000 | Time to wait before garbage collecting tombstones |
| | | | (deletion markers). |
+--------------------------------+----------+-------------+-----------------------------------------------------------+
| ``bloom_filter_fp_chance`` | *simple* | 0.00075 | The target probability of false positive of the sstable |
| | | | bloom filters. Said bloom filters will be sized to provide|
| | | | the provided probability (thus lowering this value impact |
| | | | the size of bloom filters in-memory and on-disk) |
+--------------------------------+----------+-------------+-----------------------------------------------------------+
| ``default_time_to_live`` | *simple* | 0 | The default expiration time (“TTL”) in seconds for a |
| | | | table. |
+--------------------------------+----------+-------------+-----------------------------------------------------------+
| ``compaction`` | *map* | *see below* | :ref:`Compaction options <cql-compaction-options>`. |
+--------------------------------+----------+-------------+-----------------------------------------------------------+
| ``compression`` | *map* | *see below* | :ref:`Compression options <cql-compression-options>`. |
+--------------------------------+----------+-------------+-----------------------------------------------------------+
| ``caching`` | *map* | *see below* | :ref:`Caching options <cql-caching-options>`. |
+--------------------------------+----------+-------------+-----------------------------------------------------------+
.. _cql-compaction-options:
Compaction options
##################
The ``compaction`` options must at least define the ``'class'`` sub-option, that defines the compaction strategy class
to use. The default supported class are ``'SizeTieredCompactionStrategy'`` (:ref:`STCS <STCS>`),
``'LeveledCompactionStrategy'`` (:ref:`LCS <LCS>`) and ``'TimeWindowCompactionStrategy'`` (:ref:`TWCS <TWCS>`) (the
``'DateTieredCompactionStrategy'`` is also supported but is deprecated and ``'TimeWindowCompactionStrategy'`` should be
preferred instead). Custom strategy can be provided by specifying the full class name as a :ref:`string constant
<constants>`.
All default strategies support a number of :ref:`common options <compaction-options>`, as well as options specific to
the strategy chosen (see the section corresponding to your strategy for details: :ref:`STCS <stcs-options>`, :ref:`LCS
<lcs-options>` and :ref:`TWCS <TWCS>`).
.. _cql-compression-options:
Compression options
###################
The ``compression`` options define if and how the sstables of the table are compressed. The following sub-options are
available:
========================= =============== =============================================================================
Option Default Description
========================= =============== =============================================================================
``class`` LZ4Compressor The compression algorithm to use. Default compressor are: LZ4Compressor,
SnappyCompressor and DeflateCompressor. Use ``'enabled' : false`` to disable
compression. Custom compressor can be provided by specifying the full class
name as a “string constant”:#constants.
``enabled`` true Enable/disable sstable compression.
``chunk_length_in_kb`` 64KB On disk SSTables are compressed by block (to allow random reads). This
defines the size (in KB) of said block. Bigger values may improve the
compression rate, but increases the minimum size of data to be read from disk
for a read
``crc_check_chance`` 1.0 When compression is enabled, each compressed block includes a checksum of
that block for the purpose of detecting disk bitrot and avoiding the
propagation of corruption to other replica. This option defines the
probability with which those checksums are checked during read. By default
they are always checked. Set to 0 to disable checksum checking and to 0.5 for
instance to check them every other read |
========================= =============== =============================================================================
.. _cql-caching-options:
Caching options
###############
The ``caching`` options allows to configure both the *key cache* and the *row cache* for the table. The following
sub-options are available:
======================== ========= ====================================================================================
Option Default Description
======================== ========= ====================================================================================
``keys`` ALL Whether to cache keys (“key cache”) for this table. Valid values are: ``ALL`` and
``NONE``.
``rows_per_partition`` NONE The amount of rows to cache per partition (“row cache”). If an integer ``n`` is
specified, the first ``n`` queried rows of a partition will be cached. Other
possible options are ``ALL``, to cache all rows of a queried partition, or ``NONE``
to disable row caching.
======================== ========= ====================================================================================
Other considerations:
#####################
- Adding new columns (see ``ALTER TABLE`` below) is a constant time operation. There is thus no need to try to
anticipate future usage when creating a table.
.. _alter-table-statement:
ALTER TABLE
^^^^^^^^^^^
Altering an existing table uses the ``ALTER TABLE`` statement:
.. productionlist::
alter_table_statement: ALTER TABLE `table_name` `alter_table_instruction`
alter_table_instruction: ALTER `column_name` TYPE `cql_type`
: | ADD `column_name` `cql_type` ( ',' `column_name` `cql_type` )*
: | DROP `column_name` ( `column_name` )*
: | WITH `options`
For instance::
ALTER TABLE addamsFamily ALTER lastKnownLocation TYPE uuid;
ALTER TABLE addamsFamily ADD gravesite varchar;
ALTER TABLE addamsFamily
WITH comment = 'A most excellent and useful table'
AND read_repair_chance = 0.2;
The ``ALTER TABLE`` statement can:
- Change the type of one of the column in the table (through the ``ALTER`` instruction). Note that the type of a column
cannot be changed arbitrarily. The change of type should be such that any value of the previous type should be a valid
value of the new type. Further, for :ref:`clustering columns <clustering-columns>` and columns on which a secondary
index is defined, the new type must sort values in the same way the previous type does. See the :ref:`type
compatibility table <alter-table-type-compatibility>` below for detail on which type changes are accepted.
- Add new column(s) to the table (through the ``ADD`` instruction). Note that the primary key of a table cannot be
changed and thus newly added column will, by extension, never be part of the primary key. Also note that :ref:`compact
tables <compact-tables>` have restrictions regarding column addition. Note that this is constant (in the amount of
data the cluster contains) time operation.
- Remove column(s) from the table. This drops both the column and all its content, but note that while the column
becomes immediately unavailable, its content is only removed lazily during compaction. Please also see the warnings
below. Due to lazy removal, the altering itself is a constant (in the amount of data removed or contained in the
cluster) time operation.
- Change some of the table options (through the ``WITH`` instruction). The :ref:`supported options
<create-table-options>` are the same that when creating a table (outside of ``COMPACT STORAGE`` and ``CLUSTERING
ORDER`` that cannot be changed after creation). Note that setting any ``compaction`` sub-options has the effect of
erasing all previous ``compaction`` options, so you need to re-specify all the sub-options if you want to keep them.
The same note applies to the set of ``compression`` sub-options.
.. warning:: Dropping a column assumes that the timestamps used for the value of this column are "real" timestamp in
microseconds. Using "real" timestamps in microseconds is the default is and is **strongly** recommended but as
Cassandra allows the client to provide any timestamp on any table it is theoretically possible to use another
convention. Please be aware that if you do so, dropping a column will not work correctly.
.. warning:: Once a column is dropped, it is allowed to re-add a column with the same name than the dropped one
**unless** the type of the dropped column was a (non-frozen) column (due to an internal technical limitation).
.. _alter-table-type-compatibility:
CQL type compatibility:
~~~~~~~~~~~~~~~~~~~~~~~
CQL data types may be converted only as the following table.
+-------------------------------------------------------+--------------------+
| Existing type | Can be altered to: |
+=======================================================+====================+
| timestamp | bigint |
+-------------------------------------------------------+--------------------+
| ascii, bigint, boolean, date, decimal, double, float, | blob |
| inet, int, smallint, text, time, timestamp, timeuuid, | |
| tinyint, uuid, varchar, varint | |
+-------------------------------------------------------+--------------------+
| int | date |
+-------------------------------------------------------+--------------------+
| ascii, varchar | text |
+-------------------------------------------------------+--------------------+
| bigint | time |
+-------------------------------------------------------+--------------------+
| bigint | timestamp |
+-------------------------------------------------------+--------------------+
| timeuuid | uuid |
+-------------------------------------------------------+--------------------+
| ascii, text | varchar |
+-------------------------------------------------------+--------------------+
| bigint, int, timestamp | varint |
+-------------------------------------------------------+--------------------+
Clustering columns have stricter requirements, only the following conversions are allowed:
+------------------------+----------------------+
| Existing type | Can be altered to |
+========================+======================+
| ascii, text, varchar | blob |
+------------------------+----------------------+
| ascii, varchar | text |
+------------------------+----------------------+
| ascii, text | varchar |
+------------------------+----------------------+
.. _drop-table-statement:
DROP TABLE
^^^^^^^^^^
Dropping a table uses the ``DROP TABLE`` statement:
.. productionlist::
drop_table_statement: DROP TABLE [ IF EXISTS ] `table_name`
Dropping a table results in the immediate, irreversible removal of the table, including all data it contains.
If the table does not exist, the statement will return an error, unless ``IF EXISTS`` is used in which case the
operation is a no-op.
.. _truncate-statement:
TRUNCATE
^^^^^^^^
A table can be truncated using the ``TRUNCATE`` statement:
.. productionlist::
truncate_statement: TRUNCATE [ TABLE ] `table_name`
Note that ``TRUNCATE TABLE foo`` is allowed for consistency with other DDL statements but tables are the only object
that can be truncated currently and so the ``TABLE`` keyword can be omitted.
Truncating a table permanently removes all existing data from the table, but without removing the table itself.

View File

@ -0,0 +1,230 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. _UUID: https://en.wikipedia.org/wiki/Universally_unique_identifier
Definitions
-----------
.. _conventions:
Conventions
^^^^^^^^^^^
To aid in specifying the CQL syntax, we will use the following conventions in this document:
- Language rules will be given in an informal `BNF variant
<http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form#Variants>`_ notation. In particular, we'll use square brakets
(``[ item ]``) for optional items, ``*`` and ``+`` for repeated items (where ``+`` imply at least one).
- The grammar will also use the following convention for convenience: non-terminal term will be lowercase (and link to
their definition) while terminal keywords will be provided "all caps". Note however that keywords are
:ref:`identifiers` and are thus case insensitive in practice. We will also define some early construction using
regexp, which we'll indicate with ``re(<some regular expression>)``.
- The grammar is provided for documentation purposes and leave some minor details out. For instance, the comma on the
last column definition in a ``CREATE TABLE`` statement is optional but supported if present even though the grammar in
this document suggests otherwise. Also, not everything accepted by the grammar is necessarily valid CQL.
- References to keywords or pieces of CQL code in running text will be shown in a ``fixed-width font``.
.. _identifiers:
Identifiers and keywords
^^^^^^^^^^^^^^^^^^^^^^^^
The CQL language uses *identifiers* (or *names*) to identify tables, columns and other objects. An identifier is a token
matching the regular expression ``[a-zA-Z][a-zA-Z0-9_]*``.
A number of such identifiers, like ``SELECT`` or ``WITH``, are *keywords*. They have a fixed meaning for the language
and most are reserved. The list of those keywords can be found in :ref:`appendix-A`.
Identifiers and (unquoted) keywords are case insensitive. Thus ``SELECT`` is the same than ``select`` or ``sElEcT``, and
``myId`` is the same than ``myid`` or ``MYID``. A convention often used (in particular by the samples of this
documentation) is to use upper case for keywords and lower case for other identifiers.
There is a second kind of identifiers called *quoted identifiers* defined by enclosing an arbitrary sequence of
characters (non empty) in double-quotes(``"``). Quoted identifiers are never keywords. Thus ``"select"`` is not a
reserved keyword and can be used to refer to a column (note that using this is particularly advised), while ``select``
would raise a parsing error. Also, contrarily to unquoted identifiers and keywords, quoted identifiers are case
sensitive (``"My Quoted Id"`` is *different* from ``"my quoted id"``). A fully lowercase quoted identifier that matches
``[a-zA-Z][a-zA-Z0-9_]*`` is however *equivalent* to the unquoted identifier obtained by removing the double-quote (so
``"myid"`` is equivalent to ``myid`` and to ``myId`` but different from ``"myId"``). Inside a quoted identifier, the
double-quote character can be repeated to escape it, so ``"foo "" bar"`` is a valid identifier.
.. note:: *quoted identifiers* allows to declare columns with arbitrary names, and those can sometime clash with
specific names used by the server. For instance, when using conditional update, the server will respond with a
result-set containing a special result named ``"[applied]"``. If youve declared a column with such a name, this
could potentially confuse some tools and should be avoided. In general, unquoted identifiers should be preferred but
if you use quoted identifiers, it is strongly advised to avoid any name enclosed by squared brackets (like
``"[applied]"``) and any name that looks like a function call (like ``"f(x)"``).
More formally, we have:
.. productionlist::
identifier: `unquoted_identifier` | `quoted_identifier`
unquoted_identifier: re('[a-zA-Z][a-zA-Z0-9_]*')
quoted_identifier: '"' (any character where " can appear if doubled)+ '"'
.. _constants:
Constants
^^^^^^^^^
CQL defines the following kind of *constants*:
.. productionlist::
constant: `string` | `integer` | `float` | `boolean` | `uuid` | `blob` | NULL
string: '\'' (any character where ' can appear if doubled)+ '\''
: '$$' (any character other than '$$') '$$'
integer: re('-?[0-9]+')
float: re('-?[0-9]+(\.[0-9]*)?([eE][+-]?[0-9+])?') | NAN | INFINITY
boolean: TRUE | FALSE
uuid: `hex`{8}-`hex`{4}-`hex`{4}-`hex`{4}-`hex`{12}
hex: re("[0-9a-fA-F]")
blob: '0' ('x' | 'X') `hex`+
In other words:
- A string constant is an arbitrary sequence of characters enclosed by single-quote(``'``). A single-quote
can be included by repeating it, e.g. ``'It''s raining today'``. Those are not to be confused with quoted
:ref:`identifiers` that use double-quotes. Alternatively, a string can be defined by enclosing the arbitrary sequence
of characters by two dollar characters, in which case single-quote can be use without escaping (``$$It's raining
today$$``). That latter form is often used when defining :ref:`user-defined functions <udfs>` to avoid having to
escape single-quote characters in function body (as they are more likely to occur than ``$$``).
- Integer, float and boolean constant are defined as expected. Note however than float allows the special ``NaN`` and
``Infinity`` constants.
- CQL supports UUID_ constants.
- Blobs content are provided in hexadecimal and prefixed by ``0x``.
- The special ``NULL`` constant denotes the absence of value.
For how these constants are typed, see the :ref:`data-types` section.
Terms
^^^^^
CQL has the notion of a *term*, which denotes the kind of values that CQL support. Terms are defined by:
.. productionlist::
term: `constant` | `literal` | `function_call` | `type_hint` | `bind_marker`
literal: `collection_literal` | `udt_literal` | `tuple_literal`
function_call: `identifier` '(' [ `term` (',' `term`)* ] ')'
type_hint: '(' `cql_type` `)` term
bind_marker: '?' | ':' `identifier`
A term is thus one of:
- A :ref:`constant <constants>`.
- A literal for either :ref:`a collection <collections>`, :ref:`a user-defined type <udts>` or :ref:`a tuple <tuples>`
(see the linked sections for details).
- A function call: see :ref:`the section on functions <cql-functions>` for details on which :ref:`native function
<native-functions>` exists and how to define your own :ref:`user-defined ones <udfs>`.
- A *type hint*: see the :ref:`related section <type-hints>` for details.
- A bind marker, which denotes a variable to be bound at execution time. See the section on :ref:`prepared-statements`
for details. A bind marker can be either anonymous (``?``) or named (``:some_name``). The latter form provides a more
convenient way to refer to the variable for binding it and should generally be preferred.
Comments
^^^^^^^^
A comment in CQL is a line beginning by either double dashes (``--``) or double slash (``//``).
Multi-line comments are also supported through enclosure within ``/*`` and ``*/`` (but nesting is not supported).
::
— This is a comment
// This is a comment too
/* This is
a multi-line comment */
Statements
^^^^^^^^^^
CQL consists of statements that can be divided in the following categories:
- :ref:`data-definition` statements, to define and change how the data is stored (keyspaces and tables).
- :ref:`data-manipulation` statements, for selecting, inserting and deleting data.
- :ref:`secondary-indexes` statements.
- :ref:`materialized-views` statements.
- :ref:`cql-roles` statements.
- :ref:`cql-permissions` statements.
- :ref:`User-Defined Functions <udfs>` statements.
- :ref:`udts` statements.
- :ref:`cql-triggers` statements.
All the statements are listed below and are described in the rest of this documentation (see links above):
.. productionlist::
cql_statement: `statement` [ ';' ]
statement: `ddl_statement`
: | `dml_statement`
: | `secondary_index_statement`
: | `materialized_view_statement`
: | `role_or_permission_statement`
: | `udf_statement`
: | `udt_statement`
: | `trigger_statement`
ddl_statement: `use_statement`
: | `create_keyspace_statement`
: | `alter_keyspace_statement`
: | `drop_keyspace_statement`
: | `create_table_statement`
: | `alter_table_statement`
: | `drop_table_statement`
: | `truncate_statement`
dml_statement: `select_statement`
: | `insert_statement`
: | `update_statement`
: | `delete_statement`
: | `batch_statement`
secondary_index_statement: `create_index_statement`
: | `drop_index_statement`
materialized_view_statement: `create_materialized_view_statement`
: | `drop_materialized_view_statement`
role_or_permission_statement: `create_role_statement`
: | `alter_role_statement`
: | `drop_role_statement`
: | `grant_role_statement`
: | `revoke_role_statement`
: | `list_roles_statement`
: | `grant_permission_statement`
: | `revoke_permission_statement`
: | `list_permissions_statement`
: | `create_user_statement`
: | `alter_user_statement`
: | `drop_user_statement`
: | `list_users_statement`
udf_statement: `create_function_statement`
: | `drop_function_statement`
: | `create_aggregate_statement`
: | `drop_aggregate_statement`
udt_statement: `create_type_statement`
: | `alter_type_statement`
: | `drop_type_statement`
trigger_statement: `create_trigger_statement`
: | `drop_trigger_statement`
.. _prepared-statements:
Prepared Statements
^^^^^^^^^^^^^^^^^^^
CQL supports *prepared statements*. Prepared statements are an optimization that allows to parse a query only once but
execute it multiple times with different concrete values.
Any statement that uses at least one bind marker (see :token:`bind_marker`) will need to be *prepared*. After which the statement
can be *executed* by provided concrete values for each of its marker. The exact details of how a statement is prepared
and then executed depends on the CQL driver used and you should refer to your driver documentation.

499
doc/source/cql/dml.rst Normal file
View File

@ -0,0 +1,499 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
.. _data-manipulation:
Data Manipulation
-----------------
This section describes the statements supported by CQL to insert, update, delete and query data.
.. _select-statement:
SELECT
^^^^^^
Querying data from data is done using a ``SELECT`` statement:
.. productionlist::
select_statement: SELECT [ JSON | DISTINCT ] ( `select_clause` | '*' )
: FROM `table_name`
: [ WHERE `where_clause` ]
: [ ORDER BY `ordering_clause` ]
: [ PER PARTITION LIMIT (`integer` | `bind_marker`) ]
: [ LIMIT (`integer` | `bind_marker`) ]
: [ ALLOW FILTERING ]
select_clause: `selector` [ AS `identifier` ] ( ',' `selector` [ AS `identifier` ] )
selector: `column_name`
: | `term`
: | CAST '(' `selector` AS `cql_type` ')'
: | `function_name` '(' [ `selector` ( ',' `selector` )* ] ')'
: | COUNT '(' '*' ')'
where_clause: `relation` ( AND `relation` )*
relation: `column_name` `operator` `term`
: '(' `column_name` ( ',' `column_name` )* ')' `operator` `tuple_literal`
: TOKEN '(' `column_name` ( ',' `column_name` )* ')' `operator` `term`
operator: '=' | '<' | '>' | '<=' | '>=' | '!=' | IN | CONTAINS | CONTAINS KEY
ordering_clause: `column_name` [ ASC | DESC ] ( ',' `column_name` [ ASC | DESC ] )*
For instance::
SELECT name, occupation FROM users WHERE userid IN (199, 200, 207);
SELECT JSON name, occupation FROM users WHERE userid = 199;
SELECT name AS user_name, occupation AS user_occupation FROM users;
SELECT time, value
FROM events
WHERE event_type = 'myEvent'
AND time > '2011-02-03'
AND time <= '2012-01-01'
SELECT COUNT (*) AS user_count FROM users;
The ``SELECT`` statements reads one or more columns for one or more rows in a table. It returns a result-set of the rows
matching the request, where each row contains the values for the selection corresponding to the query. Additionally,
:ref:`functions <cql-functions>` including :ref:`aggregation <aggregate-functions>` ones can be applied to the result.
A ``SELECT`` statement contains at least a :ref:`selection clause <selection-clause>` and the name of the table on which
the selection is on (note that CQL does **not** joins or sub-queries and thus a select statement only apply to a single
table). In most case, a select will also have a :ref:`where clause <where-clause>` and it can optionally have additional
clauses to :ref:`order <ordering-clause>` or :ref:`limit <limit-clause>` the results. Lastly, :ref:`queries that require
filtering <allow-filtering>` can be allowed if the ``ALLOW FILTERING`` flag is provided.
.. _selection-clause:
Selection clause
~~~~~~~~~~~~~~~~
The :token:`select_clause` determines which columns needs to be queried and returned in the result-set, as well as any
transformation to apply to this result before returning. It consists of a comma-separated list of *selectors* or,
alternatively, of the wildcard character (``*``) to select all the columns defined in the table.
Selectors
`````````
A :token:`selector` can be one of:
- A column name of the table selected, to retrieve the values for that column.
- A term, which is usually used nested inside other selectors like functions (if a term is selected directly, then the
corresponding column of the result-set will simply have the value of this term for every row returned).
- A casting, which allows to convert a nested selector to a (compatible) type.
- A function call, where the arguments are selector themselves. See the section on :ref:`functions <cql-functions>` for
more details.
- The special call ``COUNT(*)`` to the :ref:`COUNT function <count-function>`, which counts all non-null results.
Aliases
```````
Every *top-level* selector can also be aliased (using `AS`). If so, the name of the corresponding column in the result
set will be that of the alias. For instance::
// Without alias
SELECT intAsBlob(4) FROM t;
// intAsBlob(4)
// --------------
// 0x00000004
// With alias
SELECT intAsBlob(4) AS four FROM t;
// four
// ------------
// 0x00000004
.. note:: Currently, aliases aren't recognized anywhere else in the statement where they are used (not in the ``WHERE``
clause, not in the ``ORDER BY`` clause, ...). You must use the orignal column name instead.
``WRITETIME`` and ``TTL`` function
```````````````````````````````````
Selection supports two special functions (that aren't allowed anywhere else): ``WRITETIME`` and ``TTL``. Both function
take only one argument and that argument *must* be a column name (so for instance ``TTL(3)`` is invalid).
Those functions allow to retrieve meta-information that are stored internally for each column, namely:
- the timestamp of the value of the column for ``WRITETIME``.
- the remaining time to live (in seconds) for the value of the column if it set to expire (and ``null`` otherwise).
.. _where-clause:
The ``WHERE`` clause
~~~~~~~~~~~~~~~~~~~~
The ``WHERE`` clause specifies which rows must be queried. It is composed of relations on the columns that are part of
the ``PRIMARY KEY`` and/or have a `secondary index <#createIndexStmt>`__ defined on them.
Not all relations are allowed in a query. For instance, non-equal relations (where ``IN`` is considered as an equal
relation) on a partition key are not supported (but see the use of the ``TOKEN`` method below to do non-equal queries on
the partition key). Moreover, for a given partition key, the clustering columns induce an ordering of rows and relations
on them is restricted to the relations that allow to select a **contiguous** (for the ordering) set of rows. For
instance, given::
CREATE TABLE posts (
userid text,
blog_title text,
posted_at timestamp,
entry_title text,
content text,
category int,
PRIMARY KEY (userid, blog_title, posted_at)
)
The following query is allowed::
SELECT entry_title, content FROM posts
WHERE userid = 'john doe'
AND blog_title='John''s Blog'
AND posted_at >= '2012-01-01' AND posted_at < '2012-01-31'
But the following one is not, as it does not select a contiguous set of rows (and we suppose no secondary indexes are
set)::
// Needs a blog_title to be set to select ranges of posted_at
SELECT entry_title, content FROM posts
WHERE userid = 'john doe'
AND posted_at >= '2012-01-01' AND posted_at < '2012-01-31'
When specifying relations, the ``TOKEN`` function can be used on the ``PARTITION KEY`` column to query. In that case,
rows will be selected based on the token of their ``PARTITION_KEY`` rather than on the value. Note that the token of a
key depends on the partitioner in use, and that in particular the RandomPartitioner won't yield a meaningful order. Also
note that ordering partitioners always order token values by bytes (so even if the partition key is of type int,
``token(-1) > token(0)`` in particular). Example::
SELECT * FROM posts
WHERE token(userid) > token('tom') AND token(userid) < token('bob')
Moreover, the ``IN`` relation is only allowed on the last column of the partition key and on the last column of the full
primary key.
It is also possible to “group” ``CLUSTERING COLUMNS`` together in a relation using the tuple notation. For instance::
SELECT * FROM posts
WHERE userid = 'john doe'
AND (blog_title, posted_at) > ('John''s Blog', '2012-01-01')
will request all rows that sorts after the one having “John's Blog” as ``blog_tile`` and '2012-01-01' for ``posted_at``
in the clustering order. In particular, rows having a ``post_at <= '2012-01-01'`` will be returned as long as their
``blog_title > 'John''s Blog'``, which would not be the case for::
SELECT * FROM posts
WHERE userid = 'john doe'
AND blog_title > 'John''s Blog'
AND posted_at > '2012-01-01'
The tuple notation may also be used for ``IN`` clauses on clustering columns::
SELECT * FROM posts
WHERE userid = 'john doe'
AND (blog_title, posted_at) IN (('John''s Blog', '2012-01-01), ('Extreme Chess', '2014-06-01'))
The ``CONTAINS`` operator may only be used on collection columns (lists, sets, and maps). In the case of maps,
``CONTAINS`` applies to the map values. The ``CONTAINS KEY`` operator may only be used on map columns and applies to the
map keys.
.. _ordering-clause:
Ordering results
~~~~~~~~~~~~~~~~
The ``ORDER BY`` clause allows to select the order of the returned results. It takes as argument a list of column names
along with the order for the column (``ASC`` for ascendant and ``DESC`` for descendant, omitting the order being
equivalent to ``ASC``). Currently the possible orderings are limited by the :ref:`clustering order <clustering-order>`
defined on the table:
- if the table has been defined without any specific ``CLUSTERING ORDER``, then then allowed orderings are the order
induced by the clustering columns and the reverse of that one.
- otherwise, the orderings allowed are the order of the ``CLUSTERING ORDER`` option and the reversed one.
.. _limit-clause:
Limiting results
~~~~~~~~~~~~~~~~
The ``LIMIT`` option to a ``SELECT`` statement limits the number of rows returned by a query, while the ``PER PARTITION
LIMIT`` option limits the number of rows returned for a given partition by the query. Note that both type of limit can
used in the same statement.
.. _allow-filtering:
Allowing filtering
~~~~~~~~~~~~~~~~~~
By default, CQL only allows select queries that don't involve “filtering” server side, i.e. queries where we know that
all (live) record read will be returned (maybe partly) in the result set. The reasoning is that those “non filtering”
queries have predictable performance in the sense that they will execute in a time that is proportional to the amount of
data **returned** by the query (which can be controlled through ``LIMIT``).
The ``ALLOW FILTERING`` option allows to explicitly allow (some) queries that require filtering. Please note that a
query using ``ALLOW FILTERING`` may thus have unpredictable performance (for the definition above), i.e. even a query
that selects a handful of records **may** exhibit performance that depends on the total amount of data stored in the
cluster.
For instance, considering the following table holding user profiles with their year of birth (with a secondary index on
it) and country of residence::
CREATE TABLE users (
username text PRIMARY KEY,
firstname text,
lastname text,
birth_year int,
country text
)
CREATE INDEX ON users(birth_year);
Then the following queries are valid::
SELECT * FROM users;
SELECT * FROM users WHERE birth_year = 1981;
because in both case, Cassandra guarantees that these queries performance will be proportional to the amount of data
returned. In particular, if no users are born in 1981, then the second query performance will not depend of the number
of user profile stored in the database (not directly at least: due to secondary index implementation consideration, this
query may still depend on the number of node in the cluster, which indirectly depends on the amount of data stored.
Nevertheless, the number of nodes will always be multiple number of magnitude lower than the number of user profile
stored). Of course, both query may return very large result set in practice, but the amount of data returned can always
be controlled by adding a ``LIMIT``.
However, the following query will be rejected::
SELECT * FROM users WHERE birth_year = 1981 AND country = 'FR';
because Cassandra cannot guarantee that it won't have to scan large amount of data even if the result to those query is
small. Typically, it will scan all the index entries for users born in 1981 even if only a handful are actually from
France. However, if you “know what you are doing”, you can force the execution of this query by using ``ALLOW
FILTERING`` and so the following query is valid::
SELECT * FROM users WHERE birth_year = 1981 AND country = 'FR' ALLOW FILTERING;
.. _insert-statement:
INSERT
^^^^^^
Inserting data for a row is done using an ``INSERT`` statement:
.. productionlist::
insert_statement: INSERT INTO `table_name` ( `names_values` | `json_clause` )
: [ IF NOT EXISTS ]
: [ USING `update_parameter` ( AND `update_parameter` )* ]
names_values: `names` VALUES `tuple_literal`
json_clause: JSON `string`
names: '(' `column_name` ( ',' `column_name` )* ')'
For instance::
INSERT INTO NerdMovies (movie, director, main_actor, year)
VALUES ('Serenity', 'Joss Whedon', 'Nathan Fillion', 2005)
USING TTL 86400;
INSERT INTO NerdMovies JSON '{"movie": "Serenity",
"director": "Joss Whedon",
"year": 2005}';
The ``INSERT`` statement writes one or more columns for a given row in a table. Note that since a row is identified by
its ``PRIMARY KEY``, at least the columns composing it must be specified. The list of columns to insert to must be
supplied when using the ``VALUES`` syntax. When using the ``JSON`` syntax, they are optional. See the
section on :ref:`JSON support <cql-json>` for more detail.
Note that unlike in SQL, ``INSERT`` does not check the prior existence of the row by default: the row is created if none
existed before, and updated otherwise. Furthermore, there is no mean to know which of creation or update happened.
It is however possible to use the ``IF NOT EXISTS`` condition to only insert if the row does not exist prior to the
insertion. But please note that using ``IF NOT EXISTS`` will incur a non negligible performance cost (internally, Paxos
will be used) so this should be used sparingly.
All updates for an ``INSERT`` are applied atomically and in isolation.
Please refer to the :ref:`UPDATE <update-parameters>` section for informations on the :token:`update_parameter`.
Also note that ``INSERT`` does not support counters, while ``UPDATE`` does.
.. _update-statement:
UPDATE
^^^^^^
Updating a row is done using an ``UPDATE`` statement:
.. productionlist::
update_statement: UPDATE `table_name`
: [ USING `update_parameter` ( AND `update_parameter` )* ]
: SET `assignment` ( ',' `assignment` )*
: WHERE `where_clause`
: [ IF ( EXISTS | `condition` ( AND `condition` )*) ]
update_parameter: ( TIMESTAMP | TTL ) ( `integer` | `bind_marker` )
assignment: `simple_selection` '=' `term`
:| `column_name` '=' `column_name` ( '+' | '-' ) `term`
:| `column_name` '=' `list_literal` '+' `column_name`
simple_selection: `column_name`
:| `column_name` '[' `term` ']'
:| `column_name` '.' `field_name
condition: `simple_selection` `operator` `term`
For instance::
UPDATE NerdMovies USING TTL 400
SET director = 'Joss Whedon',
main_actor = 'Nathan Fillion',
year = 2005
WHERE movie = 'Serenity';
UPDATE UserActions
SET total = total + 2
WHERE user = B70DE1D0-9908-4AE3-BE34-5573E5B09F14
AND action = 'click';
The ``UPDATE`` statement writes one or more columns for a given row in a table. The :token:`where_clause` is used to
select the row to update and must include all columns composing the ``PRIMARY KEY``. Non primary key columns are then
set using the ``SET`` keyword.
Note that unlike in SQL, ``UPDATE`` does not check the prior existence of the row by default (except through ``IF``, see
below): the row is created if none existed before, and updated otherwise. Furthermore, there are no means to know
whether a creation or update occurred.
It is however possible to use the conditions on some columns through ``IF``, in which case the row will not be updated
unless the conditions are met. But, please note that using ``IF`` conditions will incur a non-negligible performance
cost (internally, Paxos will be used) so this should be used sparingly.
In an ``UPDATE`` statement, all updates within the same partition key are applied atomically and in isolation.
Regarding the :token:`assignment`:
- ``c = c + 3`` is used to increment/decrement counters. The column name after the '=' sign **must** be the same than
the one before the '=' sign. Note that increment/decrement is only allowed on counters, and are the *only* update
operations allowed on counters. See the section on :ref:`counters <counters>` for details.
- ``id = id + <some-collection>`` and ``id[value1] = value2`` are for collections, see the :ref:`relevant section
<collections>` for details.
- ``id.field = 3`` is for setting the value of a field on a non-frozen user-defined types. see the :ref:`relevant section
<udts>` for details.
.. _update-parameters:
Update parameters
~~~~~~~~~~~~~~~~~
The ``UPDATE``, ``INSERT`` (and ``DELETE`` and ``BATCH`` for the ``TIMESTAMP``) statements support the following
parameters:
- ``TIMESTAMP``: sets the timestamp for the operation. If not specified, the coordinator will use the current time (in
microseconds) at the start of statement execution as the timestamp. This is usually a suitable default.
- ``TTL``: specifies an optional Time To Live (in seconds) for the inserted values. If set, the inserted values are
automatically removed from the database after the specified time. Note that the TTL concerns the inserted values, not
the columns themselves. This means that any subsequent update of the column will also reset the TTL (to whatever TTL
is specified in that update). By default, values never expire. A TTL of 0 is equivalent to no TTL. If the table has a
default_time_to_live, a TTL of 0 will remove the TTL for the inserted or updated values.
.. _delete_statement:
DELETE
^^^^^^
Deleting rows or parts of rows uses the ``DELETE`` statement:
.. productionlist::
delete_statement: DELETE [ `simple_selection` ( ',' `simple_selection` ) ]
: FROM `table_name`
: [ USING `update_parameter` ( AND `update_parameter` )* ]
: WHERE `where_clause`
: [ IF ( EXISTS | `condition` ( AND `condition` )*) ]
For instance::
DELETE FROM NerdMovies USING TIMESTAMP 1240003134
WHERE movie = 'Serenity';
DELETE phone FROM Users
WHERE userid IN (C73DE1D3-AF08-40F3-B124-3FF3E5109F22, B70DE1D0-9908-4AE3-BE34-5573E5B09F14);
The ``DELETE`` statement deletes columns and rows. If column names are provided directly after the ``DELETE`` keyword,
only those columns are deleted from the row indicated by the ``WHERE`` clause. Otherwise, whole rows are removed.
The ``WHERE`` clause specifies which rows are to be deleted. Multiple rows may be deleted with one statement by using an
``IN`` operator. A range of rows may be deleted using an inequality operator (such as ``>=``).
``DELETE`` supports the ``TIMESTAMP`` option with the same semantics as in :ref:`updates <update-parameters>`.
In a ``DELETE`` statement, all deletions within the same partition key are applied atomically and in isolation.
A ``DELETE`` operation can be conditional through the use of an ``IF`` clause, similar to ``UPDATE`` and ``INSERT``
statements. However, as with ``INSERT`` and ``UPDATE`` statements, this will incur a non-negligible performance cost
(internally, Paxos will be used) and so should be used sparingly.
.. _batch_statement:
BATCH
^^^^^
Multiple ``INSERT``, ``UPDATE`` and ``DELETE`` can be executed in a single statement by grouping them through a
``BATCH`` statement:
.. productionlist::
batch_statement: BEGIN [ UNLOGGED | COUNTER ] BATCH
: [ USING `update_parameter` ( AND `update_parameter` )* ]
: `modification_statement` ( ';' `modification_statement` )*
: APPLY BATCH
modification_statement: `insert_statement` | `update_statement` | `delete_statement`
For instance::
BEGIN BATCH
INSERT INTO users (userid, password, name) VALUES ('user2', 'ch@ngem3b', 'second user');
UPDATE users SET password = 'ps22dhds' WHERE userid = 'user3';
INSERT INTO users (userid, password) VALUES ('user4', 'ch@ngem3c');
DELETE name FROM users WHERE userid = 'user1';
APPLY BATCH;
The ``BATCH`` statement group multiple modification statements (insertions/updates and deletions) into a single
statement. It serves several purposes:
- It saves network round-trips between the client and the server (and sometimes between the server coordinator and the
replicas) when batching multiple updates.
- All updates in a ``BATCH`` belonging to a given partition key are performed in isolation.
- By default, all operations in the batch are performed as *logged*, to ensure all mutations eventually complete (or
none will). See the notes on :ref:`UNLOGGED batches <unlogged-batches>` for more details.
Note that:
- ``BATCH`` statements may only contain ``UPDATE``, ``INSERT`` and ``DELETE`` statements (not other batches for instance).
- Batches are *not* a full analogue for SQL transactions.
- If a timestamp is not specified for each operation, then all operations will be applied with the same timestamp
(either one generated automatically, or the timestamp provided at the batch level). Due to Cassandra's conflict
resolution procedure in the case of `timestamp ties <http://wiki.apache.org/cassandra/FAQ#clocktie>`__, operations may
be applied in an order that is different from the order they are listed in the ``BATCH`` statement. To force a
particular operation ordering, you must specify per-operation timestamps.
.. _unlogged-batches:
``UNLOGGED`` batches
~~~~~~~~~~~~~~~~~~~~
By default, Cassandra uses a batch log to ensure all operations in a batch eventually complete or none will (note
however that operations are only isolated within a single partition).
There is a performance penalty for batch atomicity when a batch spans multiple partitions. If you do not want to incur
this penalty, you can tell Cassandra to skip the batchlog with the ``UNLOGGED`` option. If the ``UNLOGGED`` option is
used, a failed batch might leave the patch only partly applied.
``COUNTER`` batches
~~~~~~~~~~~~~~~~~~~
Use the ``COUNTER`` option for batched counter updates. Unlike other
updates in Cassandra, counter updates are not idempotent.

View File

@ -0,0 +1,553 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
.. _cql-functions:
.. Need some intro for UDF and native functions in general and point those to it.
.. _udfs:
.. _native-functions:
Functions
---------
CQL supports 2 main categories of functions:
- the :ref:`scalar functions <scalar-functions>`, which simply take a number of values and produce an output with it.
- the :ref:`aggregate functions <aggregate-functions>`, which are used to aggregate multiple rows results from a
``SELECT`` statement.
In both cases, CQL provides a number of native "hard-coded" functions as well as the ability to create new user-defined
functions.
.. note:: By default, the use of user-defined functions is disabled by default for security concerns (even when
enabled, the execution of user-defined functions is sandboxed and a "rogue" function should not be allowed to do
evil, but no sandbox is perfect so using user-defined functions is opt-in). See the ``enable_user_defined_functions``
in ``cassandra.yaml`` to enable them.
.. _scalar-functions:
Scalar functions
^^^^^^^^^^^^^^^^
.. _scalar-native-functions:
Native functions
~~~~~~~~~~~~~~~~
Cast
````
The ``cast`` function can be used to converts one native datatype to another.
The following table describes the conversions supported by the ``cast`` function. Cassandra will silently ignore any
cast converting a datatype into its own datatype.
=============== =======================================================================================================
From To
=============== =======================================================================================================
``ascii`` ``text``, ``varchar``
``bigint`` ``tinyint``, ``smallint``, ``int``, ``float``, ``double``, ``decimal``, ``varint``, ``text``,
``varchar``
``boolean`` ``text``, ``varchar``
``counter`` ``tinyint``, ``smallint``, ``int``, ``bigint``, ``float``, ``double``, ``decimal``, ``varint``,
``text``, ``varchar``
``date`` ``timestamp``
``decimal`` ``tinyint``, ``smallint``, ``int``, ``bigint``, ``float``, ``double``, ``varint``, ``text``,
``varchar``
``double`` ``tinyint``, ``smallint``, ``int``, ``bigint``, ``float``, ``decimal``, ``varint``, ``text``,
``varchar``
``float`` ``tinyint``, ``smallint``, ``int``, ``bigint``, ``double``, ``decimal``, ``varint``, ``text``,
``varchar``
``inet`` ``text``, ``varchar``
``int`` ``tinyint``, ``smallint``, ``bigint``, ``float``, ``double``, ``decimal``, ``varint``, ``text``,
``varchar``
``smallint`` ``tinyint``, ``int``, ``bigint``, ``float``, ``double``, ``decimal``, ``varint``, ``text``,
``varchar``
``time`` ``text``, ``varchar``
``timestamp`` ``date``, ``text``, ``varchar``
``timeuuid`` ``timestamp``, ``date``, ``text``, ``varchar``
``tinyint`` ``tinyint``, ``smallint``, ``int``, ``bigint``, ``float``, ``double``, ``decimal``, ``varint``,
``text``, ``varchar``
``uuid`` ``text``, ``varchar``
``varint`` ``tinyint``, ``smallint``, ``int``, ``bigint``, ``float``, ``double``, ``decimal``, ``text``,
``varchar``
=============== =======================================================================================================
The conversions rely strictly on Java's semantics. For example, the double value 1 will be converted to the text value
'1.0'. For instance::
SELECT avg(cast(count as double)) FROM myTable
Token
`````
The ``token`` function allows to compute the token for a given partition key. The exact signature of the token function
depends on the table concerned and of the partitioner used by the cluster.
The type of the arguments of the ``token`` depend on the type of the partition key columns. The return type depend on
the partitioner in use:
- For Murmur3Partitioner, the return type is ``bigint``.
- For RandomPartitioner, the return type is ``varint``.
- For ByteOrderedPartitioner, the return type is ``blob``.
For instance, in a cluster using the default Murmur3Partitioner, if a table is defined by::
CREATE TABLE users (
userid text PRIMARY KEY,
username text,
)
then the ``token`` function will take a single argument of type ``text`` (in that case, the partition key is ``userid``
(there is no clustering columns so the partition key is the same than the primary key)), and the return type will be
``bigint``.
Uuid
````
The ``uuid`` function takes no parameters and generates a random type 4 uuid suitable for use in ``INSERT`` or
``UPDATE`` statements.
.. _timeuuid-functions:
Timeuuid functions
``````````````````
``now``
#######
The ``now`` function takes no arguments and generates, on the coordinator node, a new unique timeuuid (at the time where
the statement using it is executed). Note that this method is useful for insertion but is largely non-sensical in
``WHERE`` clauses. For instance, a query of the form::
SELECT * FROM myTable WHERE t = now()
will never return any result by design, since the value returned by ``now()`` is guaranteed to be unique.
``minTimeuuid`` and ``maxTimeuuid``
###################################
The ``minTimeuuid`` (resp. ``maxTimeuuid``) function takes a ``timestamp`` value ``t`` (which can be `either a timestamp
or a date string <timestamps>`) and return a *fake* ``timeuuid`` corresponding to the *smallest* (resp. *biggest*)
possible ``timeuuid`` having for timestamp ``t``. So for instance::
SELECT * FROM myTable
WHERE t > maxTimeuuid('2013-01-01 00:05+0000')
AND t < minTimeuuid('2013-02-02 10:00+0000')
will select all rows where the ``timeuuid`` column ``t`` is strictly older than ``'2013-01-01 00:05+0000'`` but strictly
younger than ``'2013-02-02 10:00+0000'``. Please note that ``t >= maxTimeuuid('2013-01-01 00:05+0000')`` would still
*not* select a ``timeuuid`` generated exactly at '2013-01-01 00:05+0000' and is essentially equivalent to ``t >
maxTimeuuid('2013-01-01 00:05+0000')``.
.. note:: We called the values generated by ``minTimeuuid`` and ``maxTimeuuid`` *fake* UUID because they do no respect
the Time-Based UUID generation process specified by the `RFC 4122 <http://www.ietf.org/rfc/rfc4122.txt>`__. In
particular, the value returned by these 2 methods will not be unique. This means you should only use those methods
for querying (as in the example above). Inserting the result of those methods is almost certainly *a bad idea*.
Time conversion functions
`````````````````````````
A number of functions are provided to “convert” a ``timeuuid``, a ``timestamp`` or a ``date`` into another ``native``
type.
===================== =============== ===================================================================
Function name Input type Description
===================== =============== ===================================================================
``toDate`` ``timeuuid`` Converts the ``timeuuid`` argument into a ``date`` type
``toDate`` ``timestamp`` Converts the ``timestamp`` argument into a ``date`` type
``toTimestamp`` ``timeuuid`` Converts the ``timeuuid`` argument into a ``timestamp`` type
``toTimestamp`` ``date`` Converts the ``date`` argument into a ``timestamp`` type
``toUnixTimestamp`` ``timeuuid`` Converts the ``timeuuid`` argument into a ``bigInt`` raw value
``toUnixTimestamp`` ``timestamp`` Converts the ``timestamp`` argument into a ``bigInt`` raw value
``toUnixTimestamp`` ``date`` Converts the ``date`` argument into a ``bigInt`` raw value
``dateOf`` ``timeuuid`` Similar to ``toTimestamp(timeuuid)`` (DEPRECATED)
``unixTimestampOf`` ``timeuuid`` Similar to ``toUnixTimestamp(timeuuid)`` (DEPRECATED)
===================== =============== ===================================================================
Blob conversion functions
`````````````````````````
A number of functions are provided to “convert” the native types into binary data (``blob``). For every
``<native-type>`` ``type`` supported by CQL (a notable exceptions is ``blob``, for obvious reasons), the function
``typeAsBlob`` takes a argument of type ``type`` and return it as a ``blob``. Conversely, the function ``blobAsType``
takes a 64-bit ``blob`` argument and convert it to a ``bigint`` value. And so for instance, ``bigintAsBlob(3)`` is
``0x0000000000000003`` and ``blobAsBigint(0x0000000000000003)`` is ``3``.
.. _user-defined-scalar-functions:
User-defined functions
~~~~~~~~~~~~~~~~~~~~~~
User-defined functions allow execution of user-provided code in Cassandra. By default, Cassandra supports defining
functions in *Java* and *JavaScript*. Support for other JSR 223 compliant scripting languages (such as Python, Ruby, and
Scala) can be added by adding a JAR to the classpath.
UDFs are part of the Cassandra schema. As such, they are automatically propagated to all nodes in the cluster.
UDFs can be *overloaded* - i.e. multiple UDFs with different argument types but the same function name. Example::
CREATE FUNCTION sample ( arg int ) ...;
CREATE FUNCTION sample ( arg text ) ...;
User-defined functions are susceptible to all of the normal problems with the chosen programming language. Accordingly,
implementations should be safe against null pointer exceptions, illegal arguments, or any other potential source of
exceptions. An exception during function execution will result in the entire statement failing.
It is valid to use *complex* types like collections, tuple types and user-defined types as argument and return types.
Tuple types and user-defined types are handled by the conversion functions of the DataStax Java Driver. Please see the
documentation of the Java Driver for details on handling tuple types and user-defined types.
Arguments for functions can be literals or terms. Prepared statement placeholders can be used, too.
Note that you can use the double-quoted string syntax to enclose the UDF source code. For example::
CREATE FUNCTION some_function ( arg int )
RETURNS NULL ON NULL INPUT
RETURNS int
LANGUAGE java
AS $$ return arg; $$;
SELECT some_function(column) FROM atable ...;
UPDATE atable SET col = some_function(?) ...;
CREATE TYPE custom_type (txt text, i int);
CREATE FUNCTION fct_using_udt ( udtarg frozen )
RETURNS NULL ON NULL INPUT
RETURNS text
LANGUAGE java
AS $$ return udtarg.getString("txt"); $$;
User-defined functions can be used in ``SELECT``, ``INSERT`` and ``UPDATE`` statements.
The implicitly available ``udfContext`` field (or binding for script UDFs) provides the necessary functionality to
create new UDT and tuple values::
CREATE TYPE custom\_type (txt text, i int);
CREATE FUNCTION fct\_using\_udt ( somearg int )
RETURNS NULL ON NULL INPUT
RETURNS custom\_type
LANGUAGE java
AS $$
UDTValue udt = udfContext.newReturnUDTValue();
udt.setString(“txt”, “some string”);
udt.setInt(“i”, 42);
return udt;
$$;
The definition of the ``UDFContext`` interface can be found in the Apache Cassandra source code for
``org.apache.cassandra.cql3.functions.UDFContext``.
.. code-block:: java
public interface UDFContext
{
UDTValue newArgUDTValue(String argName);
UDTValue newArgUDTValue(int argNum);
UDTValue newReturnUDTValue();
UDTValue newUDTValue(String udtName);
TupleValue newArgTupleValue(String argName);
TupleValue newArgTupleValue(int argNum);
TupleValue newReturnTupleValue();
TupleValue newTupleValue(String cqlDefinition);
}
Java UDFs already have some imports for common interfaces and classes defined. These imports are:
.. code-block:: java
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.cassandra.cql3.functions.UDFContext;
import com.datastax.driver.core.TypeCodec;
import com.datastax.driver.core.TupleValue;
import com.datastax.driver.core.UDTValue;
Please note, that these convenience imports are not available for script UDFs.
.. _create-function-statement:
CREATE FUNCTION
```````````````
Creating a new user-defined function uses the ``CREATE FUNCTION`` statement:
.. productionlist::
create_function_statement: CREATE [ OR REPLACE ] FUNCTION [ IF NOT EXISTS]
: `function_name` '(' `arguments_declaration` ')'
: [ CALLED | RETURNS NULL ] ON NULL INPUT
: RETURNS `cql_type`
: LANGUAGE `identifier`
: AS `string`
arguments_declaration: `identifier` `cql_type` ( ',' `identifier` `cql_type` )*
For instance::
CREATE OR REPLACE FUNCTION somefunction(somearg int, anotherarg text, complexarg frozen<someUDT>, listarg list)
RETURNS NULL ON NULL INPUT
RETURNS text
LANGUAGE java
AS $$
// some Java code
$$;
CREATE FUNCTION IF NOT EXISTS akeyspace.fname(someArg int)
CALLED ON NULL INPUT
RETURNS text
LANGUAGE java
AS $$
// some Java code
$$;
``CREATE FUNCTION`` with the optional ``OR REPLACE`` keywords either creates a function or replaces an existing one with
the same signature. A ``CREATE FUNCTION`` without ``OR REPLACE`` fails if a function with the same signature already
exists.
If the optional ``IF NOT EXISTS`` keywords are used, the function will
only be created if another function with the same signature does not
exist.
``OR REPLACE`` and ``IF NOT EXISTS`` cannot be used together.
Behavior on invocation with ``null`` values must be defined for each
function. There are two options:
#. ``RETURNS NULL ON NULL INPUT`` declares that the function will always
return ``null`` if any of the input arguments is ``null``.
#. ``CALLED ON NULL INPUT`` declares that the function will always be
executed.
Function Signature
##################
Signatures are used to distinguish individual functions. The signature consists of:
#. The fully qualified function name - i.e *keyspace* plus *function-name*
#. The concatenated list of all argument types
Note that keyspace names, function names and argument types are subject to the default naming conventions and
case-sensitivity rules.
Functions belong to a keyspace. If no keyspace is specified in ``<function-name>``, the current keyspace is used (i.e.
the keyspace specified using the ``USE`` statement). It is not possible to create a user-defined function in one of the
system keyspaces.
.. _drop-function-statement:
DROP FUNCTION
`````````````
Dropping a function uses the ``DROP FUNCTION`` statement:
.. productionlist::
drop_function_statement: DROP FUNCTION [ IF EXISTS ] `function_name` [ '(' `arguments_signature` ')' ]
arguments_signature: `cql_type` ( ',' `cql_type` )*
For instance::
DROP FUNCTION myfunction;
DROP FUNCTION mykeyspace.afunction;
DROP FUNCTION afunction ( int );
DROP FUNCTION afunction ( text );
You must specify the argument types (:token:`arguments_signature`) of the function to drop if there are multiple
functions with the same name but a different signature (overloaded functions).
``DROP FUNCTION`` with the optional ``IF EXISTS`` keywords drops a function if it exists, but does not throw an error if
it doesn't
.. _aggregate-functions:
Aggregate functions
^^^^^^^^^^^^^^^^^^^
Aggregate functions work on a set of rows. They receive values for each row and returns one value for the whole set.
If ``normal`` columns, ``scalar functions``, ``UDT`` fields, ``writetime`` or ``ttl`` are selected together with
aggregate functions, the values returned for them will be the ones of the first row matching the query.
Native aggregates
~~~~~~~~~~~~~~~~~
.. _count-function:
Count
`````
The ``count`` function can be used to count the rows returned by a query. Example::
SELECT COUNT (*) FROM plays;
SELECT COUNT (1) FROM plays;
It also can be used to count the non null value of a given column::
SELECT COUNT (scores) FROM plays;
Max and Min
```````````
The ``max`` and ``min`` functions can be used to compute the maximum and the minimum value returned by a query for a
given column. For instance::
SELECT MIN (players), MAX (players) FROM plays WHERE game = 'quake';
Sum
```
The ``sum`` function can be used to sum up all the values returned by a query for a given column. For instance::
SELECT SUM (players) FROM plays;
Avg
```
The ``avg`` function can be used to compute the average of all the values returned by a query for a given column. For
instance::
SELECT AVG (players) FROM plays;
.. _user-defined-aggregates-functions:
User-Defined Aggregates
~~~~~~~~~~~~~~~~~~~~~~~
User-defined aggregates allow the creation of custom aggregate functions. Common examples of aggregate functions are
*count*, *min*, and *max*.
Each aggregate requires an *initial state* (``INITCOND``, which defaults to ``null``) of type ``STYPE``. The first
argument of the state function must have type ``STYPE``. The remaining arguments of the state function must match the
types of the user-defined aggregate arguments. The state function is called once for each row, and the value returned by
the state function becomes the new state. After all rows are processed, the optional ``FINALFUNC`` is executed with last
state value as its argument.
``STYPE`` is mandatory in order to be able to distinguish possibly overloaded versions of the state and/or final
function (since the overload can appear after creation of the aggregate).
User-defined aggregates can be used in ``SELECT`` statement.
A complete working example for user-defined aggregates (assuming that a keyspace has been selected using the ``USE``
statement)::
CREATE OR REPLACE FUNCTION averageState(state tuple<int,bigint>, val int)
CALLED ON NULL INPUT
RETURNS tuple
LANGUAGE java
AS '
if (val != null) {
state.setInt(0, state.getInt(0)+1);
state.setLong(1, state.getLong(1)+val.intValue());
}
return state;
';
CREATE OR REPLACE FUNCTION averageFinal (state tuple<int,bigint>)
CALLED ON NULL INPUT
RETURNS double
LANGUAGE java
AS '
double r = 0;
if (state.getInt(0) == 0) return null;
r = state.getLong(1);
r /= state.getInt(0);
return Double.valueOf®;
';
CREATE OR REPLACE AGGREGATE average(int)
SFUNC averageState
STYPE tuple
FINALFUNC averageFinal
INITCOND (0, 0);
CREATE TABLE atable (
pk int PRIMARY KEY,
val int
);
INSERT INTO atable (pk, val) VALUES (1,1);
INSERT INTO atable (pk, val) VALUES (2,2);
INSERT INTO atable (pk, val) VALUES (3,3);
INSERT INTO atable (pk, val) VALUES (4,4);
SELECT average(val) FROM atable;
.. _create-aggregate-statement:
CREATE AGGREGATE
````````````````
Creating (or replacing) a user-defined aggregate function uses the ``CREATE AGGREGATE`` statement:
.. productionlist::
create_aggregate_statement: CREATE [ OR REPLACE ] AGGREGATE [ IF NOT EXISTS ]
: `function_name` '(' `arguments_signature` ')'
: SFUNC `function_name`
: STYPE `cql_type`
: [ FINALFUNC `function_name` ]
: [ INITCOND `term` ]
See above for a complete example.
``CREATE AGGREGATE`` with the optional ``OR REPLACE`` keywords either creates an aggregate or replaces an existing one
with the same signature. A ``CREATE AGGREGATE`` without ``OR REPLACE`` fails if an aggregate with the same signature
already exists.
``CREATE AGGREGATE`` with the optional ``IF NOT EXISTS`` keywords either creates an aggregate if it does not already
exist.
``OR REPLACE`` and ``IF NOT EXISTS`` cannot be used together.
``STYPE`` defines the type of the state value and must be specified.
The optional ``INITCOND`` defines the initial state value for the aggregate. It defaults to ``null``. A non-\ ``null``
``INITCOND`` must be specified for state functions that are declared with ``RETURNS NULL ON NULL INPUT``.
``SFUNC`` references an existing function to be used as the state modifying function. The type of first argument of the
state function must match ``STYPE``. The remaining argument types of the state function must match the argument types of
the aggregate function. State is not updated for state functions declared with ``RETURNS NULL ON NULL INPUT`` and called
with ``null``.
The optional ``FINALFUNC`` is called just before the aggregate result is returned. It must take only one argument with
type ``STYPE``. The return type of the ``FINALFUNC`` may be a different type. A final function declared with ``RETURNS
NULL ON NULL INPUT`` means that the aggregate's return value will be ``null``, if the last state is ``null``.
If no ``FINALFUNC`` is defined, the overall return type of the aggregate function is ``STYPE``. If a ``FINALFUNC`` is
defined, it is the return type of that function.
.. _drop-aggregate-statement:
DROP AGGREGATE
``````````````
Dropping an user-defined aggregate function uses the ``DROP AGGREGATE`` statement:
.. productionlist::
drop_aggregate_statement: DROP AGGREGATE [ IF EXISTS ] `function_name` [ '(' `arguments_signature` ')' ]
For instance::
DROP AGGREGATE myAggregate;
DROP AGGREGATE myKeyspace.anAggregate;
DROP AGGREGATE someAggregate ( int );
DROP AGGREGATE someAggregate ( text );
The ``DROP AGGREGATE`` statement removes an aggregate created using ``CREATE AGGREGATE``. You must specify the argument
types of the aggregate to drop if there are multiple aggregates with the same name but a different signature (overloaded
aggregates).
``DROP AGGREGATE`` with the optional ``IF EXISTS`` keywords drops an aggregate if it exists, and does nothing if a
function with the signature does not exist.

47
doc/source/cql/index.rst Normal file
View File

@ -0,0 +1,47 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. _cql:
The Cassandra Query Language (CQL)
==================================
This document describes the Cassandra Query Language (CQL) [#]_. Note that this document describes the last version of
the languages. However, the `changes <#changes>`_ section provides the diff between the different versions of CQL.
CQL offers a model close to SQL in the sense that data is put in *tables* containing *rows* of *columns*. For
that reason, when used in this document, these terms (tables, rows and columns) have the same definition than they have
in SQL. But please note that as such, they do **not** refer to the concept of rows and columns found in the deprecated
thrift API (and earlier version 1 and 2 of CQL).
.. toctree::
:maxdepth: 2
definitions
types
ddl
dml
indexes
mvs
security
functions
json
triggers
appendices
changes
.. [#] Technically, this document CQL version 3, which is not backward compatible with CQL version 1 and 2 (which have
been deprecated and remove) and differs from it in numerous ways.

View File

@ -0,0 +1,83 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
.. _secondary-indexes:
Secondary Indexes
-----------------
CQL supports creating secondary indexes on tables, allowing queries on the table to use those indexes. A secondary index
is identified by a name defined by:
.. productionlist::
index_name: re('[a-zA-Z_0-9]+')
.. _create-index-statement:
CREATE INDEX
^^^^^^^^^^^^
Creating a secondary index on a table uses the ``CREATE INDEX`` statement:
.. productionlist::
create_index_statement: CREATE [ CUSTOM ] INDEX [ IF NOT EXISTS ] [ `index_name` ]
: ON `table_name` '(' `index_identifier` ')'
: [ USING `string` [ WITH OPTIONS = `map_literal` ] ]
index_identifier: `column_name`
:| ( KEYS | VALUES | ENTRIES | FULL ) '(' `column_name` ')'
For instance::
CREATE INDEX userIndex ON NerdMovies (user);
CREATE INDEX ON Mutants (abilityId);
CREATE INDEX ON users (keys(favs));
CREATE CUSTOM INDEX ON users (email) USING 'path.to.the.IndexClass';
CREATE CUSTOM INDEX ON users (email) USING 'path.to.the.IndexClass' WITH OPTIONS = {'storage': '/mnt/ssd/indexes/'};
The ``CREATE INDEX`` statement is used to create a new (automatic) secondary index for a given (existing) column in a
given table. A name for the index itself can be specified before the ``ON`` keyword, if desired. If data already exists
for the column, it will be indexed asynchronously. After the index is created, new data for the column is indexed
automatically at insertion time.
Attempting to create an already existing index will return an error unless the ``IF NOT EXISTS`` option is used. If it
is used, the statement will be a no-op if the index already exists.
Indexes on Map Keys
~~~~~~~~~~~~~~~~~~~
When creating an index on a :ref:`maps <maps>`, you may index either the keys or the values. If the column identifier is
placed within the ``keys()`` function, the index will be on the map keys, allowing you to use ``CONTAINS KEY`` in
``WHERE`` clauses. Otherwise, the index will be on the map values.
.. _drop-index-statement:
DROP INDEX
^^^^^^^^^^
Dropping a secondary index uses the ``DROP INDEX`` statement:
.. productionlist::
drop_index_statement: DROP INDEX [ IF EXISTS ] `index_name`
The ``DROP INDEX`` statement is used to drop an existing secondary index. The argument of the statement is the index
name, which may optionally specify the keyspace of the index.
If the index does not exists, the statement will return an error, unless ``IF EXISTS`` is used in which case the
operation is a no-op.

112
doc/source/cql/json.rst Normal file
View File

@ -0,0 +1,112 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
.. _cql-json:
JSON Support
------------
Cassandra 2.2 introduces JSON support to :ref:`SELECT <select-statement>` and :ref:`INSERT <insert-statement>`
statements. This support does not fundamentally alter the CQL API (for example, the schema is still enforced), it simply
provides a convenient way to work with JSON documents.
SELECT JSON
^^^^^^^^^^^
With ``SELECT`` statements, the ``JSON`` keyword can be used to return each row as a single ``JSON`` encoded map. The
remainder of the ``SELECT`` statement behavior is the same.
The result map keys are the same as the column names in a normal result set. For example, a statement like ``SELECT JSON
a, ttl(b) FROM ...`` would result in a map with keys ``"a"`` and ``"ttl(b)"``. However, this is one notable exception:
for symmetry with ``INSERT JSON`` behavior, case-sensitive column names with upper-case letters will be surrounded with
double quotes. For example, ``SELECT JSON myColumn FROM ...`` would result in a map key ``"\"myColumn\""`` (note the
escaped quotes).
The map values will ``JSON``-encoded representations (as described below) of the result set values.
INSERT JSON
^^^^^^^^^^^
With ``INSERT`` statements, the new ``JSON`` keyword can be used to enable inserting a ``JSON`` encoded map as a single
row. The format of the ``JSON`` map should generally match that returned by a ``SELECT JSON`` statement on the same
table. In particular, case-sensitive column names should be surrounded with double quotes. For example, to insert into a
table with two columns named "myKey" and "value", you would do the following::
INSERT INTO mytable JSON '{ "\"myKey\"": 0, "value": 0}'
Any columns which are omitted from the ``JSON`` map will be defaulted to a ``NULL`` value (which will result in a
tombstone being created).
JSON Encoding of Cassandra Data Types
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Where possible, Cassandra will represent and accept data types in their native ``JSON`` representation. Cassandra will
also accept string representations matching the CQL literal format for all single-field types. For example, floats,
ints, UUIDs, and dates can be represented by CQL literal strings. However, compound types, such as collections, tuples,
and user-defined types must be represented by native ``JSON`` collections (maps and lists) or a JSON-encoded string
representation of the collection.
The following table describes the encodings that Cassandra will accept in ``INSERT JSON`` values (and ``fromJson()``
arguments) as well as the format Cassandra will use when returning data for ``SELECT JSON`` statements (and
``fromJson()``):
=============== ======================== =============== ==============================================================
Type Formats accepted Return format Notes
=============== ======================== =============== ==============================================================
``ascii`` string string Uses JSON's ``\u`` character escape
``bigint`` integer, string integer String must be valid 64 bit integer
``blob`` string string String should be 0x followed by an even number of hex digits
``boolean`` boolean, string boolean String must be "true" or "false"
``date`` string string Date in format ``YYYY-MM-DD``, timezone UTC
``decimal`` integer, float, string float May exceed 32 or 64-bit IEEE-754 floating point precision in
client-side decoder
``double`` integer, float, string float String must be valid integer or float
``float`` integer, float, string float String must be valid integer or float
``inet`` string string IPv4 or IPv6 address
``int`` integer, string integer String must be valid 32 bit integer
``list`` list, string list Uses JSON's native list representation
``map`` map, string map Uses JSON's native map representation
``smallint`` integer, string integer String must be valid 16 bit integer
``set`` list, string list Uses JSON's native list representation
``text`` string string Uses JSON's ``\u`` character escape
``time`` string string Time of day in format ``HH-MM-SS[.fffffffff]``
``timestamp`` integer, string string A timestamp. Strings constant allows to input :ref:`timestamps
as dates <timestamps>`. Datestamps with format ``YYYY-MM-DD
HH:MM:SS.SSS`` are returned.
``timeuuid`` string string Type 1 UUID. See :token:`constant` for the UUID format
``tinyint`` integer, string integer String must be valid 8 bit integer
``tuple`` list, string list Uses JSON's native list representation
``UDT`` map, string map Uses JSON's native map representation with field names as keys
``uuid`` string string See :token:`constant` for the UUID format
``varchar`` string string Uses JSON's ``\u`` character escape
``varint`` integer, string integer Variable length; may overflow 32 or 64 bit integers in
client-side decoder
=============== ======================== =============== ==============================================================
The fromJson() Function
^^^^^^^^^^^^^^^^^^^^^^^
The ``fromJson()`` function may be used similarly to ``INSERT JSON``, but for a single column value. It may only be used
in the ``VALUES`` clause of an ``INSERT`` statement or as one of the column values in an ``UPDATE``, ``DELETE``, or
``SELECT`` statement. For example, it cannot be used in the selection clause of a ``SELECT`` statement.
The toJson() Function
^^^^^^^^^^^^^^^^^^^^^
The ``toJson()`` function may be used similarly to ``SELECT JSON``, but for a single column value. It may only be used
in the selection clause of a ``SELECT`` statement.

166
doc/source/cql/mvs.rst Normal file
View File

@ -0,0 +1,166 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
.. _materialized-views:
Materialized Views
------------------
Materialized views names are defined by:
.. productionlist::
view_name: re('[a-zA-Z_0-9]+')
.. _create-materialized-view-statement:
CREATE MATERIALIZED VIEW
^^^^^^^^^^^^^^^^^^^^^^^^
You can create a materialized view on a table using a ``CREATE MATERIALIZED VIEW`` statement:
.. productionlist::
create_materialized_view_statement: CREATE MATERIALIZED VIEW [ IF NOT EXISTS ] `view_name` AS
: `select_statement`
: PRIMARY KEY '(' `primary_key` ')'
: WITH `table_options`
For instance::
CREATE MATERIALIZED VIEW monkeySpecies_by_population AS
SELECT * FROM monkeySpecies
WHERE population IS NOT NULL AND species IS NOT NULL
PRIMARY KEY (population, species)
WITH comment=Allow query by population instead of species;
The ``CREATE MATERIALIZED VIEW`` statement creates a new materialized view. Each such view is a set of *rows* which
corresponds to rows which are present in the underlying, or base, table specified in the ``SELECT`` statement. A
materialized view cannot be directly updated, but updates to the base table will cause corresponding updates in the
view.
Creating a materialized view has 3 main parts:
- The :ref:`select statement <mv-select>` that restrict the data included in the view.
- The :ref:`primary key <mv-primary-key>` definition for the view.
- The :ref:`options <mv-options>` for the view.
Attempting to create an already existing materialized view will return an error unless the ``IF NOT EXISTS`` option is
used. If it is used, the statement will be a no-op if the materialized view already exists.
.. _mv-select:
MV select statement
```````````````````
The select statement of a materialized view creation defines which of the base table is included in the view. That
statement is limited in a number of ways:
- the :ref:`selection <selection-clause>` is limited to those that only select columns of the base table. In other
words, you can't use any function (aggregate or not), casting, term, etc. Aliases are also not supported. You can
however use `*` as a shortcut of selecting all columns. Further, :ref:`static columns <static-columns>` cannot be
included in a materialized view (which means ``SELECT *`` isn't allowed if the base table has static columns).
- the ``WHERE`` clause have the following restrictions:
- it cannot include any :token:`bind_marker`.
- the columns that are not part of the *base table* primary key can only be restricted by an ``IS NOT NULL``
restriction. No other restriction is allowed.
- as the columns that are part of the *view* primary key cannot be null, they must always be at least restricted by a
``IS NOT NULL`` restriction (or any other restriction, but they must have one).
- it cannot have neither an :ref:`ordering clause <ordering-clause>`, nor a :ref:`limit <limit-clause>`, nor :ref:`ALLOW
FILTERING <allow-filtering>`.
.. _mv-primary-key:
MV primary key
``````````````
A view must have a primary key and that primary key must conform to the following restrictions:
- it must contain all the primary key columns of the base table. This ensures that every row of the view correspond to
exactly one row of the base table.
- it can only contain a single column that is not a primary key column in the base table.
So for instance, give the following base table definition::
CREATE TABLE t (
k int,
c1 int,
c2 int,
v1 int,
v2 int,
PRIMARY KEY (k, c1, c2)
)
then the following view definitions are allowed::
CREATE MATERIALIZED VIEW mv1 AS
SELECT * FROM t WHERE k IS NOT NULL AND c1 IS NOT NULL AND c2 IS NOT NULL
PRIMARY KEY (c1, k, c2)
CREATE MATERIALIZED VIEW mv1 AS
SELECT * FROM t WHERE k IS NOT NULL AND c1 IS NOT NULL AND c2 IS NOT NULL
PRIMARY KEY (v1, k, c1, c2)
but the following ones are **not** allowed::
// Error: cannot include both v1 and v2 in the primary key as both are not in the base table primary key
CREATE MATERIALIZED VIEW mv1 AS
SELECT * FROM t WHERE k IS NOT NULL AND c1 IS NOT NULL AND c2 IS NOT NULL AND v1 IS NOT NULL
PRIMARY KEY (v1, v2, k, c1, c2)
// Error: must include k in the primary as it's a base table primary key column
CREATE MATERIALIZED VIEW mv1 AS
SELECT * FROM t WHERE c1 IS NOT NULL AND c2 IS NOT NULL
PRIMARY KEY (c1, c2)
.. _mv-options:
MV options
``````````
A materialized view is internally implemented by a table and as such, creating a MV allows the :ref:`same options than
creating a table <create-table-options>`.
.. _alter-materialized-view-statement:
ALTER MATERIALIZED VIEW
^^^^^^^^^^^^^^^^^^^^^^^
After creation, you can alter the options of a materialized view using the ``ALTER MATERIALIZED VIEW`` statement:
.. productionlist::
alter_materialized_view_statement: ALTER MATERIALIZED VIEW `view_name` WITH `table_options`
The options that can be updated are the same than at creation time and thus the :ref:`same than for tables
<create-table-options>`.
.. _drop-materialized-view-statement:
DROP MATERIALIZED VIEW
^^^^^^^^^^^^^^^^^^^^^^
Dropping a materialized view users the ``DROP MATERIALIZED VIEW`` statement:
.. productionlist::
drop_materialized_view_statement: DROP MATERIALIZED VIEW [ IF EXISTS ] `view_name`;
If the materialized view does not exists, the statement will return an error, unless ``IF EXISTS`` is used in which case
the operation is a no-op.

497
doc/source/cql/security.rst Normal file
View File

@ -0,0 +1,497 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
.. _cql-security:
Security
--------
.. _cql-roles:
Database Roles
^^^^^^^^^^^^^^
.. _create-role-statement:
CREATE ROLE
~~~~~~~~~~~
Creating a role uses the ``CREATE ROLE`` statement:
.. productionlist::
create_role_statement: CREATE ROLE [ IF NOT EXISTS ] `role_name`
: [ WITH `role_options` ]
role_options: `role_option` ( AND `role_option` )*
role_option: PASSWORD '=' `string`
:| LOGIN '=' `boolean`
:| SUPERUSER '=' `boolean`
:| OPTIONS '=' `map_literal`
For instance::
CREATE ROLE new_role;
CREATE ROLE alice WITH PASSWORD = 'password_a' AND LOGIN = true;
CREATE ROLE bob WITH PASSWORD = 'password_b' AND LOGIN = true AND SUPERUSER = true;
CREATE ROLE carlos WITH OPTIONS = { 'custom_option1' : 'option1_value', 'custom_option2' : 99 };
By default roles do not possess ``LOGIN`` privileges or ``SUPERUSER`` status.
:ref:`Permissions <cql-permissions>` on database resources are granted to roles; types of resources include keyspaces,
tables, functions and roles themselves. Roles may be granted to other roles to create hierarchical permissions
structures; in these hierarchies, permissions and ``SUPERUSER`` status are inherited, but the ``LOGIN`` privilege is
not.
If a role has the ``LOGIN`` privilege, clients may identify as that role when connecting. For the duration of that
connection, the client will acquire any roles and privileges granted to that role.
Only a client with with the ``CREATE`` permission on the database roles resource may issue ``CREATE ROLE`` requests (see
the :ref:`relevant section <cql-permissions>` below), unless the client is a ``SUPERUSER``. Role management in Cassandra
is pluggable and custom implementations may support only a subset of the listed options.
Role names should be quoted if they contain non-alphanumeric characters.
.. _setting-credentials-for-internal-authentication:
Setting credentials for internal authentication
```````````````````````````````````````````````
Use the ``WITH PASSWORD`` clause to set a password for internal authentication, enclosing the password in single
quotation marks.
If internal authentication has not been set up or the role does not have ``LOGIN`` privileges, the ``WITH PASSWORD``
clause is not necessary.
Creating a role conditionally
`````````````````````````````
Attempting to create an existing role results in an invalid query condition unless the ``IF NOT EXISTS`` option is used.
If the option is used and the role exists, the statement is a no-op::
CREATE ROLE other_role;
CREATE ROLE IF NOT EXISTS other_role;
.. _alter-role-statement:
ALTER ROLE
~~~~~~~~~~
Altering a role options uses the ``ALTER ROLE`` statement:
.. productionlist::
alter_role_statement: ALTER ROLE `role_name` WITH `role_options`
For instance::
ALTER ROLE bob WITH PASSWORD = 'PASSWORD_B' AND SUPERUSER = false;
Conditions on executing ``ALTER ROLE`` statements:
- A client must have ``SUPERUSER`` status to alter the ``SUPERUSER`` status of another role
- A client cannot alter the ``SUPERUSER`` status of any role it currently holds
- A client can only modify certain properties of the role with which it identified at login (e.g. ``PASSWORD``)
- To modify properties of a role, the client must be granted ``ALTER`` :ref:`permission <cql-permissions>` on that role
.. _drop-role-statement:
DROP ROLE
~~~~~~~~~
Dropping a role uses the ``DROP ROLE`` statement:
.. productionlist::
drop_role_statement: DROP ROLE [ IF EXISTS ] `role_name`
``DROP ROLE`` requires the client to have ``DROP`` :ref:`permission <cql-permissions>` on the role in question. In
addition, client may not ``DROP`` the role with which it identified at login. Finally, only a client with ``SUPERUSER``
status may ``DROP`` another ``SUPERUSER`` role.
Attempting to drop a role which does not exist results in an invalid query condition unless the ``IF EXISTS`` option is
used. If the option is used and the role does not exist the statement is a no-op.
.. _grant-role-statement:
GRANT ROLE
~~~~~~~~~~
Granting a role to another uses the ``GRANT ROLE`` statement:
.. productionlist::
grant_role_statement: GRANT `role_name` TO `role_name`
For instance::
GRANT report_writer TO alice;
This statement grants the ``report_writer`` role to ``alice``. Any permissions granted to ``report_writer`` are also
acquired by ``alice``.
Roles are modelled as a directed acyclic graph, so circular grants are not permitted. The following examples result in
error conditions::
GRANT role_a TO role_b;
GRANT role_b TO role_a;
GRANT role_a TO role_b;
GRANT role_b TO role_c;
GRANT role_c TO role_a;
.. _revoke-role-statement:
REVOKE ROLE
~~~~~~~~~~~
Revoking a role uses the ``REVOKE ROLE`` statement:
.. productionlist::
revoke_role_statement: REVOKE `role_name` FROM `role_name`
For instance::
REVOKE report_writer FROM alice;
This statement revokes the ``report_writer`` role from ``alice``. Any permissions that ``alice`` has acquired via the
``report_writer`` role are also revoked.
.. _list-roles-statement:
LIST ROLES
~~~~~~~~~~
All the known roles (in the system or granted to specific role) can be listed using the ``LIST ROLES`` statement:
.. productionlist::
list_roles_statement: LIST ROLES [ OF `role_name` ] [ NORECURSIVE ]
For instance::
LIST ROLES;
returns all known roles in the system, this requires ``DESCRIBE`` permission on the database roles resource. And::
LIST ROLES OF alice;
enumerates all roles granted to ``alice``, including those transitively acquired. But::
LIST ROLES OF bob NORECURSIVE
lists all roles directly granted to ``bob`` without including any of the transitively acquired ones.
Users
^^^^^
Prior to the introduction of roles in Cassandra 2.2, authentication and authorization were based around the concept of a
``USER``. For backward compatibility, the legacy syntax has been preserved with ``USER`` centric statements becoming
synonyms for the ``ROLE`` based equivalents. In other words, creating/updating a user is just a different syntax for
creating/updating a role.
.. _create-user-statement:
CREATE USER
~~~~~~~~~~~
Creating a user uses the ``CREATE USER`` statement:
.. productionlist::
create_user_statement: CREATE USER [ IF NOT EXISTS ] `role_name` [ WITH PASSWORD `string` ] [ `user_option` ]
user_option: SUPERUSER | NOSUPERUSER
For instance::
CREATE USER alice WITH PASSWORD 'password_a' SUPERUSER;
CREATE USER bob WITH PASSWORD 'password_b' NOSUPERUSER;
``CREATE USER`` is equivalent to ``CREATE ROLE`` where the ``LOGIN`` option is ``true``. So, the following pairs of
statements are equivalent::
CREATE USER alice WITH PASSWORD 'password_a' SUPERUSER;
CREATE ROLE alice WITH PASSWORD = 'password_a' AND LOGIN = true AND SUPERUSER = true;
CREATE USER IF EXISTS alice WITH PASSWORD 'password_a' SUPERUSER;
CREATE ROLE IF EXISTS alice WITH PASSWORD = 'password_a' AND LOGIN = true AND SUPERUSER = true;
CREATE USER alice WITH PASSWORD 'password_a' NOSUPERUSER;
CREATE ROLE alice WITH PASSWORD = 'password_a' AND LOGIN = true AND SUPERUSER = false;
CREATE USER alice WITH PASSWORD 'password_a' NOSUPERUSER;
CREATE ROLE alice WITH PASSWORD = 'password_a' WITH LOGIN = true;
CREATE USER alice WITH PASSWORD 'password_a';
CREATE ROLE alice WITH PASSWORD = 'password_a' WITH LOGIN = true;
.. _alter-user-statement:
ALTER USER
~~~~~~~~~~
Altering the options of a user uses the ``ALTER USER`` statement:
.. productionlist::
alter_user_statement: ALTER USER `role_name` [ WITH PASSWORD `string` ] [ `user_option` ]
For instance::
ALTER USER alice WITH PASSWORD 'PASSWORD_A';
ALTER USER bob SUPERUSER;
.. _drop-user-statement:
DROP USER
~~~~~~~~~
Dropping a user uses the ``DROP USER`` statement:
.. productionlist::
drop_user_statement: DROP USER [ IF EXISTS ] `role_name`
.. _list-users-statement:
LIST USERS
~~~~~~~~~~
Existing users can be listed using the ``LIST USERS`` statement:
.. productionlist::
list_users_statement: LIST USERS
Note that this statement is equivalent to::
LIST ROLES;
but only roles with the ``LOGIN`` privilege are included in the output.
Data Control
^^^^^^^^^^^^
.. _cql-permissions:
Permissions
~~~~~~~~~~~
Permissions on resources are granted to roles; there are several different types of resources in Cassandra and each type
is modelled hierarchically:
- The hierarchy of Data resources, Keyspaces and Tables has the structure ``ALL KEYSPACES`` -> ``KEYSPACE`` ->
``TABLE``.
- Function resources have the structure ``ALL FUNCTIONS`` -> ``KEYSPACE`` -> ``FUNCTION``
- Resources representing roles have the structure ``ALL ROLES`` -> ``ROLE``
- Resources representing JMX ObjectNames, which map to sets of MBeans/MXBeans, have the structure ``ALL MBEANS`` ->
``MBEAN``
Permissions can be granted at any level of these hierarchies and they flow downwards. So granting a permission on a
resource higher up the chain automatically grants that same permission on all resources lower down. For example,
granting ``SELECT`` on a ``KEYSPACE`` automatically grants it on all ``TABLES`` in that ``KEYSPACE``. Likewise, granting
a permission on ``ALL FUNCTIONS`` grants it on every defined function, regardless of which keyspace it is scoped in. It
is also possible to grant permissions on all functions scoped to a particular keyspace.
Modifications to permissions are visible to existing client sessions; that is, connections need not be re-established
following permissions changes.
The full set of available permissions is:
- ``CREATE``
- ``ALTER``
- ``DROP``
- ``SELECT``
- ``MODIFY``
- ``AUTHORIZE``
- ``DESCRIBE``
- ``EXECUTE``
Not all permissions are applicable to every type of resource. For instance, ``EXECUTE`` is only relevant in the context
of functions or mbeans; granting ``EXECUTE`` on a resource representing a table is nonsensical. Attempting to ``GRANT``
a permission on resource to which it cannot be applied results in an error response. The following illustrates which
permissions can be granted on which types of resource, and which statements are enabled by that permission.
=============== =============================== =======================================================================
Permission Resource Operations
=============== =============================== =======================================================================
``CREATE`` ``ALL KEYSPACES`` ``CREATE KEYSPACE`` and ``CREATE TABLE`` in any keyspace
``CREATE`` ``KEYSPACE`` ``CREATE TABLE`` in specified keyspace
``CREATE`` ``ALL FUNCTIONS`` ``CREATE FUNCTION`` in any keyspace and ``CREATE AGGREGATE`` in any
keyspace
``CREATE`` ``ALL FUNCTIONS IN KEYSPACE`` ``CREATE FUNCTION`` and ``CREATE AGGREGATE`` in specified keyspace
``CREATE`` ``ALL ROLES`` ``CREATE ROLE``
``ALTER`` ``ALL KEYSPACES`` ``ALTER KEYSPACE`` and ``ALTER TABLE`` in any keyspace
``ALTER`` ``KEYSPACE`` ``ALTER KEYSPACE`` and ``ALTER TABLE`` in specified keyspace
``ALTER`` ``TABLE`` ``ALTER TABLE``
``ALTER`` ``ALL FUNCTIONS`` ``CREATE FUNCTION`` and ``CREATE AGGREGATE``: replacing any existing
``ALTER`` ``ALL FUNCTIONS IN KEYSPACE`` ``CREATE FUNCTION`` and ``CREATE AGGREGATE``: replacing existing in
specified keyspace
``ALTER`` ``FUNCTION`` ``CREATE FUNCTION`` and ``CREATE AGGREGATE``: replacing existing
``ALTER`` ``ALL ROLES`` ``ALTER ROLE`` on any role
``ALTER`` ``ROLE`` ``ALTER ROLE``
``DROP`` ``ALL KEYSPACES`` ``DROP KEYSPACE`` and ``DROP TABLE`` in any keyspace
``DROP`` ``KEYSPACE`` ``DROP TABLE`` in specified keyspace
``DROP`` ``TABLE`` ``DROP TABLE``
``DROP`` ``ALL FUNCTIONS`` ``DROP FUNCTION`` and ``DROP AGGREGATE`` in any keyspace
``DROP`` ``ALL FUNCTIONS IN KEYSPACE`` ``DROP FUNCTION`` and ``DROP AGGREGATE`` in specified keyspace
``DROP`` ``FUNCTION`` ``DROP FUNCTION``
``DROP`` ``ALL ROLES`` ``DROP ROLE`` on any role
``DROP`` ``ROLE`` ``DROP ROLE``
``SELECT`` ``ALL KEYSPACES`` ``SELECT`` on any table
``SELECT`` ``KEYSPACE`` ``SELECT`` on any table in specified keyspace
``SELECT`` ``TABLE`` ``SELECT`` on specified table
``SELECT`` ``ALL MBEANS`` Call getter methods on any mbean
``SELECT`` ``MBEANS`` Call getter methods on any mbean matching a wildcard pattern
``SELECT`` ``MBEAN`` Call getter methods on named mbean
``MODIFY`` ``ALL KEYSPACES`` ``INSERT``, ``UPDATE``, ``DELETE`` and ``TRUNCATE`` on any table
``MODIFY`` ``KEYSPACE`` ``INSERT``, ``UPDATE``, ``DELETE`` and ``TRUNCATE`` on any table in
specified keyspace
``MODIFY`` ``TABLE`` ``INSERT``, ``UPDATE``, ``DELETE`` and ``TRUNCATE`` on specified table
``MODIFY`` ``ALL MBEANS`` Call setter methods on any mbean
``MODIFY`` ``MBEANS`` Call setter methods on any mbean matching a wildcard pattern
``MODIFY`` ``MBEAN`` Call setter methods on named mbean
``AUTHORIZE`` ``ALL KEYSPACES`` ``GRANT PERMISSION`` and ``REVOKE PERMISSION`` on any table
``AUTHORIZE`` ``KEYSPACE`` ``GRANT PERMISSION`` and ``REVOKE PERMISSION`` on any table in
specified keyspace
``AUTHORIZE`` ``TABLE`` ``GRANT PERMISSION`` and ``REVOKE PERMISSION`` on specified table
``AUTHORIZE`` ``ALL FUNCTIONS`` ``GRANT PERMISSION`` and ``REVOKE PERMISSION`` on any function
``AUTHORIZE`` ``ALL FUNCTIONS IN KEYSPACE`` ``GRANT PERMISSION`` and ``REVOKE PERMISSION`` in specified keyspace
``AUTHORIZE`` ``FUNCTION`` ``GRANT PERMISSION`` and ``REVOKE PERMISSION`` on specified function
``AUTHORIZE`` ``ALL MBEANS`` ``GRANT PERMISSION`` and ``REVOKE PERMISSION`` on any mbean
``AUTHORIZE`` ``MBEANS`` ``GRANT PERMISSION`` and ``REVOKE PERMISSION`` on any mbean matching
a wildcard pattern
``AUTHORIZE`` ``MBEAN`` ``GRANT PERMISSION`` and ``REVOKE PERMISSION`` on named mbean
``AUTHORIZE`` ``ALL ROLES`` ``GRANT ROLE`` and ``REVOKE ROLE`` on any role
``AUTHORIZE`` ``ROLES`` ``GRANT ROLE`` and ``REVOKE ROLE`` on specified roles
``DESCRIBE`` ``ALL ROLES`` ``LIST ROLES`` on all roles or only roles granted to another,
specified role
``DESCRIBE`` ``ALL MBEANS`` Retrieve metadata about any mbean from the platform's MBeanServer
``DESCRIBE`` ``MBEANS`` Retrieve metadata about any mbean matching a wildcard patter from the
platform's MBeanServer
``DESCRIBE`` ``MBEAN`` Retrieve metadata about a named mbean from the platform's MBeanServer
``EXECUTE`` ``ALL FUNCTIONS`` ``SELECT``, ``INSERT`` and ``UPDATE`` using any function, and use of
any function in ``CREATE AGGREGATE``
``EXECUTE`` ``ALL FUNCTIONS IN KEYSPACE`` ``SELECT``, ``INSERT`` and ``UPDATE`` using any function in specified
keyspace and use of any function in keyspace in ``CREATE AGGREGATE``
``EXECUTE`` ``FUNCTION`` ``SELECT``, ``INSERT`` and ``UPDATE`` using specified function and use
of the function in ``CREATE AGGREGATE``
``EXECUTE`` ``ALL MBEANS`` Execute operations on any mbean
``EXECUTE`` ``MBEANS`` Execute operations on any mbean matching a wildcard pattern
``EXECUTE`` ``MBEAN`` Execute operations on named mbean
=============== =============================== =======================================================================
.. _grant-permission-statement:
GRANT PERMISSION
~~~~~~~~~~~~~~~~
Granting a permission uses the ``GRANT PERMISSION`` statement:
.. productionlist::
grant_permission_statement: GRANT `permissions` ON `resource` TO `role_name`
permissions: ALL [ PERMISSIONS ] | `permission` [ PERMISSION ]
permission: CREATE | ALTER | DROP | SELECT | MODIFY | AUTHORIZE | DESCRIBE | EXECUTE
resource: ALL KEYSPACES
:| KEYSPACE `keyspace_name`
:| [ TABLE ] `table_name`
:| ALL ROLES
:| ROLE `role_name`
:| ALL FUNCTIONS [ IN KEYSPACE `keyspace_name` ]
:| FUNCTION `function_name` '(' [ `cql_type` ( ',' `cql_type` )* ] ')'
:| ALL MBEANS
:| ( MBEAN | MBEANS ) `string`
For instance::
GRANT SELECT ON ALL KEYSPACES TO data_reader;
This gives any user with the role ``data_reader`` permission to execute ``SELECT`` statements on any table across all
keyspaces::
GRANT MODIFY ON KEYSPACE keyspace1 TO data_writer;
This give any user with the role ``data_writer`` permission to perform ``UPDATE``, ``INSERT``, ``UPDATE``, ``DELETE``
and ``TRUNCATE`` queries on all tables in the ``keyspace1`` keyspace::
GRANT DROP ON keyspace1.table1 TO schema_owner;
This gives any user with the ``schema_owner`` role permissions to ``DROP`` ``keyspace1.table1``::
GRANT EXECUTE ON FUNCTION keyspace1.user_function( int ) TO report_writer;
This grants any user with the ``report_writer`` role permission to execute ``SELECT``, ``INSERT`` and ``UPDATE`` queries
which use the function ``keyspace1.user_function( int )``::
GRANT DESCRIBE ON ALL ROLES TO role_admin;
This grants any user with the ``role_admin`` role permission to view any and all roles in the system with a ``LIST
ROLES`` statement
.. _grant-all:
GRANT ALL
`````````
When the ``GRANT ALL`` form is used, the appropriate set of permissions is determined automatically based on the target
resource.
Automatic Granting
``````````````````
When a resource is created, via a ``CREATE KEYSPACE``, ``CREATE TABLE``, ``CREATE FUNCTION``, ``CREATE AGGREGATE`` or
``CREATE ROLE`` statement, the creator (the role the database user who issues the statement is identified as), is
automatically granted all applicable permissions on the new resource.
.. _revoke-permission-statement:
REVOKE PERMISSION
~~~~~~~~~~~~~~~~~
Revoking a permission from a role uses the ``REVOKE PERMISSION`` statement:
.. productionlist::
revoke_permission_statement: REVOKE `permissions` ON `resource` FROM `role_name`
For instance::
REVOKE SELECT ON ALL KEYSPACES FROM data_reader;
REVOKE MODIFY ON KEYSPACE keyspace1 FROM data_writer;
REVOKE DROP ON keyspace1.table1 FROM schema_owner;
REVOKE EXECUTE ON FUNCTION keyspace1.user_function( int ) FROM report_writer;
REVOKE DESCRIBE ON ALL ROLES FROM role_admin;
.. _list-permissions-statement:
LIST PERMISSIONS
~~~~~~~~~~~~~~~~
Listing granted permissions uses the ``LIST PERMISSIONS`` statement:
.. productionlist::
list_permissions_statement: LIST `permissions` [ ON `resource` ] [ OF `role_name` [ NORECURSIVE ] ]
For instance::
LIST ALL PERMISSIONS OF alice;
Show all permissions granted to ``alice``, including those acquired transitively from any other roles::
LIST ALL PERMISSIONS ON keyspace1.table1 OF bob;
Show all permissions on ``keyspace1.table1`` granted to ``bob``, including those acquired transitively from any other
roles. This also includes any permissions higher up the resource hierarchy which can be applied to ``keyspace1.table1``.
For example, should ``bob`` have ``ALTER`` permission on ``keyspace1``, that would be included in the results of this
query. Adding the ``NORECURSIVE`` switch restricts the results to only those permissions which were directly granted to
``bob`` or one of ``bob``'s roles::
LIST SELECT PERMISSIONS OF carlos;
Show any permissions granted to ``carlos`` or any of ``carlos``'s roles, limited to ``SELECT`` permissions on any
resource.

View File

@ -0,0 +1,63 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
.. _cql-triggers:
Triggers
--------
Triggers are identified by a name defined by:
.. productionlist::
trigger_name: `identifier`
.. _create-trigger-statement:
CREATE TRIGGER
^^^^^^^^^^^^^^
Creating a new trigger uses the ``CREATE TRIGGER`` statement:
.. productionlist::
create_trigger_statement: CREATE TRIGGER [ IF NOT EXISTS ] `trigger_name`
: ON `table_name`
: USING `string`
For instance::
CREATE TRIGGER myTrigger ON myTable USING 'org.apache.cassandra.triggers.InvertedIndex';
The actual logic that makes up the trigger can be written in any Java (JVM) language and exists outside the database.
You place the trigger code in a ``lib/triggers`` subdirectory of the Cassandra installation directory, it loads during
cluster startup, and exists on every node that participates in a cluster. The trigger defined on a table fires before a
requested DML statement occurs, which ensures the atomicity of the transaction.
.. _drop-trigger-statement:
DROP TRIGGER
^^^^^^^^^^^^
Dropping a trigger uses the ``DROP TRIGGER`` statement:
.. productionlist::
drop_trigger_statement: DROP TRIGGER [ IF EXISTS ] `trigger_name` ON `table_name`
For instance::
DROP TRIGGER myTrigger ON myTable;

518
doc/source/cql/types.rst Normal file
View File

@ -0,0 +1,518 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: sql
.. _UUID: https://en.wikipedia.org/wiki/Universally_unique_identifier
.. _data-types:
Data Types
----------
CQL is a typed language and supports a rich set of data types, including :ref:`native types <native-types>`,
:ref:`collection types <collections>`, :ref:`user-defined types <udts>`, :ref:`tuple types <tuples>` and :ref:`custom
types <custom-types>`:
.. productionlist::
cql_type: `native_type` | `collection_type` | `user_defined_type` | `tuple_type` | `custom_type`
.. _native-types:
Native Types
^^^^^^^^^^^^
The native types supported by CQL are:
.. productionlist::
native_type: ASCII
: | BIGINT
: | BLOB
: | BOOLEAN
: | COUNTER
: | DATE
: | DECIMAL
: | DOUBLE
: | FLOAT
: | INET
: | INT
: | SMALLINT
: | TEXT
: | TIME
: | TIMESTAMP
: | TIMEUUID
: | TINYINT
: | UUID
: | VARCHAR
: | VARINT
The following table gives additional informations on the native data types, and on which kind of :ref:`constants
<constants>` each type supports:
=============== ===================== ==================================================================================
type constants supported description
=============== ===================== ==================================================================================
``ascii`` :token:`string` ASCII character string
``bigint`` :token:`integer` 64-bit signed long
``blob`` :token:`blob` Arbitrary bytes (no validation)
``boolean`` :token:`boolean` Either ``true`` or ``false``
``counter`` :token:`integer` Counter column (64-bit signed value). See :ref:`counters` for details
``date`` :token:`integer`, A date (with no corresponding time value). See :ref:`dates` below for details
:token:`string`
``decimal`` :token:`integer`, Variable-precision decimal
:token:`float`
``double`` :token:`integer` 64-bit IEEE-754 floating point
:token:`float`
``float`` :token:`integer`, 32-bit IEEE-754 floating point
:token:`float`
``inet`` :token:`string` An IP address, either IPv4 (4 bytes long) or IPv6 (16 bytes long). Note that
there is no ``inet`` constant, IP address should be input as strings
``int`` :token:`integer` 32-bit signed int
``smallint`` :token:`integer` 16-bit signed int
``text`` :token:`string` UTF8 encoded string
``time`` :token:`integer`, A time (with no corresponding date value) with nanosecond precision. See
:token:`string` :ref:`times` below for details
``timestamp`` :token:`integer`, A timestamp (date and time) with millisecond precision. See :ref:`timestamps`
:token:`string` below for details
``timeuuid`` :token:`uuid` Version 1 UUID_, generally used as a “conflict-free” timestamp. Also see
:ref:`timeuuid-functions`
``tinyint`` :token:`integer` 8-bit signed int
``uuid`` :token:`uuid` A UUID_ (of any version)
``varchar`` :token:`string` UTF8 encoded string
``varint`` :token:`integer` Arbitrary-precision integer
=============== ===================== ==================================================================================
.. _counters:
Counters
~~~~~~~~
The ``counter`` type is used to define *counter columns*. A counter column is a column whose value is a 64-bit signed
integer and on which 2 operations are supported: incrementing and decrementing (see the :ref:`UPDATE statement
<update-statement>` for syntax). Note that the value of a counter cannot be set: a counter does not exist until first
incremented/decremented, and that first increment/decrement is made as if the prior value was 0.
.. _counter-limitations:
Counters have a number of important limitations:
- They cannot be used for columns part of the ``PRIMARY KEY`` of a table.
- A table that contains a counter can only contain counters. In other words, either all the columns of a table outside
the ``PRIMARY KEY`` have the ``counter`` type, or none of them have it.
- Counters do not support :ref:`expiration <ttls>`.
- The deletion of counters is supported, but is only guaranteed to work the first time you delete a counter. In other
words, you should not re-update a counter that you have deleted (if you do, proper behavior is not guaranteed).
- Counter updates are, by nature, not `idemptotent <https://en.wikipedia.org/wiki/Idempotence>`__. An important
consequence is that if a counter update fails unexpectedly (timeout or loss of connection to the coordinator node),
the client has no way to know if the update has been applied or not. In particular, replaying the update may or may
not lead to an over count.
.. _timestamps:
Working with timestamps
^^^^^^^^^^^^^^^^^^^^^^^
Values of the ``timestamp`` type are encoded as 64-bit signed integers representing a number of milliseconds since the
standard base time known as `the epoch <https://en.wikipedia.org/wiki/Unix_time>`__: January 1 1970 at 00:00:00 GMT.
Timestamps can be input in CQL either using their value as an :token:`integer`, or using a :token:`string` that
represents an `ISO 8601 <https://en.wikipedia.org/wiki/ISO_8601>`__ date. For instance, all of the values below are
valid ``timestamp`` values for Mar 2, 2011, at 04:05:00 AM, GMT:
- ``1299038700000``
- ``'2011-02-03 04:05+0000'``
- ``'2011-02-03 04:05:00+0000'``
- ``'2011-02-03 04:05:00.000+0000'``
- ``'2011-02-03T04:05+0000'``
- ``'2011-02-03T04:05:00+0000'``
- ``'2011-02-03T04:05:00.000+0000'``
The ``+0000`` above is an RFC 822 4-digit time zone specification; ``+0000`` refers to GMT. US Pacific Standard Time is
``-0800``. The time zone may be omitted if desired (``'2011-02-03 04:05:00'``), and if so, the date will be interpreted
as being in the time zone under which the coordinating Cassandra node is configured. There are however difficulties
inherent in relying on the time zone configuration being as expected, so it is recommended that the time zone always be
specified for timestamps when feasible.
The time of day may also be omitted (``'2011-02-03'`` or ``'2011-02-03+0000'``), in which case the time of day will
default to 00:00:00 in the specified or default time zone. However, if only the date part is relevant, consider using
the :ref:`date <dates>` type.
.. _dates:
Working with dates
^^^^^^^^^^^^^^^^^^
Values of the ``date`` type are encoded as 32-bit unsigned integers representing a number of days with “the epoch” at
the center of the range (2^31). Epoch is January 1st, 1970
As for :ref:`timestamp <timestamps>`, a date can be input either as an :token:`integer` or using a date
:token:`string`. In the later case, the format should be ``yyyy-mm-dd`` (so ``'2011-02-03'`` for instance).
.. _times:
Working with times
^^^^^^^^^^^^^^^^^^
Values of the ``time`` type are encoded as 64-bit signed integers representing the number of nanoseconds since midnight.
As for :ref:`timestamp <timestamps>`, a time can be input either as an :token:`integer` or using a :token:`string`
representing the time. In the later case, the format should be ``hh:mm:ss[.fffffffff]`` (where the sub-second precision
is optional and if provided, can be less than the nanosecond). So for instance, the following are valid inputs for a
time:
- ``'08:12:54'``
- ``'08:12:54.123'``
- ``'08:12:54.123456'``
- ``'08:12:54.123456789'``
.. _collections:
Collections
^^^^^^^^^^^
CQL supports 3 kind of collections: :ref:`maps`, :ref:`sets` and :ref:`lists`. The types of those collections is defined
by:
.. productionlist::
collection_type: MAP '<' `cql_type` ',' `cql_type` '>'
: | SET '<' `cql_type` '>'
: | LIST '<' `cql_type` '>'
and their values can be inputd using collection literals:
.. productionlist::
collection_literal: `map_literal` | `set_literal` | `list_literal`
map_literal: '{' [ `term` ':' `term` (',' `term` : `term`)* ] '}'
set_literal: '{' [ `term` (',' `term`)* ] '}'
list_literal: '[' [ `term` (',' `term`)* ] ']'
Note however that neither :token:`bind_marker` nor ``NULL`` are supported inside collection literals.
Noteworthy characteristics
~~~~~~~~~~~~~~~~~~~~~~~~~~
Collections are meant for storing/denormalizing relatively small amount of data. They work well for things like “the
phone numbers of a given user”, “labels applied to an email”, etc. But when items are expected to grow unbounded (“all
messages sent by a user”, “events registered by a sensor”...), then collections are not appropriate and a specific table
(with clustering columns) should be used. Concretely, (non-frozen) collections have the following noteworthy
characteristics and limitations:
- Individual collections are not indexed internally. Which means that even to access a single element of a collection,
the while collection has to be read (and reading one is not paged internally).
- While insertion operations on sets and maps never incur a read-before-write internally, some operations on lists do.
Further, some lists operations are not idempotent by nature (see the section on :ref:`lists <lists>` below for
details), making their retry in case of timeout problematic. It is thus advised to prefer sets over lists when
possible.
Please note that while some of those limitations may or may not be removed/improved upon in the future, it is a
anti-pattern to use a (single) collection to store large amounts of data.
.. _maps:
Maps
~~~~
A ``map`` is a (sorted) set of key-value pairs, where keys are unique and the map is sorted by its keys. You can define
and insert a map with::
CREATE TABLE users (
id text PRIMARY KEY,
name text,
favs map<text, text> // A map of text keys, and text values
);
INSERT INTO users (id, name, favs)
VALUES ('jsmith', 'John Smith', { 'fruit' : 'Apple', 'band' : 'Beatles' });
// Replace the existing map entirely.
UPDATE users SET favs = { 'fruit' : 'Banana' } WHERE id = 'jsmith';
Further, maps support:
- Updating or inserting one or more elements::
UPDATE users SET favs['author'] = 'Ed Poe' WHERE id = 'jsmith';
UPDATE users SET favs = favs + { 'movie' : 'Cassablanca', 'band' : 'ZZ Top' } WHERE id = 'jsmith';
- Removing one or more element (if an element doesn't exist, removing it is a no-op but no error is thrown)::
DELETE favs['author'] FROM users WHERE id = 'jsmith';
UPDATE users SET favs = favs - { 'movie', 'band'} WHERE id = 'jsmith';
Note that for removing multiple elements in a ``map``, you remove from it a ``set`` of keys.
Lastly, TTLs are allowed for both ``INSERT`` and ``UPDATE``, but in both case the TTL set only apply to the newly
inserted/updated elements. In other words::
UPDATE users USING TTL 10 SET favs['color'] = 'green' WHERE id = 'jsmith';
will only apply the TTL to the ``{ 'color' : 'green' }`` record, the rest of the map remaining unaffected.
.. _sets:
Sets
~~~~
A ``set`` is a (sorted) collection of unique values. You can define and insert a map with::
CREATE TABLE images (
name text PRIMARY KEY,
owner text,
tags set<text> // A set of text values
);
INSERT INTO images (name, owner, tags)
VALUES ('cat.jpg', 'jsmith', { 'pet', 'cute' });
// Replace the existing set entirely
UPDATE images SET tags = { 'kitten', 'cat', 'lol' } WHERE id = 'jsmith';
Further, sets support:
- Adding one or multiple elements (as this is a set, inserting an already existing element is a no-op)::
UPDATE images SET tags = tags + { 'gray', 'cuddly' } WHERE name = 'cat.jpg';
- Removing one or multiple elements (if an element doesn't exist, removing it is a no-op but no error is thrown)::
UPDATE images SET tags = tags - { 'cat' } WHERE name = 'cat.jpg';
Lastly, as for :ref:`maps <maps>`, TTLs if used only apply to the newly inserted values.
.. _lists:
Lists
~~~~~
.. note:: As mentioned above and further discussed at the end of this section, lists have limitations and specific
performance considerations that you should take into account before using them. In general, if you can use a
:ref:`set <sets>` instead of list, always prefer a set.
A ``list`` is a (sorted) collection of non-unique values where elements are ordered by there position in the list. You
can define and insert a list with::
CREATE TABLE plays (
id text PRIMARY KEY,
game text,
players int,
scores list<int> // A list of integers
)
INSERT INTO plays (id, game, players, scores)
VALUES ('123-afde', 'quake', 3, [17, 4, 2]);
// Replace the existing list entirely
UPDATE plays SET scores = [ 3, 9, 4] WHERE id = '123-afde';
Further, lists support:
- Appending and prepending values to a list::
UPDATE plays SET players = 5, scores = scores + [ 14, 21 ] WHERE id = '123-afde';
UPDATE plays SET players = 6, scores = [ 3 ] + scores WHERE id = '123-afde';
- Setting the value at a particular position in the list. This imply that the list has a pre-existing element for that
position or an error will be thrown that the list is too small::
UPDATE plays SET scores[1] = 7 WHERE id = '123-afde';
- Removing an element by its position in the list. This imply that the list has a pre-existing element for that position
or an error will be thrown that the list is too small. Further, as the operation removes an element from the list, the
list size will be diminished by 1, shifting the position of all the elements following the one deleted::
DELETE scores[1] FROM plays WHERE id = '123-afde';
- Deleting *all* the occurrences of particular values in the list (if a particular element doesn't occur at all in the
list, it is simply ignored and no error is thrown)::
UPDATE plays SET scores = scores - [ 12, 21 ] WHERE id = '123-afde';
.. warning:: The append and prepend operations are not idempotent by nature. So in particular, if one of these operation
timeout, then retrying the operation is not safe and it may (or may not) lead to appending/prepending the value
twice.
.. warning:: Setting and removing an element by position and removing occurences of particular values incur an internal
*read-before-write*. They will thus run more slowly and take more ressources than usual updates (with the exclusion
of conditional write that have their own cost).
Lastly, as for :ref:`maps <maps>`, TTLs when used only apply to the newly inserted values.
.. _udts:
User-Defined Types
^^^^^^^^^^^^^^^^^^
CQL support the definition of user-defined types (UDT for short). Such a type can be created, modified and removed using
the :token:`create_type_statement`, :token:`alter_type_statement` and :token:`drop_type_statement` described below. But
once created, a UDT is simply referred to by its name:
.. productionlist::
user_defined_type: `udt_name`
udt_name: [ `keyspace_name` '.' ] `identifier`
Creating a UDT
~~~~~~~~~~~~~~
Creating a new user-defined type is done using a ``CREATE TYPE`` statement defined by:
.. productionlist::
create_type_statement: CREATE TYPE [ IF NOT EXISTS ] `udt_name`
: '(' `field_definition` ( ',' `field_definition` )* ')'
field_definition: `identifier` `cql_type`
A UDT has a name (used to declared columns of that type) and is a set of named and typed fields. Fields name can be any
type, including collections or other UDT. For instance::
CREATE TYPE phone (
country_code int,
number text,
)
CREATE TYPE address (
street text,
city text,
zip int,
phones map<text, phone>
)
CREATE TABLE user (
name text PRIMARY KEY,
addresses map<text, frozen<address>>
)
Note that:
- Attempting to create an already existing type will result in an error unless the ``IF NOT EXISTS`` option is used. If
it is used, the statement will be a no-op if the type already exists.
- A type is intrinsically bound to the keyspace in which it is created, and can only be used in that keyspace. At
creation, if the type name is prefixed by a keyspace name, it is created in that keyspace. Otherwise, it is created in
the current keyspace.
- As of Cassandra |version|, UDT have to be frozen in most cases, hence the ``frozen<address>`` in the table definition
above. Please see the section on :ref:`frozen <frozen>` for more details.
UDT literals
~~~~~~~~~~~~
Once a used-defined type has been created, value can be input using a UDT literal:
.. productionlist::
udt_literal: '{' `identifier` ':' `term` ( ',' `identifier` ':' `term` )* '}'
In other words, a UDT literal is like a :ref:`map <maps>` literal but its keys are the names of the fields of the type.
For instance, one could insert into the table define in the previous section using::
INSERT INTO user (name, addresses)
VALUES ('z3 Pr3z1den7', {
'home' : {
street: '1600 Pennsylvania Ave NW',
city: 'Washington',
zip: '20500',
phones: { 'cell' : { country_code: 1, number: '202 456-1111' },
'landline' : { country_code: 1, number: '...' } }
}
'work' : {
street: '1600 Pennsylvania Ave NW',
city: 'Washington',
zip: '20500',
phones: { 'fax' : { country_code: 1, number: '...' } }
}
})
To be valid, a UDT literal should only include fields defined by the type it is a literal of, but it can omit some field
(in which case those will be ``null``).
Altering a UDT
~~~~~~~~~~~~~~
An existing user-defined type can be modified using an ``ALTER TYPE`` statement:
.. productionlist::
alter_type_statement: ALTER TYPE `udt_name` `alter_type_modification`
alter_type_modification: ALTER `identifier` TYPE `cql_type`
: | ADD `field_definition`
: | RENAME `identifier` TO `identifier` ( `identifier` TO `identifier` )*
You can:
- modify the type of particular field (``ALTER TYPE address ALTER zip TYPE bigint``). The restrictions for such change
are the same than when :ref:`altering the type of column <alter-table-statement>`.
- add a new field to the type (``ALTER TYPE address ADD country text``). That new field will be ``null`` for any values
of the type created before the addition.
- rename the fields of the type (``ALTER TYPE address RENAME zip TO zipcode``).
Dropping a UDT
~~~~~~~~~~~~~~
You can drop an existing user-defined type using a ``DROP TYPE`` statement:
.. productionlist::
drop_type_statement: DROP TYPE [ IF EXISTS ] `udt_name`
Dropping a type results in the immediate, irreversible removal of that type. However, attempting to drop a type that is
still in use by another type, table or function will result in an error.
If the type dropped does not exist, an error will be returned unless ``IF EXISTS`` is used, in which case the operation
is a no-op.
.. _tuples:
Tuples
^^^^^^
CQL also support tuples and tuple types (where the elements can be of different types). Functionally, tuples can be
though as anonymous UDT with anonymous fields. Tuple types and tuple literals are defined by:
.. productionlist::
tuple_type: TUPLE '<' `cql_type` ( ',' `cql_type` )* '>'
tuple_literal: '(' `term` ( ',' `term` )* ')'
and can be used thusly::
CREATE TABLE durations (
event text,
duration tuple<int, text>,
)
INSERT INTO durations (event, duration) VALUES ('ev1', (3, 'hours'));
Unlike other "composed" types (collections and UDT), a tuple is always :ref:`frozen <frozen>` (without the need of the
`frozen` keyword) and it is not possible to update only some elements of a tuple (without updating the whole tuple).
Also, a tuple literal should always have the same number of value than declared in the type it is a tuple of (some of
those values can be null but they need to be explicitly declared as so).
.. _custom-types:
Custom Types
^^^^^^^^^^^^
.. note:: Custom types exists mostly for backward compatiliby purposes and their usage is discouraged. Their usage is
complex, not user friendly and the other provided types, particularly :ref:`user-defined types <udts>`, should almost
always be enough.
A custom type is defined by:
.. productionlist::
custom_type: `string`
A custom type is a :token:`string` that contains the name of Java class that extends the server side ``AbstractType``
class and that can be loaded by Cassandra (it should thus be in the ``CLASSPATH`` of every node running Cassandra). That
class will define what values are valid for the type and how the time sorts when used for a clustering column. For any
other purpose, a value of a custom type is the same than that of a ``blob``, and can in particular be input using the
:token:`blob` literal syntax.

View File

@ -0,0 +1,20 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Data Modeling
=============
.. todo:: TODO

20
doc/source/faq/index.rst Normal file
View File

@ -0,0 +1,20 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Frequently Asked Questions
==========================
.. TODO: todo

View File

@ -0,0 +1,67 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Configuring Cassandra
---------------------
For running Cassandra on a single node, the steps above are enough, you don't really need to change any configuration.
However, when you deploy a cluster of nodes, or use clients that are not on the same host, then there are some
parameters that must be changed.
The Cassandra configuration files can be found in the ``conf`` directory of tarballs. For packages, the configuration
files will be located in ``/etc/cassandra``.
Main runtime properties
^^^^^^^^^^^^^^^^^^^^^^^
Most of configuration in Cassandra is done via yaml properties that can be set in ``cassandra.yaml``. At a minimum you
should consider setting the following properties:
- ``cluster_name``: the name of your cluster.
- ``seeds``: a comma separated list of the IP addresses of your cluster seeds.
- ``storage_port``: you don't necessarily need to change this but make sure that there are no firewalls blocking this
port.
- ``listen_address``: the IP address of your node, this is what allows other nodes to communicate with this node so it
is important that you change it. Alternatively, you can set ``listen_interface`` to tell Cassandra which interface to
use, and consecutively which address to use. Set only one, not both.
- ``native_transport_port``: as for storage\_port, make sure this port is not blocked by firewalls as clients will
communicate with Cassandra on this port.
Changing the location of directories
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The following yaml properties control the location of directories:
- ``data_file_directories``: one or more directories where data files are located.
- ``commitlog_directory``: the directory where commitlog files are located.
- ``saved_caches_directory``: the directory where saved caches are located.
- ``hints_directory``: the directory where hints are located.
For performance reasons, if you have multiple disks, consider putting commitlog and data files on different disks.
Environment variables
^^^^^^^^^^^^^^^^^^^^^
JVM-level settings such as heap size can be set in ``cassandra-env.sh``. You can add any additional JVM command line
argument to the ``JVM_OPTS`` environment variable; when Cassandra starts these arguments will be passed to the JVM.
Logging
^^^^^^^
The logger in use is logback. You can change logging properties by editing ``logback.xml``. By default it will log at
INFO level into a file called ``system.log`` and at debug level into a file called ``debug.log``. When running in the
foreground, it will also log at INFO level to the console.

View File

@ -0,0 +1,107 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. _client-drivers:
Client drivers
--------------
Here are known Cassandra client drivers organized by language. Before choosing a driver, you should verify the Cassandra
version and functionality supported by a specific driver.
Java
^^^^
- `Achilles <http://achilles.archinnov.info/>`__
- `Astyanax <https://github.com/Netflix/astyanax/wiki/Getting-Started>`__
- `Casser <https://github.com/noorq/casser>`__
- `Datastax Java driver <https://github.com/datastax/java-driver>`__
- `Kundera <https://github.com/impetus-opensource/Kundera>`__
- `PlayORM <https://github.com/deanhiller/playorm>`__
Python
^^^^^^
- `Datastax Python driver <https://github.com/datastax/python-driver>`__
Ruby
^^^^
- `Datastax Ruby driver <https://github.com/datastax/ruby-driver>`__
C# / .NET
^^^^^^^^^
- `Cassandra Sharp <https://github.com/pchalamet/cassandra-sharp>`__
- `Datastax C# driver <https://github.com/datastax/csharp-driver>`__
- `Fluent Cassandra <https://github.com/managedfusion/fluentcassandra>`__
Nodejs
^^^^^^
- `Datastax Nodejs driver <https://github.com/datastax/nodejs-driver>`__
- `Node-Cassandra-CQL <https://github.com/jorgebay/node-cassandra-cql>`__
PHP
^^^
- `CQL \| PHP <http://code.google.com/a/apache-extras.org/p/cassandra-pdo>`__
- `Datastax PHP driver <https://github.com/datastax/php-driver/>`__
- `PHP-Cassandra <https://github.com/aparkhomenko/php-cassandra>`__
- `PHP Library for Cassandra <http://evseevnn.github.io/php-cassandra-binary/>`__
C++
^^^
- `Datastax C++ driver <https://github.com/datastax/cpp-driver>`__
- `libQTCassandra <http://sourceforge.net/projects/libqtcassandra>`__
Scala
^^^^^
- `Datastax Spark connector <https://github.com/datastax/spark-cassandra-connector>`__
- `Phantom <https://github.com/newzly/phantom>`__
- `Quill <https://github.com/getquill/quill>`__
Clojure
^^^^^^^
- `Alia <https://github.com/mpenet/alia>`__
- `Cassaforte <https://github.com/clojurewerkz/cassaforte>`__
- `Hayt <https://github.com/mpenet/hayt>`__
Erlang
^^^^^^
- `CQerl <https://github.com/matehat/cqerl>`__
- `Erlcass <https://github.com/silviucpp/erlcass>`__
Go
^^
- `CQLc <http://relops.com/cqlc/>`__
- `Gocassa <https://github.com/hailocab/gocassa>`__
- `GoCQL <https://github.com/gocql/gocql>`__
Haskell
^^^^^^^
- `Cassy <https://github.com/ozataman/cassy>`__
Rust
^^^^
- `Rust CQL <https://github.com/neich/rust-cql>`__

View File

@ -0,0 +1,33 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Getting Started
===============
This section covers how to get started using Apache Cassandra and should be the first thing to read if you are new to
Cassandra.
.. toctree::
:maxdepth: 2
installing
configuring
querying
drivers

View File

@ -0,0 +1,99 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Installing Cassandra
--------------------
Prerequisites
^^^^^^^^^^^^^
- The latest version of Java 8, either the `Oracle Java Standard Edition 8
<http://www.oracle.com/technetwork/java/javase/downloads/index.html>`__ or `OpenJDK 8 <http://openjdk.java.net/>`__. To
verify that you have the correct version of java installed, type ``java -version``.
- For using cqlsh, the latest version of `Python 2.7 <https://www.python.org/downloads/>`__. To verify that you have
the correct version of Python installed, type ``python --version``.
Installation from binary tarball files
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Download the latest stable release from the `Apache Cassandra downloads website <http://cassandra.apache.org/download/>`__.
- Untar the file somewhere, for example:
::
tar -xvf apache-cassandra-3.6-bin.tar.gz cassandra
The files will be extracted into ``apache-cassandra-3.6``, you need to substitute 3.6 with the release number that you
have downloaded.
- Optionally add ``apache-cassandra-3.6\bin`` to your path.
- Start Cassandra in the foreground by invoking ``bin/cassandra -f`` from the command line. Press "Control-C" to stop
Cassandra. Start Cassandra in the background by invoking ``bin/cassandra`` from the command line. Invoke ``kill pid``
or ``pkill -f CassandraDaemon`` to stop Cassandra, where pid is the Cassandra process id, which you can find for
example by invoking ``pgrep -f CassandraDaemon``.
- Verify that Cassandra is running by invoking ``bin/nodetool status`` from the command line.
- Configuration files are located in the ``conf`` sub-directory.
- Since Cassandra 2.1, log and data directories are located in the ``logs`` and ``data`` sub-directories respectively.
Older versions defaulted to ``/var/log/cassandra`` and ``/var/lib/cassandra``. Due to this, it is necessary to either
start Cassandra with root privileges or change ``conf/cassandra.yaml`` to use directories owned by the current user,
as explained below in the section on changing the location of directories.
Installation from Debian packages
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- Add the Apache repository of Cassandra to ``/etc/apt/sources.list.d/cassandra.sources.list``, for example for version
3.6:
::
echo "deb http://www.apache.org/dist/cassandra/debian 36x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
- Update the repositories:
::
sudo apt-get update
- If you encounter this error:
::
GPG error: http://www.apache.org 36x InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 749D6EEC0353B12C
Then add the public key 749D6EEC0353B12C as follows:
::
gpg --keyserver pgp.mit.edu --recv-keys 749D6EEC0353B12C
gpg --export --armor 749D6EEC0353B12C | sudo apt-key add -
and repeat ``sudo apt-get update``. The actual key may be different, you get it from the error message itself. For a
full list of Apache contributors public keys, you can refer to `this link <https://www.apache.org/dist/cassandra/KEYS>`__.
- Install Cassandra:
::
sudo apt-get install cassandra
- You can start Cassandra with ``sudo service cassandra start`` and stop it with ``sudo service cassandra stop``.
However, normally the service will start automatically. For this reason be sure to stop it if you need to make any
configuration changes.
- Verify that Cassandra is running by invoking ``nodetool status`` from the command line.
- The default location of configuration files is ``/etc/cassandra``.
- The default location of log and data directories is ``/var/log/cassandra/`` and ``/var/lib/cassandra``.

View File

@ -0,0 +1,52 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Inserting and querying
----------------------
The API to Cassandra is :ref:`CQL <cql>`, the Cassandra Query Language. To use CQL, you will need to connect to the
cluster, which can be done:
- either using cqlsh,
- or through a client driver for Cassandra.
CQLSH
^^^^^
cqlsh is a command line shell for interacting with Cassandra through CQL. It is shipped with every Cassandra package,
and can be found in the bin/ directory alongside the cassandra executable. It connects to the single node specified on
the command line. For example::
$ bin/cqlsh localhost
Connected to Test Cluster at localhost:9042.
[cqlsh 5.0.1 | Cassandra 3.8 | CQL spec 3.4.2 | Native protocol v4]
Use HELP for help.
cqlsh> SELECT cluster_name, listen_address FROM system.local;
cluster_name | listen_address
--------------+----------------
Test Cluster | 127.0.0.1
(1 rows)
cqlsh>
See the :ref:`cqlsh section <cqlsh>` for full documentation.
Client drivers
^^^^^^^^^^^^^^
A lot of client drivers are provided by the Community and a list of known drivers is provided in :ref:`the next section
<client-drivers>`. You should refer to the documentation of each drivers for more information on how to use them.

40
doc/source/index.rst Normal file
View File

@ -0,0 +1,40 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Welcome to Apache Cassandra's documentation!
============================================
This is the official documentation for `Apache Cassandra <http://cassandra.apache.org>`__ |version|. If you would like
to contribute to this documentation, you are welcome to do so by submitting your contribution like any other patch
following `these instructions <https://wiki.apache.org/cassandra/HowToContribute>`__.
Contents:
.. toctree::
:maxdepth: 2
getting_started/index
architecture/index
data_modeling/index
cql/index
configuration/index
operating/index
tools/index
troubleshooting/index
faq/index
bugs
contactus

View File

@ -0,0 +1,22 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Backups
=======
.. todo:: TODO

View File

@ -0,0 +1,65 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Bloom Filters
-------------
In the read path, Cassandra merges data on disk (in SSTables) with data in RAM (in memtables). To avoid checking every
SSTable data file for the partition being requested, Cassandra employs a data structure known as a bloom filter.
Bloom filters are a probabilistic data structure that allows Cassandra to determine one of two possible states: - The
data definitely does not exist in the given file, or - The data probably exists in the given file.
While bloom filters can not guarantee that the data exists in a given SSTable, bloom filters can be made more accurate
by allowing them to consume more RAM. Operators have the opportunity to tune this behavior per table by adjusting the
the ``bloom_filter_fp_chance`` to a float between 0 and 1.
The default value for ``bloom_filter_fp_chance`` is 0.1 for tables using LeveledCompactionStrategy and 0.01 for all
other cases.
Bloom filters are stored in RAM, but are stored offheap, so operators should not consider bloom filters when selecting
the maximum heap size. As accuracy improves (as the ``bloom_filter_fp_chance`` gets closer to 0), memory usage
increases non-linearly - the bloom filter for ``bloom_filter_fp_chance = 0.01`` will require about three times as much
memory as the same table with ``bloom_filter_fp_chance = 0.1``.
Typical values for ``bloom_filter_fp_chance`` are usually between 0.01 (1%) to 0.1 (10%) false-positive chance, where
Cassandra may scan an SSTable for a row, only to find that it does not exist on the disk. The parameter should be tuned
by use case:
- Users with more RAM and slower disks may benefit from setting the ``bloom_filter_fp_chance`` to a numerically lower
number (such as 0.01) to avoid excess IO operations
- Users with less RAM, more dense nodes, or very fast disks may tolerate a higher ``bloom_filter_fp_chance`` in order to
save RAM at the expense of excess IO operations
- In workloads that rarely read, or that only perform reads by scanning the entire data set (such as analytics
workloads), setting the ``bloom_filter_fp_chance`` to a much higher number is acceptable.
Changing
^^^^^^^^
The bloom filter false positive chance is visible in the ``DESCRIBE TABLE`` output as the field
``bloom_filter_fp_chance``. Operators can change the value with an ``ALTER TABLE`` statement:
::
ALTER TABLE keyspace.table WITH bloom_filter_fp_chance=0.01
Operators should be aware, however, that this change is not immediate: the bloom filter is calculated when the file is
written, and persisted on disk as the Filter component of the SSTable. Upon issuing an ``ALTER TABLE`` statement, new
files on disk will be written with the new ``bloom_filter_fp_chance``, but existing sstables will not be modified until
they are compacted - if an operator needs a change to ``bloom_filter_fp_chance`` to take effect, they can trigger an
SSTable rewrite using ``nodetool scrub`` or ``nodetool upgradesstables -a``, both of which will rebuild the sstables on
disk, regenerating the bloom filters in the progress.

View File

@ -0,0 +1,89 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Change Data Capture
-------------------
Overview
^^^^^^^^
Change data capture (CDC) provides a mechanism to flag specific tables for archival as well as rejecting writes to those
tables once a configurable size-on-disk for the combined flushed and unflushed CDC-log is reached. An operator can
enable CDC on a table by setting the table property ``cdc=true`` (either when :ref:`creating the table
<create-table-statement>` or :ref:`altering it <alter-table-statement>`), after which any CommitLogSegments containing
data for a CDC-enabled table are moved to the directory specified in ``cassandra.yaml`` on segment discard. A threshold
of total disk space allowed is specified in the yaml at which time newly allocated CommitLogSegments will not allow CDC
data until a consumer parses and removes data from the destination archival directory.
Configuration
^^^^^^^^^^^^^
Enabling or disable CDC on a table
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CDC is enable or disable through the `cdc` table property, for instance::
CREATE TABLE foo (a int, b text, PRIMARY KEY(a)) WITH cdc=true;
ALTER TABLE foo WITH cdc=true;
ALTER TABLE foo WITH cdc=false;
cassandra.yaml parameters
~~~~~~~~~~~~~~~~~~~~~~~~~
The following `cassandra.yaml` are available for CDC:
``cdc_enabled`` (default: false)
Enable or disable CDC operations node-wide.
``cdc_raw_directory`` (default: ``$CASSANDRA_HOME/data/cdc_raw``)
Destination for CommitLogSegments to be moved after all corresponding memtables are flushed.
``cdc_free_space_in_mb``: (default: min of 4096 and 1/8th volume space)
Calculated as sum of all active CommitLogSegments that permit CDC + all flushed CDC segments in
``cdc_raw_directory``.
``cdc_free_space_check_interval_ms`` (default: 250)
When at capacity, we limit the frequency with which we re-calculate the space taken up by ``cdc_raw_directory`` to
prevent burning CPU cycles unnecessarily. Default is to check 4 times per second.
.. _reading-commitlogsegments:
Reading CommitLogSegments
^^^^^^^^^^^^^^^^^^^^^^^^^
This implementation included a refactor of CommitLogReplayer into `CommitLogReader.java
<https://github.com/apache/cassandra/blob/e31e216234c6b57a531cae607e0355666007deb2/src/java/org/apache/cassandra/db/commitlog/CommitLogReader.java>`__.
Usage is `fairly straightforward
<https://github.com/apache/cassandra/blob/e31e216234c6b57a531cae607e0355666007deb2/src/java/org/apache/cassandra/db/commitlog/CommitLogReplayer.java#L132-L140>`__
with a `variety of signatures
<https://github.com/apache/cassandra/blob/e31e216234c6b57a531cae607e0355666007deb2/src/java/org/apache/cassandra/db/commitlog/CommitLogReader.java#L71-L103>`__
available for use. In order to handle mutations read from disk, implement `CommitLogReadHandler
<https://github.com/apache/cassandra/blob/e31e216234c6b57a531cae607e0355666007deb2/src/java/org/apache/cassandra/db/commitlog/CommitLogReadHandler.java>`__.
Warnings
^^^^^^^^
**Do not enable CDC without some kind of consumption process in-place.**
The initial implementation of Change Data Capture does not include a parser (see :ref:`reading-commitlogsegments` above)
so, if CDC is enabled on a node and then on a table, the ``cdc_free_space_in_mb`` will fill up and then writes to
CDC-enabled tables will be rejected unless some consumption process is in place.
Further Reading
^^^^^^^^^^^^^^^
- `Design doc <https://docs.google.com/document/d/1ZxCWYkeZTquxsvf5hdPc0fiUnUHna8POvgt6TIzML4Y/edit>`__
- `JIRA ticket <https://issues.apache.org/jira/browse/CASSANDRA-8844>`__

View File

@ -0,0 +1,432 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
.. _compaction:
Compaction
----------
Types of compaction
^^^^^^^^^^^^^^^^^^^
The concept of compaction is used for different kinds of operations in Cassandra, the common thing about these
operations is that it takes one or more sstables and output new sstables. The types of compactions are;
Minor compaction
triggered automatically in Cassandra.
Major compaction
a user executes a compaction over all sstables on the node.
User defined compaction
a user triggers a compaction on a given set of sstables.
Scrub
try to fix any broken sstables. This can actually remove valid data if that data is corrupted, if that happens you
will need to run a full repair on the node.
Upgradesstables
upgrade sstables to the latest version. Run this after upgrading to a new major version.
Cleanup
remove any ranges this node does not own anymore, typically triggered on neighbouring nodes after a node has been
bootstrapped since that node will take ownership of some ranges from those nodes.
Secondary index rebuild
rebuild the secondary indexes on the node.
Anticompaction
after repair the ranges that were actually repaired are split out of the sstables that existed when repair started.
When is a minor compaction triggered?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# When an sstable is added to the node through flushing/streaming etc.
# When autocompaction is enabled after being disabled (``nodetool enableautocompaction``)
# When compaction adds new sstables.
# A check for new minor compactions every 5 minutes.
Merging sstables
^^^^^^^^^^^^^^^^
Compaction is about merging sstables, since partitions in sstables are sorted based on the hash of the partition key it
is possible to efficiently merge separate sstables. Content of each partition is also sorted so each partition can be
merged efficiently.
Tombstones and Garbage Collection (GC) Grace
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Why Tombstones
~~~~~~~~~~~~~~
When a delete request is received by Cassandra it does not actually remove the data from the underlying store. Instead
it writes a special piece of data known as a tombstone. The Tombstone represents the delete and causes all values which
occurred before the tombstone to not appear in queries to the database. This approach is used instead of removing values
because of the distributed nature of Cassandra.
Deletes without tombstones
~~~~~~~~~~~~~~~~~~~~~~~~~~
Imagine a three node cluster which has the value [A] replicated to every node.::
[A], [A], [A]
If one of the nodes fails and and our delete operation only removes existing values we can end up with a cluster that
looks like::
[], [], [A]
Then a repair operation would replace the value of [A] back onto the two
nodes which are missing the value.::
[A], [A], [A]
This would cause our data to be resurrected even though it had been
deleted.
Deletes with Tombstones
~~~~~~~~~~~~~~~~~~~~~~~
Starting again with a three node cluster which has the value [A] replicated to every node.::
[A], [A], [A]
If instead of removing data we add a tombstone record, our single node failure situation will look like this.::
[A, Tombstone[A]], [A, Tombstone[A]], [A]
Now when we issue a repair the Tombstone will be copied to the replica, rather than the deleted data being
resurrected.::
[A, Tombstone[A]], [A, Tombstone[A]], [A, Tombstone[A]]
Our repair operation will correctly put the state of the system to what we expect with the record [A] marked as deleted
on all nodes. This does mean we will end up accruing Tombstones which will permanently accumulate disk space. To avoid
keeping tombstones forever we have a parameter known as ``gc_grace_seconds`` for every table in Cassandra.
The gc_grace_seconds parameter and Tombstone Removal
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The table level ``gc_grace_seconds`` parameter controls how long Cassandra will retain tombstones through compaction
events before finally removing them. This duration should directly reflect the amount of time a user expects to allow
before recovering a failed node. After ``gc_grace_seconds`` has expired the tombstone may be removed (meaning there will
no longer be any record that a certain piece of data was deleted), but as a tombstone can live in one sstable and the
data it covers in another, a compaction must also include both sstable for a tombstone to be removed. More precisely, to
be able to drop an actual tombstone the following needs to be true;
- The tombstone must be older than ``gc_grace_seconds``
- If partition X contains the tombstone, the sstable containing the partition plus all sstables containing data older
than the tombstone containing X must be included in the same compaction. We don't need to care if the partition is in
an sstable if we can guarantee that all data in that sstable is newer than the tombstone. If the tombstone is older
than the data it cannot shadow that data.
- If the option ``only_purge_repaired_tombstones`` is enabled, tombstones are only removed if the data has also been
repaired.
If a node remains down or disconnected for longer than ``gc_grace_seconds`` it's deleted data will be repaired back to
the other nodes and re-appear in the cluster. This is basically the same as in the "Deletes without Tombstones" section.
Note that tombstones will not be removed until a compaction event even if ``gc_grace_seconds`` has elapsed.
The default value for ``gc_grace_seconds`` is 864000 which is equivalent to 10 days. This can be set when creating or
altering a table using ``WITH gc_grace_seconds``.
TTL
^^^
Data in Cassandra can have an additional property called time to live - this is used to automatically drop data that has
expired once the time is reached. Once the TTL has expired the data is converted to a tombstone which stays around for
at least ``gc_grace_seconds``. Note that if you mix data with TTL and data without TTL (or just different length of the
TTL) Cassandra will have a hard time dropping the tombstones created since the partition might span many sstables and
not all are compacted at once.
Fully expired sstables
^^^^^^^^^^^^^^^^^^^^^^
If an sstable contains only tombstones and it is guaranteed that that sstable is not shadowing data in any other sstable
compaction can drop that sstable. If you see sstables with only tombstones (note that TTL:ed data is considered
tombstones once the time to live has expired) but it is not being dropped by compaction, it is likely that other
sstables contain older data. There is a tool called ``sstableexpiredblockers`` that will list which sstables are
droppable and which are blocking them from being dropped. This is especially useful for time series compaction with
``TimeWindowCompactionStrategy`` (and the deprecated ``DateTieredCompactionStrategy``).
Repaired/unrepaired data
^^^^^^^^^^^^^^^^^^^^^^^^
With incremental repairs Cassandra must keep track of what data is repaired and what data is unrepaired. With
anticompaction repaired data is split out into repaired and unrepaired sstables. To avoid mixing up the data again
separate compaction strategy instances are run on the two sets of data, each instance only knowing about either the
repaired or the unrepaired sstables. This means that if you only run incremental repair once and then never again, you
might have very old data in the repaired sstables that block compaction from dropping tombstones in the unrepaired
(probably newer) sstables.
Data directories
^^^^^^^^^^^^^^^^
Since tombstones and data can live in different sstables it is important to realize that losing an sstable might lead to
data becoming live again - the most common way of losing sstables is to have a hard drive break down. To avoid making
data live tombstones and actual data are always in the same data directory. This way, if a disk is lost, all versions of
a partition are lost and no data can get undeleted. To achieve this a compaction strategy instance per data directory is
run in addition to the compaction strategy instances containing repaired/unrepaired data, this means that if you have 4
data directories there will be 8 compaction strategy instances running. This has a few more benefits than just avoiding
data getting undeleted:
- It is possible to run more compactions in parallel - leveled compaction will have several totally separate levelings
and each one can run compactions independently from the others.
- Users can backup and restore a single data directory.
- Note though that currently all data directories are considered equal, so if you have a tiny disk and a big disk
backing two data directories, the big one will be limited the by the small one. One work around to this is to create
more data directories backed by the big disk.
Single sstable tombstone compaction
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
When an sstable is written a histogram with the tombstone expiry times is created and this is used to try to find
sstables with very many tombstones and run single sstable compaction on that sstable in hope of being able to drop
tombstones in that sstable. Before starting this it is also checked how likely it is that any tombstones will actually
will be able to be dropped how much this sstable overlaps with other sstables. To avoid most of these checks the
compaction option ``unchecked_tombstone_compaction`` can be enabled.
.. _compaction-options:
Common options
^^^^^^^^^^^^^^
There is a number of common options for all the compaction strategies;
``enabled`` (default: true)
Whether minor compactions should run. Note that you can have 'enabled': true as a compaction option and then do
'nodetool enableautocompaction' to start running compactions.
``tombstone_threshold`` (default: 0.2)
How much of the sstable should be tombstones for us to consider doing a single sstable compaction of that sstable.
``tombstone_compaction_interval`` (default: 86400s (1 day))
Since it might not be possible to drop any tombstones when doing a single sstable compaction we need to make sure
that one sstable is not constantly getting recompacted - this option states how often we should try for a given
sstable.
``log_all`` (default: false)
New detailed compaction logging, see :ref:`below <detailed-compaction-logging>`.
``unchecked_tombstone_compaction`` (default: false)
The single sstable compaction has quite strict checks for whether it should be started, this option disables those
checks and for some usecases this might be needed. Note that this does not change anything for the actual
compaction, tombstones are only dropped if it is safe to do so - it might just rewrite an sstable without being able
to drop any tombstones.
``only_purge_repaired_tombstone`` (default: false)
Option to enable the extra safety of making sure that tombstones are only dropped if the data has been repaired.
``min_threshold`` (default: 4)
Lower limit of number of sstables before a compaction is triggered. Not used for ``LeveledCompactionStrategy``.
``max_threshold`` (default: 32)
Upper limit of number of sstables before a compaction is triggered. Not used for ``LeveledCompactionStrategy``.
Further, see the section on each strategy for specific additional options.
Compaction nodetool commands
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The :ref:`nodetool <nodetool>` utility provides a number of commands related to compaction:
``enableautocompaction``
Enable compaction.
``disableautocompaction``
Disable compaction.
``setcompactionthroughput``
How fast compaction should run at most - defaults to 16MB/s, but note that it is likely not possible to reach this
throughput.
``compactionstats``
Statistics about current and pending compactions.
``compactionhistory``
List details about the last compactions.
``setcompactionthreshold``
Set the min/max sstable count for when to trigger compaction, defaults to 4/32.
Switching the compaction strategy and options using JMX
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
It is possible to switch compaction strategies and its options on just a single node using JMX, this is a great way to
experiment with settings without affecting the whole cluster. The mbean is::
org.apache.cassandra.db:type=ColumnFamilies,keyspace=<keyspace_name>,columnfamily=<table_name>
and the attribute to change is ``CompactionParameters`` or ``CompactionParametersJson`` if you use jconsole or jmc. The
syntax for the json version is the same as you would use in an :ref:`ALTER TABLE <alter-table-statement>` statement -
for example::
{ 'class': 'LeveledCompactionStrategy', 'sstable_size_in_mb': 123 }
The setting is kept until someone executes an :ref:`ALTER TABLE <alter-table-statement>` that touches the compaction
settings or restarts the node.
.. _detailed-compaction-logging:
More detailed compaction logging
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Enable with the compaction option ``log_all`` and a more detailed compaction log file will be produced in your log
directory.
.. _STCS:
Size Tiered Compaction Strategy
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The basic idea of ``SizeTieredCompactionStrategy`` (STCS) is to merge sstables of approximately the same size. All
sstables are put in different buckets depending on their size. An sstable is added to the bucket if size of the sstable
is within ``bucket_low`` and ``bucket_high`` of the current average size of the sstables already in the bucket. This
will create several buckets and the most interesting of those buckets will be compacted. The most interesting one is
decided by figuring out which bucket's sstables takes the most reads.
Major compaction
~~~~~~~~~~~~~~~~
When running a major compaction with STCS you will end up with two sstables per data directory (one for repaired data
and one for unrepaired data). There is also an option (-s) to do a major compaction that splits the output into several
sstables. The sizes of the sstables are approximately 50%, 25%, 12.5%... of the total size.
.. _stcs-options:
STCS options
~~~~~~~~~~~~
``min_sstable_size`` (default: 50MB)
Sstables smaller than this are put in the same bucket.
``bucket_low`` (default: 0.5)
How much smaller than the average size of a bucket a sstable should be before not being included in the bucket. That
is, if ``bucket_low * avg_bucket_size < sstable_size`` (and the ``bucket_high`` condition holds, see below), then
the sstable is added to the bucket.
``bucket_high`` (default: 1.5)
How much bigger than the average size of a bucket a sstable should be before not being included in the bucket. That
is, if ``sstable_size < bucket_high * avg_bucket_size`` (and the ``bucket_low`` condition holds, see above), then
the sstable is added to the bucket.
Defragmentation
~~~~~~~~~~~~~~~
Defragmentation is done when many sstables are touched during a read. The result of the read is put in to the memtable
so that the next read will not have to touch as many sstables. This can cause writes on a read-only-cluster.
.. _LCS:
Leveled Compaction Strategy
^^^^^^^^^^^^^^^^^^^^^^^^^^^
The idea of ``LeveledCompactionStrategy`` (LCS) is that all sstables are put into different levels where we guarantee
that no overlapping sstables are in the same level. By overlapping we mean that the first/last token of a single sstable
are never overlapping with other sstables. This means that for a SELECT we will only have to look for the partition key
in a single sstable per level. Each level is 10x the size of the previous one and each sstable is 160MB by default. L0
is where sstables are streamed/flushed - no overlap guarantees are given here.
When picking compaction candidates we have to make sure that the compaction does not create overlap in the target level.
This is done by always including all overlapping sstables in the next level. For example if we select an sstable in L3,
we need to guarantee that we pick all overlapping sstables in L4 and make sure that no currently ongoing compactions
will create overlap if we start that compaction. We can start many parallel compactions in a level if we guarantee that
we wont create overlap. For L0 -> L1 compactions we almost always need to include all L1 sstables since most L0 sstables
cover the full range. We also can't compact all L0 sstables with all L1 sstables in a single compaction since that can
use too much memory.
When deciding which level to compact LCS checks the higher levels first (with LCS, a "higher" level is one with a higher
number, L0 being the lowest one) and if the level is behind a compaction will be started in that level.
Major compaction
~~~~~~~~~~~~~~~~
It is possible to do a major compaction with LCS - it will currently start by filling out L1 and then once L1 is full,
it continues with L2 etc. This is sub optimal and will change to create all the sstables in a high level instead,
CASSANDRA-11817.
Bootstrapping
~~~~~~~~~~~~~
During bootstrap sstables are streamed from other nodes. The level of the remote sstable is kept to avoid many
compactions after the bootstrap is done. During bootstrap the new node also takes writes while it is streaming the data
from a remote node - these writes are flushed to L0 like all other writes and to avoid those sstables blocking the
remote sstables from going to the correct level, we only do STCS in L0 until the bootstrap is done.
STCS in L0
~~~~~~~~~~
If LCS gets very many L0 sstables reads are going to hit all (or most) of the L0 sstables since they are likely to be
overlapping. To more quickly remedy this LCS does STCS compactions in L0 if there are more than 32 sstables there. This
should improve read performance more quickly compared to letting LCS do its L0 -> L1 compactions. If you keep getting
too many sstables in L0 it is likely that LCS is not the best fit for your workload and STCS could work out better.
Starved sstables
~~~~~~~~~~~~~~~~
If a node ends up with a leveling where there are a few very high level sstables that are not getting compacted they
might make it impossible for lower levels to drop tombstones etc. For example, if there are sstables in L6 but there is
only enough data to actually get a L4 on the node the left over sstables in L6 will get starved and not compacted. This
can happen if a user changes sstable\_size\_in\_mb from 5MB to 160MB for example. To avoid this LCS tries to include
those starved high level sstables in other compactions if there has been 25 compaction rounds where the highest level
has not been involved.
.. _lcs-options:
LCS options
~~~~~~~~~~~
``sstable_size_in_mb`` (default: 160MB)
The target compressed (if using compression) sstable size - the sstables can end up being larger if there are very
large partitions on the node.
LCS also support the ``cassandra.disable_stcs_in_l0`` startup option (``-Dcassandra.disable_stcs_in_l0=true``) to avoid
doing STCS in L0.
.. _TWCS:
Time Window CompactionStrategy
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
``TimeWindowCompactionStrategy`` (TWCS) is designed specifically for workloads where it's beneficial to have data on
disk grouped by the timestamp of the data, a common goal when the workload is time-series in nature or when all data is
written with a TTL. In an expiring/TTL workload, the contents of an entire SSTable likely expire at approximately the
same time, allowing them to be dropped completely, and space reclaimed much more reliably than when using
``SizeTieredCompactionStrategy`` or ``LeveledCompactionStrategy``. The basic concept is that
``TimeWindowCompactionStrategy`` will create 1 sstable per file for a given window, where a window is simply calculated
as the combination of two primary options:
``compaction_window_unit`` (default: DAYS)
A Java TimeUnit (MINUTES, HOURS, or DAYS).
``compaction_window_size`` (default: 1)
The number of units that make up a window.
Taken together, the operator can specify windows of virtually any size, and `TimeWindowCompactionStrategy` will work to
create a single sstable for writes within that window. For efficiency during writing, the newest window will be
compacted using `SizeTieredCompactionStrategy`.
Ideally, operators should select a ``compaction_window_unit`` and ``compaction_window_size`` pair that produces
approximately 20-30 windows - if writing with a 90 day TTL, for example, a 3 Day window would be a reasonable choice
(``'compaction_window_unit':'DAYS','compaction_window_size':3``).
TimeWindowCompactionStrategy Operational Concerns
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The primary motivation for TWCS is to separate data on disk by timestamp and to allow fully expired SSTables to drop
more efficiently. One potential way this optimal behavior can be subverted is if data is written to SSTables out of
order, with new data and old data in the same SSTable. Out of order data can appear in two ways:
- If the user mixes old data and new data in the traditional write path, the data will be comingled in the memtables
and flushed into the same SSTable, where it will remain comingled.
- If the user's read requests for old data cause read repairs that pull old data into the current memtable, that data
will be comingled and flushed into the same SSTable.
While TWCS tries to minimize the impact of comingled data, users should attempt to avoid this behavior. Specifically,
users should avoid queries that explicitly set the timestamp via CQL ``USING TIMESTAMP``. Additionally, users should run
frequent repairs (which streams data in such a way that it does not become comingled), and disable background read
repair by setting the table's ``read_repair_chance`` and ``dclocal_read_repair_chance`` to 0.
Changing TimeWindowCompactionStrategy Options
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Operators wishing to enable ``TimeWindowCompactionStrategy`` on existing data should consider running a major compaction
first, placing all existing data into a single (old) window. Subsequent newer writes will then create typical SSTables
as expected.
Operators wishing to change ``compaction_window_unit`` or ``compaction_window_size`` can do so, but may trigger
additional compactions as adjacent windows are joined together. If the window size is decrease d (for example, from 24
hours to 12 hours), then the existing SSTables will not be modified - TWCS can not split existing SSTables into multiple
windows.

View File

@ -0,0 +1,94 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Compression
-----------
Cassandra offers operators the ability to configure compression on a per-table basis. Compression reduces the size of
data on disk by compressing the SSTable in user-configurable compression ``chunk_length_in_kb``. Because Cassandra
SSTables are immutable, the CPU cost of compressing is only necessary when the SSTable is written - subsequent updates
to data will land in different SSTables, so Cassandra will not need to decompress, overwrite, and recompress data when
UPDATE commands are issued. On reads, Cassandra will locate the relevant compressed chunks on disk, decompress the full
chunk, and then proceed with the remainder of the read path (merging data from disks and memtables, read repair, and so
on).
Configuring Compression
^^^^^^^^^^^^^^^^^^^^^^^
Compression is configured on a per-table basis as an optional argument to ``CREATE TABLE`` or ``ALTER TABLE``. By
default, three options are relevant:
- ``class`` specifies the compression class - Cassandra provides three classes (``LZ4Compressor``,
``SnappyCompressor``, and ``DeflateCompressor`` ). The default is ``SnappyCompressor``.
- ``chunk_length_in_kb`` specifies the number of kilobytes of data per compression chunk. The default is 64KB.
- ``crc_check_chance`` determines how likely Cassandra is to verify the checksum on each compression chunk during
reads. The default is 1.0.
Users can set compression using the following syntax:
::
CREATE TABLE keyspace.table (id int PRIMARY KEY) WITH compression = {'class': 'LZ4Compressor'};
Or
::
ALTER TABLE keyspace.table WITH compression = {'class': 'SnappyCompressor', 'chunk_length_in_kb': 128, 'crc_check_chance': 0.5};
Once enabled, compression can be disabled with ``ALTER TABLE`` setting ``enabled`` to ``false``:
::
ALTER TABLE keyspace.table WITH compression = {'enabled':'false'};
Operators should be aware, however, that changing compression is not immediate. The data is compressed when the SSTable
is written, and as SSTables are immutable, the compression will not be modified until the table is compacted. Upon
issuing a change to the compression options via ``ALTER TABLE``, the existing SSTables will not be modified until they
are compacted - if an operator needs compression changes to take effect immediately, the operator can trigger an SSTable
rewrite using ``nodetool scrub`` or ``nodetool upgradesstables -a``, both of which will rebuild the SSTables on disk,
re-compressing the data in the process.
Benefits and Uses
^^^^^^^^^^^^^^^^^
Compression's primary benefit is that it reduces the amount of data written to disk. Not only does the reduced size save
in storage requirements, it often increases read and write throughput, as the CPU overhead of compressing data is faster
than the time it would take to read or write the larger volume of uncompressed data from disk.
Compression is most useful in tables comprised of many rows, where the rows are similar in nature. Tables containing
similar text columns (such as repeated JSON blobs) often compress very well.
Operational Impact
^^^^^^^^^^^^^^^^^^
- Compression metadata is stored off-heap and scales with data on disk. This often requires 1-3GB of off-heap RAM per
terabyte of data on disk, though the exact usage varies with ``chunk_length_in_kb`` and compression ratios.
- Streaming operations involve compressing and decompressing data on compressed tables - in some code paths (such as
non-vnode bootstrap), the CPU overhead of compression can be a limiting factor.
- The compression path checksums data to ensure correctness - while the traditional Cassandra read path does not have a
way to ensure correctness of data on disk, compressed tables allow the user to set ``crc_check_chance`` (a float from
0.0 to 1.0) to allow Cassandra to probabilistically validate chunks on read to verify bits on disk are not corrupt.
Advanced Use
^^^^^^^^^^^^
Advanced users can provide their own compression class by implementing the interface at
``org.apache.cassandra.io.compress.ICompressor``.

View File

@ -0,0 +1,87 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Hardware Choices
----------------
Like most databases, Cassandra throughput improves with more CPU cores, more RAM, and faster disks. While Cassandra can
be made to run on small servers for testing or development environments (including Raspberry Pis), a minimal production
server requires at least 2 cores, and at least 8GB of RAM. Typical production servers have 8 or more cores and at least
32GB of RAM.
CPU
^^^
Cassandra is highly concurrent, handling many simultaneous requests (both read and write) using multiple threads running
on as many CPU cores as possible. The Cassandra write path tends to be heavily optimized (writing to the commitlog and
then inserting the data into the memtable), so writes, in particular, tend to be CPU bound. Consequently, adding
additional CPU cores often increases throughput of both reads and writes.
Memory
^^^^^^
Cassandra runs within a Java VM, which will pre-allocate a fixed size heap (java's Xmx system parameter). In addition to
the heap, Cassandra will use significant amounts of RAM offheap for compression metadata, bloom filters, row, key, and
counter caches, and an in process page cache. Finally, Cassandra will take advantage of the operating system's page
cache, storing recently accessed portions files in RAM for rapid re-use.
For optimal performance, operators should benchmark and tune their clusters based on their individual workload. However,
basic guidelines suggest:
- ECC RAM should always be used, as Cassandra has few internal safeguards to protect against bit level corruption
- The Cassandra heap should be no less than 2GB, and no more than 50% of your system RAM
- Heaps smaller than 12GB should consider ParNew/ConcurrentMarkSweep garbage collection
- Heaps larger than 12GB should consider G1GC
Disks
^^^^^
Cassandra persists data to disk for two very different purposes. The first is to the commitlog when a new write is made
so that it can be replayed after a crash or system shutdown. The second is to the data directory when thresholds are
exceeded and memtables are flushed to disk as SSTables.
Commitlogs receive every write made to a Cassandra node and have the potential to block client operations, but they are
only ever read on node start-up. SSTable (data file) writes on the other hand occur asynchronously, but are read to
satisfy client look-ups. SSTables are also periodically merged and rewritten in a process called compaction. The data
held in the commitlog directory is data that has not been permanently saved to the SSTable data directories - it will be
periodically purged once it is flushed to the SSTable data files.
Cassandra performs very well on both spinning hard drives and solid state disks. In both cases, Cassandra's sorted
immutable SSTables allow for linear reads, few seeks, and few overwrites, maximizing throughput for HDDs and lifespan of
SSDs by avoiding write amplification. However, when using spinning disks, it's important that the commitlog
(``commitlog_directory``) be on one physical disk (not simply a partition, but a physical disk), and the data files
(``data_file_directories``) be set to a separate physical disk. By separating the commitlog from the data directory,
writes can benefit from sequential appends to the commitlog without having to seek around the platter as reads request
data from various SSTables on disk.
In most cases, Cassandra is designed to provide redundancy via multiple independent, inexpensive servers. For this
reason, using NFS or a SAN for data directories is an antipattern and should typically be avoided. Similarly, servers
with multiple disks are often better served by using RAID0 or JBOD than RAID1 or RAID5 - replication provided by
Cassandra obsoletes the need for replication at the disk layer, so it's typically recommended that operators take
advantage of the additional throughput of RAID0 rather than protecting against failures with RAID1 or RAID5.
Common Cloud Choices
^^^^^^^^^^^^^^^^^^^^
Many large users of Cassandra run in various clouds, including AWS, Azure, and GCE - Cassandra will happily run in any
of these environments. Users should choose similar hardware to what would be needed in physical space. In EC2, popular
options include:
- m1.xlarge instances, which provide 1.6TB of local ephemeral spinning storage and sufficient RAM to run moderate
workloads
- i2 instances, which provide both a high RAM:CPU ratio and local ephemeral SSDs
- m4.2xlarge / c4.4xlarge instances, which provide modern CPUs, enhanced networking and work well with EBS GP2 (SSD)
storage
Generally, disk and network performance increases with instance size and generation, so newer generations of instances
and larger instance types within each family often perform better than their smaller or older alternatives.

View File

@ -0,0 +1,22 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Hints
-----
.. todo:: todo

View File

@ -0,0 +1,38 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Operating Cassandra
===================
.. toctree::
:maxdepth: 2
snitch
topo_changes
repair
read_repair
hints
compaction
bloom_filters
compression
cdc
backups
metrics
security
hardware

View File

@ -0,0 +1,619 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Monitoring
----------
Metrics in Cassandra are managed using the `Dropwizard Metrics <http://metrics.dropwizard.io>`__ library. These metrics
can be queried via JMX or pushed to external monitoring systems using a number of `built in
<http://metrics.dropwizard.io/3.1.0/getting-started/#other-reporting>`__ and `third party
<http://metrics.dropwizard.io/3.1.0/manual/third-party/>`__ reporter plugins.
Metrics are collected for a single node. It's up to the operator to use an external monitoring system to aggregate them.
Metric Types
^^^^^^^^^^^^
All metrics reported by cassandra fit into one of the following types.
``Gauge``
An instantaneous measurement of a value.
``Counter``
A gauge for an ``AtomicLong`` instance. Typically this is consumed by monitoring the change since the last call to
see if there is a large increase compared to the norm.
``Histogram``
Measures the statistical distribution of values in a stream of data.
In addition to minimum, maximum, mean, etc., it also measures median, 75th, 90th, 95th, 98th, 99th, and 99.9th
percentiles.
``Timer``
Measures both the rate that a particular piece of code is called and the histogram of its duration.
``Latency``
Special type that tracks latency (in microseconds) with a ``Timer`` plus a ``Counter`` that tracks the total latency
accrued since starting. The former is useful if you track the change in total latency since the last check. Each
metric name of this type will have 'Latency' and 'TotalLatency' appended to it.
``Meter``
A meter metric which measures mean throughput and one-, five-, and fifteen-minute exponentially-weighted moving
average throughputs.
Table Metrics
^^^^^^^^^^^^^
Each table in Cassandra has metrics responsible for tracking its state and performance.
The metric names are all appended with the specific ``Keyspace`` and ``Table`` name.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.Table.{{MetricName}}.{{Keyspace}}.{{Table}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=Table keyspace={{Keyspace} scope={{Table}} name={{MetricName}}``
.. NOTE::
There is a special table called '``all``' without a keyspace. This represents the aggregation of metrics across
**all** tables and keyspaces on the node.
======================================= ============== ===========
Name Type Description
======================================= ============== ===========
MemtableOnHeapSize Gauge<Long> Total amount of data stored in the memtable that resides **on**-heap, including column related overhead and partitions overwritten.
MemtableOffHeapSize Gauge<Long> Total amount of data stored in the memtable that resides **off**-heap, including column related overhead and partitions overwritten.
MemtableLiveDataSize Gauge<Long> Total amount of live data stored in the memtable, excluding any data structure overhead.
AllMemtablesOnHeapSize Gauge<Long> Total amount of data stored in the memtables (2i and pending flush memtables included) that resides **on**-heap.
AllMemtablesOffHeapSize Gauge<Long> Total amount of data stored in the memtables (2i and pending flush memtables included) that resides **off**-heap.
AllMemtablesLiveDataSize Gauge<Long> Total amount of live data stored in the memtables (2i and pending flush memtables included) that resides off-heap, excluding any data structure overhead.
MemtableColumnsCount Gauge<Long> Total number of columns present in the memtable.
MemtableSwitchCount Counter Number of times flush has resulted in the memtable being switched out.
CompressionRatio Gauge<Double> Current compression ratio for all SSTables.
EstimatedPartitionSizeHistogram Gauge<long[]> Histogram of estimated partition size (in bytes).
EstimatedPartitionCount Gauge<Long> Approximate number of keys in table.
EstimatedColumnCountHistogram Gauge<long[]> Histogram of estimated number of columns.
SSTablesPerReadHistogram Histogram Histogram of the number of sstable data files accessed per read.
ReadLatency Latency Local read latency for this table.
RangeLatency Latency Local range scan latency for this table.
WriteLatency Latency Local write latency for this table.
CoordinatorReadLatency Timer Coordinator read latency for this table.
CoordinatorScanLatency Timer Coordinator range scan latency for this table.
PendingFlushes Counter Estimated number of flush tasks pending for this table.
BytesFlushed Counter Total number of bytes flushed since server [re]start.
CompactionBytesWritten Counter Total number of bytes written by compaction since server [re]start.
PendingCompactions Gauge<Integer> Estimate of number of pending compactions for this table.
LiveSSTableCount Gauge<Integer> Number of SSTables on disk for this table.
LiveDiskSpaceUsed Counter Disk space used by SSTables belonging to this table (in bytes).
TotalDiskSpaceUsed Counter Total disk space used by SSTables belonging to this table, including obsolete ones waiting to be GC'd.
MinPartitionSize Gauge<Long> Size of the smallest compacted partition (in bytes).
MaxPartitionSize Gauge<Long> Size of the largest compacted partition (in bytes).
MeanPartitionSize Gauge<Long> Size of the average compacted partition (in bytes).
BloomFilterFalsePositives Gauge<Long> Number of false positives on table's bloom filter.
BloomFilterFalseRatio Gauge<Double> False positive ratio of table's bloom filter.
BloomFilterDiskSpaceUsed Gauge<Long> Disk space used by bloom filter (in bytes).
BloomFilterOffHeapMemoryUsed Gauge<Long> Off-heap memory used by bloom filter.
IndexSummaryOffHeapMemoryUsed Gauge<Long> Off-heap memory used by index summary.
CompressionMetadataOffHeapMemoryUsed Gauge<Long> Off-heap memory used by compression meta data.
KeyCacheHitRate Gauge<Double> Key cache hit rate for this table.
TombstoneScannedHistogram Histogram Histogram of tombstones scanned in queries on this table.
LiveScannedHistogram Histogram Histogram of live cells scanned in queries on this table.
ColUpdateTimeDeltaHistogram Histogram Histogram of column update time delta on this table.
ViewLockAcquireTime Timer Time taken acquiring a partition lock for materialized view updates on this table.
ViewReadTime Timer Time taken during the local read of a materialized view update.
TrueSnapshotsSize Gauge<Long> Disk space used by snapshots of this table including all SSTable components.
RowCacheHitOutOfRange Counter Number of table row cache hits that do not satisfy the query filter, thus went to disk.
RowCacheHit Counter Number of table row cache hits.
RowCacheMiss Counter Number of table row cache misses.
CasPrepare Latency Latency of paxos prepare round.
CasPropose Latency Latency of paxos propose round.
CasCommit Latency Latency of paxos commit round.
PercentRepaired Gauge<Double> Percent of table data that is repaired on disk.
SpeculativeRetries Counter Number of times speculative retries were sent for this table.
WaitingOnFreeMemtableSpace Histogram Histogram of time spent waiting for free memtable space, either on- or off-heap.
DroppedMutations Counter Number of dropped mutations on this table.
======================================= ============== ===========
Keyspace Metrics
^^^^^^^^^^^^^^^^
Each keyspace in Cassandra has metrics responsible for tracking its state and performance.
These metrics are the same as the ``Table Metrics`` above, only they are aggregated at the Keyspace level.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.keyspace.{{MetricName}}.{{Keyspace}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=Keyspace scope={{Keyspace}} name={{MetricName}}``
ThreadPool Metrics
^^^^^^^^^^^^^^^^^^
Cassandra splits work of a particular type into its own thread pool. This provides back-pressure and asynchrony for
requests on a node. It's important to monitor the state of these thread pools since they can tell you how saturated a
node is.
The metric names are all appended with the specific ``ThreadPool`` name. The thread pools are also categorized under a
specific type.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.ThreadPools.{{MetricName}}.{{Path}}.{{ThreadPoolName}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=ThreadPools scope={{ThreadPoolName}} type={{Type}} name={{MetricName}}``
===================== ============== ===========
Name Type Description
===================== ============== ===========
ActiveTasks Gauge<Integer> Number of tasks being actively worked on by this pool.
PendingTasks Gauge<Integer> Number of queued tasks queued up on this pool.
CompletedTasks Counter Number of tasks completed.
TotalBlockedTasks Counter Number of tasks that were blocked due to queue saturation.
CurrentlyBlockedTask Counter Number of tasks that are currently blocked due to queue saturation but on retry will become unblocked.
MaxPoolSize Gauge<Integer> The maximum number of threads in this pool.
===================== ============== ===========
The following thread pools can be monitored.
============================ ============== ===========
Name Type Description
============================ ============== ===========
Native-Transport-Requests transport Handles client CQL requests
CounterMutationStage request Responsible for counter writes
ViewMutationStage request Responsible for materialized view writes
MutationStage request Responsible for all other writes
ReadRepairStage request ReadRepair happens on this thread pool
ReadStage request Local reads run on this thread pool
RequestResponseStage request Coordinator requests to the cluster run on this thread pool
AntiEntropyStage internal Builds merkle tree for repairs
CacheCleanupExecutor internal Cache maintenance performed on this thread pool
CompactionExecutor internal Compactions are run on these threads
GossipStage internal Handles gossip requests
HintsDispatcher internal Performs hinted handoff
InternalResponseStage internal Responsible for intra-cluster callbacks
MemtableFlushWriter internal Writes memtables to disk
MemtablePostFlush internal Cleans up commit log after memtable is written to disk
MemtableReclaimMemory internal Memtable recycling
MigrationStage internal Runs schema migrations
MiscStage internal Misceleneous tasks run here
PendingRangeCalculator internal Calculates token range
PerDiskMemtableFlushWriter_0 internal Responsible for writing a spec (there is one of these per disk 0-N)
Sampler internal Responsible for re-sampling the index summaries of SStables
SecondaryIndexManagement internal Performs updates to secondary indexes
ValidationExecutor internal Performs validation compaction or scrubbing
============================ ============== ===========
.. |nbsp| unicode:: 0xA0 .. nonbreaking space
Client Request Metrics
^^^^^^^^^^^^^^^^^^^^^^
Client requests have their own set of metrics that encapsulate the work happening at coordinator level.
Different types of client requests are broken down by ``RequestType``.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.ClientRequest.{{MetricName}}.{{RequestType}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=ClientRequest scope={{RequestType}} name={{MetricName}}``
:RequestType: CASRead
:Description: Metrics related to transactional read requests.
:Metrics:
===================== ============== =============================================================
Name Type Description
===================== ============== =============================================================
Timeouts Counter Number of timeouts encountered.
Failures Counter Number of transaction failures encountered.
|nbsp| Latency Transaction read latency.
Unavailables Counter Number of unavailable exceptions encountered.
UnfinishedCommit Counter Number of transactions that were committed on read.
ConditionNotMet Counter Number of transaction preconditions did not match current values.
ContentionHistogram Histogram How many contended reads were encountered
===================== ============== =============================================================
:RequestType: CASWrite
:Description: Metrics related to transactional write requests.
:Metrics:
===================== ============== =============================================================
Name Type Description
===================== ============== =============================================================
Timeouts Counter Number of timeouts encountered.
Failures Counter Number of transaction failures encountered.
|nbsp| Latency Transaction write latency.
UnfinishedCommit Counter Number of transactions that were committed on write.
ConditionNotMet Counter Number of transaction preconditions did not match current values.
ContentionHistogram Histogram How many contended writes were encountered
===================== ============== =============================================================
:RequestType: Read
:Description: Metrics related to standard read requests.
:Metrics:
===================== ============== =============================================================
Name Type Description
===================== ============== =============================================================
Timeouts Counter Number of timeouts encountered.
Failures Counter Number of read failures encountered.
|nbsp| Latency Read latency.
Unavailables Counter Number of unavailable exceptions encountered.
===================== ============== =============================================================
:RequestType: RangeSlice
:Description: Metrics related to token range read requests.
:Metrics:
===================== ============== =============================================================
Name Type Description
===================== ============== =============================================================
Timeouts Counter Number of timeouts encountered.
Failures Counter Number of range query failures encountered.
|nbsp| Latency Range query latency.
Unavailables Counter Number of unavailable exceptions encountered.
===================== ============== =============================================================
:RequestType: Write
:Description: Metrics related to regular write requests.
:Metrics:
===================== ============== =============================================================
Name Type Description
===================== ============== =============================================================
Timeouts Counter Number of timeouts encountered.
Failures Counter Number of write failures encountered.
|nbsp| Latency Write latency.
Unavailables Counter Number of unavailable exceptions encountered.
===================== ============== =============================================================
:RequestType: ViewWrite
:Description: Metrics related to materialized view write wrtes.
:Metrics:
===================== ============== =============================================================
Timeouts Counter Number of timeouts encountered.
Failures Counter Number of transaction failures encountered.
Unavailables Counter Number of unavailable exceptions encountered.
ViewReplicasAttempted Counter Total number of attempted view replica writes.
ViewReplicasSuccess Counter Total number of succeded view replica writes.
ViewPendingMutations Gauge<Long> ViewReplicasAttempted - ViewReplicasSuccess.
ViewWriteLatency Timer Time between when mutation is applied to base table and when CL.ONE is achieved on view.
===================== ============== =============================================================
Cache Metrics
^^^^^^^^^^^^^
Cassandra caches have metrics to track the effectivness of the caches. Though the ``Table Metrics`` might be more useful.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.Cache.{{MetricName}}.{{CacheName}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=Cache scope={{CacheName}} name={{MetricName}}``
========================== ============== ===========
Name Type Description
========================== ============== ===========
Capacity Gauge<Long> Cache capacity in bytes.
Entries Gauge<Integer> Total number of cache entries.
FifteenMinuteCacheHitRate Gauge<Double> 15m cache hit rate.
FiveMinuteCacheHitRate Gauge<Double> 5m cache hit rate.
OneMinuteCacheHitRate Gauge<Double> 1m cache hit rate.
HitRate Gauge<Double> All time cache hit rate.
Hits Meter Total number of cache hits.
Misses Meter Total number of cache misses.
MissLatency Timer Latency of misses.
Requests Gauge<Long> Total number of cache requests.
Size Gauge<Long> Total size of occupied cache, in bytes.
========================== ============== ===========
The following caches are covered:
============================ ===========
Name Description
============================ ===========
CounterCache Keeps hot counters in memory for performance.
ChunkCache In process uncompressed page cache.
KeyCache Cache for partition to sstable offsets.
RowCache Cache for rows kept in memory.
============================ ===========
.. NOTE::
Misses and MissLatency are only defined for the ChunkCache
CQL Metrics
^^^^^^^^^^^
Metrics specific to CQL prepared statement caching.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.CQL.{{MetricName}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=CQL name={{MetricName}}``
========================== ============== ===========
Name Type Description
========================== ============== ===========
PreparedStatementsCount Gauge<Integer> Number of cached prepared statements.
PreparedStatementsEvicted Counter Number of prepared statements evicted from the prepared statement cache
PreparedStatementsExecuted Counter Number of prepared statements executed.
RegularStatementsExecuted Counter Number of **non** prepared statements executed.
PreparedStatementsRatio Gauge<Double> Percentage of statements that are prepared vs unprepared.
========================== ============== ===========
DroppedMessage Metrics
^^^^^^^^^^^^^^^^^^^^^^
Metrics specific to tracking dropped messages for different types of requests.
Dropped writes are stored and retried by ``Hinted Handoff``
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.DroppedMessages.{{MetricName}}.{{Type}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=DroppedMetrics scope={{Type}} name={{MetricName}}``
========================== ============== ===========
Name Type Description
========================== ============== ===========
CrossNodeDroppedLatency Timer The dropped latency across nodes.
InternalDroppedLatency Timer The dropped latency within node.
Dropped Meter Number of dropped messages.
========================== ============== ===========
The different types of messages tracked are:
============================ ===========
Name Description
============================ ===========
BATCH_STORE Batchlog write
BATCH_REMOVE Batchlog cleanup (after succesfully applied)
COUNTER_MUTATION Counter writes
HINT Hint replay
MUTATION Regular writes
READ Regular reads
READ_REPAIR Read repair
PAGED_SLICE Paged read
RANGE_SLICE Token range read
REQUEST_RESPONSE RPC Callbacks
_TRACE Tracing writes
============================ ===========
Streaming Metrics
^^^^^^^^^^^^^^^^^
Metrics reported during ``Streaming`` operations, such as repair, bootstrap, rebuild.
These metrics are specific to a peer endpoint, with the source node being the node you are pulling the metrics from.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.Streaming.{{MetricName}}.{{PeerIP}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=Streaming scope={{PeerIP}} name={{MetricName}}``
========================== ============== ===========
Name Type Description
========================== ============== ===========
IncomingBytes Counter Number of bytes streamed to this node from the peer.
OutgoingBytes Counter Number of bytes streamed to the peer endpoint from this node.
========================== ============== ===========
Compaction Metrics
^^^^^^^^^^^^^^^^^^
Metrics specific to ``Compaction`` work.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.Compaction.{{MetricName}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=Compaction name={{MetricName}}``
========================== ======================================== ===============================================
Name Type Description
========================== ======================================== ===============================================
BytesCompacted Counter Total number of bytes compacted since server [re]start.
PendingTasks Gauge<Integer> Estimated number of compactions remaining to perform.
CompletedTasks Gauge<Long> Number of completed compactions since server [re]start.
TotalCompactionsCompleted Meter Throughput of completed compactions since server [re]start.
PendingTasksByTableName Gauge<Map<String, Map<String, Integer>>> Estimated number of compactions remaining to perform, grouped by keyspace and then table name. This info is also kept in ``Table Metrics``.
========================== ======================================== ===============================================
CommitLog Metrics
^^^^^^^^^^^^^^^^^
Metrics specific to the ``CommitLog``
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.CommitLog.{{MetricName}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=CommitLog name={{MetricName}}``
========================== ============== ===========
Name Type Description
========================== ============== ===========
CompletedTasks Gauge<Long> Total number of commit log messages written since [re]start.
PendingTasks Gauge<Long> Number of commit log messages written but yet to be fsync'd.
TotalCommitLogSize Gauge<Long> Current size, in bytes, used by all the commit log segments.
WaitingOnSegmentAllocation Timer Time spent waiting for a CommitLogSegment to be allocated - under normal conditions this should be zero.
WaitingOnCommit Timer The time spent waiting on CL fsync; for Periodic this is only occurs when the sync is lagging its sync interval.
========================== ============== ===========
Storage Metrics
^^^^^^^^^^^^^^^
Metrics specific to the storage engine.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.Storage.{{MetricName}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=Storage name={{MetricName}}``
========================== ============== ===========
Name Type Description
========================== ============== ===========
Exceptions Counter Number of internal exceptions caught. Under normal exceptions this should be zero.
Load Counter Size, in bytes, of the on disk data size this node manages.
TotalHints Counter Number of hint messages written to this node since [re]start. Includes one entry for each host to be hinted per hint.
TotalHintsInProgress Counter Number of hints attemping to be sent currently.
========================== ============== ===========
HintedHandoff Metrics
^^^^^^^^^^^^^^^^^^^^^
Metrics specific to Hinted Handoff. There are also some metrics related to hints tracked in ``Storage Metrics``
These metrics include the peer endpoint **in the metric name**
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.HintedHandOffManager.{{MetricName}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=HintedHandOffManager name={{MetricName}}``
=========================== ============== ===========
Name Type Description
=========================== ============== ===========
Hints_created-{{PeerIP}} Counter Number of hints on disk for this peer.
Hints_not_stored-{{PeerIP}} Counter Number of hints not stored for this peer, due to being down past the configured hint window.
=========================== ============== ===========
SSTable Index Metrics
^^^^^^^^^^^^^^^^^^^^^
Metrics specific to the SSTable index metadata.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.Index.{{MetricName}}.RowIndexEntry``
**JMX MBean**
``org.apache.cassandra.metrics:type=Index scope=RowIndexEntry name={{MetricName}}``
=========================== ============== ===========
Name Type Description
=========================== ============== ===========
IndexedEntrySize Histogram Histogram of the on-heap size, in bytes, of the index across all SSTables.
IndexInfoCount Histogram Histogram of the number of on-heap index entries managed across all SSTables.
IndexInfoGets Histogram Histogram of the number index seeks performed per SSTable.
=========================== ============== ===========
BufferPool Metrics
^^^^^^^^^^^^^^^^^^
Metrics specific to the internal recycled buffer pool Cassandra manages. This pool is meant to keep allocations and GC
lower by recycling on and off heap buffers.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.BufferPool.{{MetricName}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=BufferPool name={{MetricName}}``
=========================== ============== ===========
Name Type Description
=========================== ============== ===========
Size Gauge<Long> Size, in bytes, of the managed buffer pool
Misses Meter The rate of misses in the pool. The higher this is the more allocations incurred.
=========================== ============== ===========
Client Metrics
^^^^^^^^^^^^^^
Metrics specifc to client managment.
Reported name format:
**Metric Name**
``org.apache.cassandra.metrics.Client.{{MetricName}}``
**JMX MBean**
``org.apache.cassandra.metrics:type=Client name={{MetricName}}``
=========================== ============== ===========
Name Type Description
=========================== ============== ===========
connectedNativeClients Counter Number of clients connected to this nodes native protocol server
connectedThriftClients Counter Number of clients connected to this nodes thrift protocol server
=========================== ============== ===========
JMX
^^^
Any JMX based client can access metrics from cassandra.
If you wish to access JMX metrics over http it's possible to download `Mx4jTool <http://mx4j.sourceforge.net/>`__ and
place ``mx4j-tools.jar`` into the classpath. On startup you will see in the log::
HttpAdaptor version 3.0.2 started on port 8081
To choose a different port (8081 is the default) or a different listen address (0.0.0.0 is not the default) edit
``conf/cassandra-env.sh`` and uncomment::
#MX4J_ADDRESS="-Dmx4jaddress=0.0.0.0"
#MX4J_PORT="-Dmx4jport=8081"
Metric Reporters
^^^^^^^^^^^^^^^^
As mentioned at the top of this section on monitoring the Cassandra metrics can be exported to a number of monitoring
system a number of `built in <http://metrics.dropwizard.io/3.1.0/getting-started/#other-reporting>`__ and `third party
<http://metrics.dropwizard.io/3.1.0/manual/third-party/>`__ reporter plugins.
The configuration of these plugins is managed by the `metrics reporter config project
<https://github.com/addthis/metrics-reporter-config>`__. There is a sample configuration file located at
``conf/metrics-reporter-config-sample.yaml``.
Once configured, you simply start cassandra with the flag
``-Dcassandra.metricsReporterConfigFile=metrics-reporter-config.yaml``. The specified .yaml file plus any 3rd party
reporter jars must all be in Cassandra's classpath.

View File

@ -0,0 +1,22 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Read repair
-----------
.. todo:: todo

View File

@ -0,0 +1,22 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Repair
------
.. todo:: todo

View File

@ -0,0 +1,410 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Security
--------
There are three main components to the security features provided by Cassandra:
- TLS/SSL encryption for client and inter-node communication
- Client authentication
- Authorization
TLS/SSL Encryption
^^^^^^^^^^^^^^^^^^
Cassandra provides secure communication between a client machine and a database cluster and between nodes within a
cluster. Enabling encryption ensures that data in flight is not compromised and is transferred securely. The options for
client-to-node and node-to-node encryption are managed separately and may be configured independently.
In both cases, the JVM defaults for supported protocols and cipher suites are used when encryption is enabled. These can
be overidden using the settings in ``cassandra.yaml``, but this is not recommended unless there are policies in place
which dictate certain settings or a need to disable vulnerable ciphers or protocols in cases where the JVM cannot be
updated.
FIPS compliant settings can be configured at the JVM level and should not involve changing encryption settings in
cassandra.yaml. See `the java document on FIPS <https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/FIPS.html>`__
for more details.
For information on generating the keystore and truststore files used in SSL communications, see the
`java documentation on creating keystores <http://download.oracle.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#CreateKeystore>`__
Inter-node Encryption
~~~~~~~~~~~~~~~~~~~~~
The settings for managing inter-node encryption are found in ``cassandra.yaml`` in the ``server_encryption_options``
section. To enable inter-node encryption, change the ``internode_encryption`` setting from its default value of ``none``
to one value from: ``rack``, ``dc`` or ``all``.
Client to Node Encryption
~~~~~~~~~~~~~~~~~~~~~~~~~
The settings for managing client to node encryption are found in ``cassandra.yaml`` in the ``client_encryption_options``
section. There are two primary toggles here for enabling encryption, ``enabled`` and ``optional``.
- If neither is set to ``true``, client connections are entirely unencrypted.
- If ``enabled`` is set to ``true`` and ``optional`` is set to ``false``, all client connections must be secured.
- If both options are set to ``true``, both encrypted and unencrypted connections are supported using the same port.
Client connections using encryption with this configuration will be automatically detected and handled by the server.
As an alternative to the ``optional`` setting, separate ports can also be configured for secure and unsecure connections
where operational requirements demand it. To do so, set ``optional`` to false and use the ``native_transport_port_ssl``
setting in ``cassandra.yaml`` to specify the port to be used for secure client communication.
.. _operation-roles:
Roles
^^^^^
Cassandra uses database roles, which may represent either a single user or a group of users, in both authentication and
permissions management. Role management is an extension point in Cassandra and may be configured using the
``role_manager`` setting in ``cassandra.yaml``. The default setting uses ``CassandraRoleManager``, an implementation
which stores role information in the tables of the ``system_auth`` keyspace.
See also the :ref:`CQL documentation on roles <cql-roles>`.
Authentication
^^^^^^^^^^^^^^
Authentication is pluggable in Cassandra and is configured using the ``authenticator`` setting in ``cassandra.yaml``.
Cassandra ships with two options included in the default distribution.
By default, Cassandra is configured with ``AllowAllAuthenticator`` which performs no authentication checks and therefore
requires no credentials. It is used to disable authentication completely. Note that authentication is a necessary
condition of Cassandra's permissions subsystem, so if authentication is disabled, effectively so are permissions.
The default distribution also includes ``PasswordAuthenticator``, which stores encrypted credentials in a system table.
This can be used to enable simple username/password authentication.
.. _password-authentication:
Enabling Password Authentication
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Before enabling client authentication on the cluster, client applications should be pre-configured with their intended
credentials. When a connection is initiated, the server will only ask for credentials once authentication is
enabled, so setting up the client side config in advance is safe. In contrast, as soon as a server has authentication
enabled, any connection attempt without proper credentials will be rejected which may cause availability problems for
client applications. Once clients are setup and ready for authentication to be enabled, follow this procedure to enable
it on the cluster.
Pick a single node in the cluster on which to perform the initial configuration. Ideally, no clients should connect
to this node during the setup process, so you may want to remove it from client config, block it at the network level
or possibly add a new temporary node to the cluster for this purpose. On that node, perform the following steps:
1. Open a ``cqlsh`` session and change the replication factor of the ``system_auth`` keyspace. By default, this keyspace
uses ``SimpleReplicationStrategy`` and a ``replication_factor`` of 1. It is recommended to change this for any
non-trivial deployment to ensure that should nodes become unavailable, login is still possible. Best practice is to
configure a replication factor of 3 to 5 per-DC.
::
ALTER KEYSPACE system_auth WITH replication = {'class': 'NetworkTopologyStrategy', 'DC1': 3, 'DC2': 3};
2. Edit ``cassandra.yaml`` to change the ``authenticator`` option like so:
::
authenticator: PasswordAuthenticator
3. Restart the node.
4. Open a new ``cqlsh`` session using the credentials of the default superuser:
::
cqlsh -u cassandra -p cassandra
5. During login, the credentials for the default superuser are read with a consistency level of ``QUORUM``, whereas
those for all other users (including superusers) are read at ``LOCAL_ONE``. In the interests of performance and
availability, as well as security, operators should create another superuser and disable the default one. This step
is optional, but highly recommended. While logged in as the default superuser, create another superuser role which
can be used to bootstrap further configuration.
::
# create a new superuser
CREATE ROLE dba WITH SUPERUSER = true AND LOGIN = true AND PASSWORD = 'super';
6. Start a new cqlsh session, this time logging in as the new_superuser and disable the default superuser.
::
ALTER ROLE cassandra WITH SUPERUSER = false AND LOGIN = false;
7. Finally, set up the roles and credentials for your application users with :ref:`CREATE ROLE <create-role-statement>`
statements.
At the end of these steps, the one node is configured to use password authentication. To roll that out across the
cluster, repeat steps 2 and 3 on each node in the cluster. Once all nodes have been restarted, authentication will be
fully enabled throughout the cluster.
Note that using ``PasswordAuthenticator`` also requires the use of :ref:`CassandraRoleManager <operation-roles>`.
See also: :ref:`setting-credentials-for-internal-authentication`, :ref:`CREATE ROLE <create-role-statement>`,
:ref:`ALTER ROLE <alter-role-statement>`, :ref:`ALTER KEYSPACE <alter-keyspace-statement>` and :ref:`GRANT PERMISSION
<grant-permission-statement>`,
Authorization
^^^^^^^^^^^^^
Authorization is pluggable in Cassandra and is configured using the ``authorizer`` setting in ``cassandra.yaml``.
Cassandra ships with two options included in the default distribution.
By default, Cassandra is configured with ``AllowAllAuthorizer`` which performs no checking and so effectively grants all
permissions to all roles. This must be used if ``AllowAllAuthenticator`` is the configured authenticator.
The default distribution also includes ``CassandraAuthorizer``, which does implement full permissions management
functionality and stores its data in Cassandra system tables.
Enabling Internal Authorization
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Permissions are modelled as a whitelist, with the default assumption that a given role has no access to any database
resources. The implication of this is that once authorization is enabled on a node, all requests will be rejected until
the required permissions have been granted. For this reason, it is strongly recommended to perform the initial setup on
a node which is not processing client requests.
The following assumes that authentication has already been enabled via the process outlined in
:ref:`password-authentication`. Perform these steps to enable internal authorization across the cluster:
1. On the selected node, edit ``cassandra.yaml`` to change the ``authorizer`` option like so:
::
authorizer: CassandraAuthorizer
2. Restart the node.
3. Open a new ``cqlsh`` session using the credentials of a role with superuser credentials:
::
cqlsh -u dba -p super
4. Configure the appropriate access privileges for your clients using `GRANT PERMISSION <cql.html#grant-permission>`_
statements. On the other nodes, until configuration is updated and the node restarted, this will have no effect so
disruption to clients is avoided.
::
GRANT SELECT ON ks.t1 TO db_user;
5. Once all the necessary permissions have been granted, repeat steps 1 and 2 for each node in turn. As each node
restarts and clients reconnect, the enforcement of the granted permissions will begin.
See also: :ref:`GRANT PERMISSION <grant-permission-statement>`, `GRANT ALL <grant-all>` and :ref:`REVOKE PERMISSION
<revoke-permission-statement>`
Caching
^^^^^^^
Enabling authentication and authorization places additional load on the cluster by frequently reading from the
``system_auth`` tables. Furthermore, these reads are in the critical paths of many client operations, and so has the
potential to severely impact quality of service. To mitigate this, auth data such as credentials, permissions and role
details are cached for a configurable period. The caching can be configured (and even disabled) from ``cassandra.yaml``
or using a JMX client. The JMX interface also supports invalidation of the various caches, but any changes made via JMX
are not persistent and will be re-read from ``cassandra.yaml`` when the node is restarted.
Each cache has 3 options which can be set:
Validity Period
Controls the expiration of cache entries. After this period, entries are invalidated and removed from the cache.
Refresh Rate
Controls the rate at which background reads are performed to pick up any changes to the underlying data. While these
async refreshes are performed, caches will continue to serve (possibly) stale data. Typically, this will be set to a
shorter time than the validity period.
Max Entries
Controls the upper bound on cache size.
The naming for these options in ``cassandra.yaml`` follows the convention:
* ``<type>_validity_in_ms``
* ``<type>_update_interval_in_ms``
* ``<type>_cache_max_entries``
Where ``<type>`` is one of ``credentials``, ``permissions``, or ``roles``.
As mentioned, these are also exposed via JMX in the mbeans under the ``org.apache.cassandra.auth`` domain.
JMX access
^^^^^^^^^^
Access control for JMX clients is configured separately to that for CQL. For both authentication and authorization, two
providers are available; the first based on standard JMX security and the second which integrates more closely with
Cassandra's own auth subsystem.
The default settings for Cassandra make JMX accessible only from localhost. To enable remote JMX connections, edit
``cassandra-env.sh`` (or ``cassandra-env.ps1`` on Windows) to change the ``LOCAL_JMX`` setting to ``yes``. Under the
standard configuration, when remote JMX connections are enabled, :ref:`standard JMX authentication <standard-jmx-auth>`
is also switched on.
Note that by default, local-only connections are not subject to authentication, but this can be enabled.
If enabling remote connections, it is recommended to also use :ref:`SSL <jmx-with-ssl>` connections.
Finally, after enabling auth and/or SSL, ensure that tools which use JMX, such as :ref:`nodetool <nodetool>`, are
correctly configured and working as expected.
.. _standard-jmx-auth:
Standard JMX Auth
~~~~~~~~~~~~~~~~~
Users permitted to connect to the JMX server are specified in a simple text file. The location of this file is set in
``cassandra-env.sh`` by the line:
::
JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.password.file=/etc/cassandra/jmxremote.password"
Edit the password file to add username/password pairs:
::
jmx_user jmx_password
Secure the credentials file so that only the user running the Cassandra process can read it :
::
$ chown cassandra:cassandra /etc/cassandra/jmxremote.password
$ chmod 400 /etc/cassandra/jmxremote.password
Optionally, enable access control to limit the scope of what defined users can do via JMX. Note that this is a fairly
blunt instrument in this context as most operational tools in Cassandra require full read/write access. To configure a
simple access file, uncomment this line in ``cassandra-env.sh``:
::
#JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.access.file=/etc/cassandra/jmxremote.access"
Then edit the access file to grant your JMX user readwrite permission:
::
jmx_user readwrite
Cassandra must be restarted to pick up the new settings.
See also : `Using File-Based Password Authentication In JMX
<http://docs.oracle.com/javase/7/docs/technotes/guides/management/agent.html#gdenv>`__
Cassandra Integrated Auth
~~~~~~~~~~~~~~~~~~~~~~~~~
An alternative to the out-of-the-box JMX auth is to useeCassandra's own authentication and/or authorization providers
for JMX clients. This is potentially more flexible and secure but it come with one major caveat. Namely that it is not
available until `after` a node has joined the ring, because the auth subsystem is not fully configured until that point
However, it is often critical for monitoring purposes to have JMX access particularly during bootstrap. So it is
recommended, where possible, to use local only JMX auth during bootstrap and then, if remote connectivity is required,
to switch to integrated auth once the node has joined the ring and initial setup is complete.
With this option, the same database roles used for CQL authentication can be used to control access to JMX, so updates
can be managed centrally using just ``cqlsh``. Furthermore, fine grained control over exactly which operations are
permitted on particular MBeans can be acheived via :ref:`GRANT PERMISSION <grant-permission-statement>`.
To enable integrated authentication, edit ``cassandra-env.sh`` to uncomment these lines:
::
#JVM_OPTS="$JVM_OPTS -Dcassandra.jmx.remote.login.config=CassandraLogin"
#JVM_OPTS="$JVM_OPTS -Djava.security.auth.login.config=$CASSANDRA_HOME/conf/cassandra-jaas.config"
And disable the JMX standard auth by commenting this line:
::
JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.password.file=/etc/cassandra/jmxremote.password"
To enable integrated authorization, uncomment this line:
::
#JVM_OPTS="$JVM_OPTS -Dcassandra.jmx.authorizer=org.apache.cassandra.auth.jmx.AuthorizationProxy"
Check standard access control is off by ensuring this line is commented out:
::
#JVM_OPTS="$JVM_OPTS -Dcom.sun.management.jmxremote.access.file=/etc/cassandra/jmxremote.access"
With integrated authentication and authorization enabled, operators can define specific roles and grant them access to
the particular JMX resources that they need. For example, a role with the necessary permissions to use tools such as
jconsole or jmc in read-only mode would be defined as:
::
CREATE ROLE jmx WITH LOGIN = false;
GRANT SELECT ON ALL MBEANS TO jmx;
GRANT DESCRIBE ON ALL MBEANS TO jmx;
GRANT EXECUTE ON MBEAN 'java.lang:type=Threading' TO jmx;
GRANT EXECUTE ON MBEAN 'com.sun.management:type=HotSpotDiagnostic' TO jmx;
# Grant the jmx role to one with login permissions so that it can access the JMX tooling
CREATE ROLE ks_user WITH PASSWORD = 'password' AND LOGIN = true AND SUPERUSER = false;
GRANT jmx TO ks_user;
Fine grained access control to individual MBeans is also supported:
::
GRANT EXECUTE ON MBEAN 'org.apache.cassandra.db:type=Tables,keyspace=test_keyspace,table=t1' TO ks_user;
GRANT EXECUTE ON MBEAN 'org.apache.cassandra.db:type=Tables,keyspace=test_keyspace,table=*' TO ks_owner;
This permits the ``ks_user`` role to invoke methods on the MBean representing a single table in ``test_keyspace``, while
granting the same permission for all table level MBeans in that keyspace to the ``ks_owner`` role.
Adding/removing roles and granting/revoking of permissions is handled dynamically once the initial setup is complete, so
no further restarts are required if permissions are altered.
See also: :ref:`Permissions <cql-permissions>`.
.. _jmx-with-ssl:
JMX With SSL
~~~~~~~~~~~~
JMX SSL configuration is controlled by a number of system properties, some of which are optional. To turn on SSL, edit
the relevant lines in ``cassandra-env.sh`` (or ``cassandra-env.ps1`` on Windows) to uncomment and set the values of these
properties as required:
``com.sun.management.jmxremote.ssl``
set to true to enable SSL
``com.sun.management.jmxremote.ssl.need.client.auth``
set to true to enable validation of client certificates
``com.sun.management.jmxremote.registry.ssl``
enables SSL sockets for the RMI registry from which clients obtain the JMX connector stub
``com.sun.management.jmxremote.ssl.enabled.protocols``
by default, the protocols supported by the JVM will be used, override with a comma-separated list. Note that this is
not usually necessary and using the defaults is the preferred option.
``com.sun.management.jmxremote.ssl.enabled.cipher.suites``
by default, the cipher suites supported by the JVM will be used, override with a comma-separated list. Note that
this is not usually necessary and using the defaults is the preferred option.
``javax.net.ssl.keyStore``
set the path on the local filesystem of the keystore containing server private keys and public certificates
``javax.net.ssl.keyStorePassword``
set the password of the keystore file
``javax.net.ssl.trustStore``
if validation of client certificates is required, use this property to specify the path of the truststore containing
the public certificates of trusted clients
``javax.net.ssl.trustStorePassword``
set the password of the truststore file
See also: `Oracle Java7 Docs <http://docs.oracle.com/javase/7/docs/technotes/guides/management/agent.html#gdemv>`__,
`Monitor Java with JMX <https://www.lullabot.com/articles/monitor-java-with-jmx>`__

View File

@ -0,0 +1,78 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Snitch
------
In cassandra, the snitch has two functions:
- it teaches Cassandra enough about your network topology to route requests efficiently.
- it allows Cassandra to spread replicas around your cluster to avoid correlated failures. It does this by grouping
machines into "datacenters" and "racks." Cassandra will do its best not to have more than one replica on the same
"rack" (which may not actually be a physical location).
Dynamic snitching
^^^^^^^^^^^^^^^^^
The dynamic snitch monitor read latencies to avoid reading from hosts that have slowed down. The dynamic snitch is
configured with the following properties on ``cassandra.yaml``:
- ``dynamic_snitch``: whether the dynamic snitch should be enabled or disabled.
- ``dynamic_snitch_update_interval_in_ms``: controls how often to perform the more expensive part of host score
calculation.
- ``dynamic_snitch_reset_interval_in_ms``: if set greater than zero and read_repair_chance is < 1.0, this will allow
'pinning' of replicas to hosts in order to increase cache capacity.
- ``dynamic_snitch_badness_threshold:``: The badness threshold will control how much worse the pinned host has to be
before the dynamic snitch will prefer other replicas over it. This is expressed as a double which represents a
percentage. Thus, a value of 0.2 means Cassandra would continue to prefer the static snitch values until the pinned
host was 20% worse than the fastest.
Snitch classes
^^^^^^^^^^^^^^
The ``endpoint_snitch`` parameter in ``cassandra.yaml`` should be set to the class the class that implements
``IEndPointSnitch`` which will be wrapped by the dynamic snitch and decide if two endpoints are in the same data center
or on the same rack. Out of the box, Cassandra provides the snitch implementations:
GossipingPropertyFileSnitch
This should be your go-to snitch for production use. The rack and datacenter for the local node are defined in
cassandra-rackdc.properties and propagated to other nodes via gossip. If ``cassandra-topology.properties`` exists,
it is used as a fallback, allowing migration from the PropertyFileSnitch.
SimpleSnitch
Treats Strategy order as proximity. This can improve cache locality when disabling read repair. Only appropriate for
single-datacenter deployments.
PropertyFileSnitch
Proximity is determined by rack and data center, which are explicitly configured in
``cassandra-topology.properties``.
Ec2Snitch
Appropriate for EC2 deployments in a single Region. Loads Region and Availability Zone information from the EC2 API.
The Region is treated as the datacenter, and the Availability Zone as the rack. Only private IPs are used, so this
will not work across multiple regions.
Ec2MultiRegionSnitch
Uses public IPs as broadcast_address to allow cross-region connectivity (thus, you should set seed addresses to the
public IP as well). You will need to open the ``storage_port`` or ``ssl_storage_port`` on the public IP firewall
(For intra-Region traffic, Cassandra will switch to the private IP after establishing a connection).
RackInferringSnitch
Proximity is determined by rack and data center, which are assumed to correspond to the 3rd and 2nd octet of each
node's IP address, respectively. Unless this happens to match your deployment conventions, this is best used as an
example of writing a custom Snitch class and is provided in that spirit.

View File

@ -0,0 +1,122 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. highlight:: none
Adding, replacing, moving and removing nodes
--------------------------------------------
Bootstrap
^^^^^^^^^
Adding new nodes is called "bootstrapping". The ``num_tokens`` parameter will define the amount of virtual nodes
(tokens) the joining node will be assigned during bootstrap. The tokens define the sections of the ring (token ranges)
the node will become responsible for.
Token allocation
~~~~~~~~~~~~~~~~
With the default token allocation algorithm the new node will pick ``num_tokens`` random tokens to become responsible
for. Since tokens are distributed randomly, load distribution improves with a higher amount of virtual nodes, but it
also increases token management overhead. The default of 256 virtual nodes should provide a reasonable load balance with
acceptable overhead.
On 3.0+ a new token allocation algorithm was introduced to allocate tokens based on the load of existing virtual nodes
for a given keyspace, and thus yield an improved load distribution with a lower number of tokens. To use this approach,
the new node must be started with the JVM option ``-Dcassandra.allocate_tokens_for_keyspace=<keyspace>``, where
``<keyspace>`` is the keyspace from which the algorithm can find the load information to optimize token assignment for.
Manual token assignment
"""""""""""""""""""""""
You may specify a comma-separated list of tokens manually with the ``initial_token`` ``cassandra.yaml`` parameter, and
if that is specified Cassandra will skip the token allocation process. This may be useful when doing token assignment
with an external tool or when restoring a node with its previous tokens.
Range streaming
~~~~~~~~~~~~~~~~
After the tokens are allocated, the joining node will pick current replicas of the token ranges it will become
responsible for to stream data from. By default it will stream from the primary replica of each token range in order to
guarantee data in the new node will be consistent with the current state.
In the case of any unavailable replica, the consistent bootstrap process will fail. To override this behavior and
potentially miss data from an unavailable replica, set the JVM flag ``-Dcassandra.consistent.rangemovement=false``.
Resuming failed/hanged bootstrap
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
On 2.2+, if the bootstrap process fails, it's possible to resume bootstrap from the previous saved state by calling
``nodetool bootstrap resume``. If for some reason the bootstrap hangs or stalls, it may also be resumed by simply
restarting the node. In order to cleanup bootstrap state and start fresh, you may set the JVM startup flag
``-Dcassandra.reset_bootstrap_progress=true``.
On lower versions, when the bootstrap proces fails it is recommended to wipe the node (remove all the data), and restart
the bootstrap process again.
Manual bootstrapping
~~~~~~~~~~~~~~~~~~~~
It's possible to skip the bootstrapping process entirely and join the ring straight away by setting the hidden parameter
``auto_bootstrap: false``. This may be useful when restoring a node from a backup or creating a new data-center.
Removing nodes
^^^^^^^^^^^^^^
You can take a node out of the cluster with ``nodetool decommission`` to a live node, or ``nodetool removenode`` (to any
other machine) to remove a dead one. This will assign the ranges the old node was responsible for to other nodes, and
replicate the appropriate data there. If decommission is used, the data will stream from the decommissioned node. If
removenode is used, the data will stream from the remaining replicas.
No data is removed automatically from the node being decommissioned, so if you want to put the node back into service at
a different token on the ring, it should be removed manually.
Moving nodes
^^^^^^^^^^^^
When ``num_tokens: 1`` it's possible to move the node position in the ring with ``nodetool move``. Moving is both a
convenience over and more efficient than decommission + bootstrap. After moving a node, ``nodetool cleanup`` should be
run to remove any unnecessary data.
Replacing a dead node
^^^^^^^^^^^^^^^^^^^^^
In order to replace a dead node, start cassandra with the JVM startup flag
``-Dcassandra.replace_address_first_boot=<dead_node_ip>``. Once this property is enabled the node starts in a hibernate
state, during which all the other nodes will see this node to be down.
The replacing node will now start to bootstrap the data from the rest of the nodes in the cluster. The main difference
between normal bootstrapping of a new node is that this new node will not accept any writes during this phase.
Once the bootstrapping is complete the node will be marked "UP", we rely on the hinted handoff's for making this node
consistent (since we don't accept writes since the start of the bootstrap).
.. Note:: If the replacement process takes longer than ``max_hint_window_in_ms`` you **MUST** run repair to make the
replaced node consistent again, since it missed ongoing writes during bootstrapping.
Monitoring progress
^^^^^^^^^^^^^^^^^^^
Bootstrap, replace, move and remove progress can be monitored using ``nodetool netstats`` which will show the progress
of the streaming operations.
Cleanup data after range movements
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
As a safety measure, Cassandra does not automatically remove data from nodes that "lose" part of their token range due
to a range movement operation (bootstrap, move, replace). Run ``nodetool cleanup`` on the nodes that lost ranges to the
joining node when you are satisfied the new node is up and working. If you do not do this the old data will still be
counted against the load on that node.

455
doc/source/tools/cqlsh.rst Normal file
View File

@ -0,0 +1,455 @@
.. highlight:: none
.. _cqlsh:
cqlsh: the CQL shell
--------------------
cqlsh is a command line shell for interacting with Cassandra through CQL (the Cassandra Query Language). It is shipped
with every Cassandra package, and can be found in the bin/ directory alongside the cassandra executable. cqlsh utilizes
the Python native protocol driver, and connects to the single node specified on the command line.
Compatibility
^^^^^^^^^^^^^
cqlsh is compatible with Python 2.7.
In general, a given version of cqlsh is only guaranteed to work with the version of Cassandra that it was released with.
In some cases, cqlsh make work with older or newer versions of Cassandra, but this is not officially supported.
Optional Dependencies
^^^^^^^^^^^^^^^^^^^^^
cqlsh ships with all essential dependencies. However, there are some optional dependencies that can be installed to
improve the capabilities of cqlsh.
pytz
~~~~
By default, cqlsh displays all timestamps with a UTC timezone. To support display of timestamps with another timezone,
the `pytz <http://pytz.sourceforge.net/>`__ library must be installed. See the ``timezone`` option in cqlshrc_ for
specifying a timezone to use.
cython
~~~~~~
The performance of cqlsh's ``COPY`` operations can be improved by installing `cython <http://cython.org/>`__. This will
compile the python modules that are central to the performance of ``COPY``.
cqlshrc
^^^^^^^
The ``cqlshrc`` file holds configuration options for cqlsh. By default this is in the user's home directory at
``~/.cassandra/cqlsh``, but a custom location can be specified with the ``--cqlshrc`` option.
Example config values and documentation can be found in the ``conf/cqlshrc.sample`` file of a tarball installation. You
can also view the latest version of `cqlshrc online <https://github.com/apache/cassandra/blob/trunk/conf/cqlshrc.sample>`__.
Command Line Options
^^^^^^^^^^^^^^^^^^^^
Usage:
``cqlsh [options] [host [port]]``
Options:
``-C`` ``--color``
Force color output
``--no-color``
Disable color output
``--browser``
Specify the browser to use for displaying cqlsh help. This can be one of the `supported browser names
<https://docs.python.org/2/library/webbrowser.html>`__ (e.g. ``firefox``) or a browser path followed by ``%s`` (e.g.
``/usr/bin/google-chrome-stable %s``).
``--ssl``
Use SSL when connecting to Cassandra
``-u`` ``--user``
Username to authenticate against Cassandra with
``-p`` ``--password``
Password to authenticate against Cassandra with, should
be used in conjunction with ``--user``
``-k`` ``--keyspace``
Keyspace to authenticate to, should be used in conjunction
with ``--user``
``-f`` ``--file``
Execute commands from the given file, then exit
``--debug``
Print additional debugging information
``--encoding``
Specify a non-default encoding for output (defaults to UTF-8)
``--cqlshrc``
Specify a non-default location for the ``cqlshrc`` file
``-e`` ``--execute``
Execute the given statement, then exit
``--connect-timeout``
Specify the connection timeout in seconds (defaults to 2s)
``--request-timeout``
Specify the request timeout in seconds (defaults to 10s)
``-t`` ``--tty``
Force tty mode (command prompt)
Special Commands
^^^^^^^^^^^^^^^^
In addition to supporting regular CQL statements, cqlsh also supports a number of special commands that are not part of
CQL. These are detailed below.
``CONSISTENCY``
~~~~~~~~~~~~~~~
`Usage`: ``CONSISTENCY <consistency level>``
Sets the consistency level for operations to follow. Valid arguments include:
- ``ANY``
- ``ONE``
- ``TWO``
- ``THREE``
- ``QUORUM``
- ``ALL``
- ``LOCAL_QUORUM``
- ``LOCAL_ONE``
- ``SERIAL``
- ``LOCAL_SERIAL``
``SERIAL CONSISTENCY``
~~~~~~~~~~~~~~~~~~~~~~
`Usage`: ``SERIAL CONSISTENCY <consistency level>``
Sets the serial consistency level for operations to follow. Valid arguments include:
- ``SERIAL``
- ``LOCAL_SERIAL``
The serial consistency level is only used by conditional updates (``INSERT``, ``UPDATE`` and ``DELETE`` with an ``IF``
condition). For those, the serial consistency level defines the consistency level of the serial phase (or “paxos” phase)
while the normal consistency level defines the consistency for the “learn” phase, i.e. what type of reads will be
guaranteed to see the update right away. For example, if a conditional write has a consistency level of ``QUORUM`` (and
is successful), then a ``QUORUM`` read is guaranteed to see that write. But if the regular consistency level of that
write is ``ANY``, then only a read with a consistency level of ``SERIAL`` is guaranteed to see it (even a read with
consistency ``ALL`` is not guaranteed to be enough).
``SHOW VERSION``
~~~~~~~~~~~~~~~~
Prints the cqlsh, Cassandra, CQL, and native protocol versions in use. Example::
cqlsh> SHOW VERSION
[cqlsh 5.0.1 | Cassandra 3.8 | CQL spec 3.4.2 | Native protocol v4]
``SHOW HOST``
~~~~~~~~~~~~~
Prints the IP address and port of the Cassandra node that cqlsh is connected to in addition to the cluster name.
Example::
cqlsh> SHOW HOST
Connected to Prod_Cluster at 192.0.0.1:9042.
``SHOW SESSION``
~~~~~~~~~~~~~~~~
Pretty prints a specific tracing session.
`Usage`: ``SHOW SESSION <session id>``
Example usage::
cqlsh> SHOW SESSION 95ac6470-327e-11e6-beca-dfb660d92ad8
Tracing session: 95ac6470-327e-11e6-beca-dfb660d92ad8
activity | timestamp | source | source_elapsed | client
-----------------------------------------------------------+----------------------------+-----------+----------------+-----------
Execute CQL3 query | 2016-06-14 17:23:13.979000 | 127.0.0.1 | 0 | 127.0.0.1
Parsing SELECT * FROM system.local; [SharedPool-Worker-1] | 2016-06-14 17:23:13.982000 | 127.0.0.1 | 3843 | 127.0.0.1
...
``SOURCE``
~~~~~~~~~~
Reads the contents of a file and executes each line as a CQL statement or special cqlsh command.
`Usage`: ``SOURCE <string filename>``
Example usage::
cqlsh> SOURCE '/home/thobbs/commands.cql'
``CAPTURE``
~~~~~~~~~~~
Begins capturing command output and appending it to a specified file. Output will not be shown at the console while it
is captured.
`Usage`::
CAPTURE '<file>';
CAPTURE OFF;
CAPTURE;
That is, the path to the file to be appended to must be given inside a string literal. The path is interpreted relative
to the current working directory. The tilde shorthand notation (``'~/mydir'``) is supported for referring to ``$HOME``.
Only query result output is captured. Errors and output from cqlsh-only commands will still be shown in the cqlsh
session.
To stop capturing output and show it in the cqlsh session again, use ``CAPTURE OFF``.
To inspect the current capture configuration, use ``CAPTURE`` with no arguments.
``HELP``
~~~~~~~~
Gives information about cqlsh commands. To see available topics, enter ``HELP`` without any arguments. To see help on a
topic, use ``HELP <topic>``. Also see the ``--browser`` argument for controlling what browser is used to display help.
``TRACING``
~~~~~~~~~~~
Enables or disables tracing for queries. When tracing is enabled, once a query completes, a trace of the events during
the query will be printed.
`Usage`::
TRACING ON
TRACING OFF
``PAGING``
~~~~~~~~~~
Enables paging, disables paging, or sets the page size for read queries. When paging is enabled, only one page of data
will be fetched at a time and a prompt will appear to fetch the next page. Generally, it's a good idea to leave paging
enabled in an interactive session to avoid fetching and printing large amounts of data at once.
`Usage`::
PAGING ON
PAGING OFF
PAGING <page size in rows>
``EXPAND``
~~~~~~~~~~
Enables or disables vertical printing of rows. Enabling ``EXPAND`` is useful when many columns are fetched, or the
contents of a single column are large.
`Usage`::
EXPAND ON
EXPAND OFF
``LOGIN``
~~~~~~~~~
Authenticate as a specified Cassandra user for the current session.
`Usage`::
LOGIN <username> [<password>]
``EXIT``
~~~~~~~~~
Ends the current session and terminates the cqlsh process.
`Usage`::
EXIT
QUIT
``CLEAR``
~~~~~~~~~
Clears the console.
`Usage`::
CLEAR
CLS
``DESCRIBE``
~~~~~~~~~~~~
Prints a description (typically a series of DDL statements) of a schema element or the cluster. This is useful for
dumping all or portions of the schema.
`Usage`::
DESCRIBE CLUSTER
DESCRIBE SCHEMA
DESCRIBE KEYSPACES
DESCRIBE KEYSPACE <keyspace name>
DESCRIBE TABLES
DESCRIBE TABLE <table name>
DESCRIBE INDEX <index name>
DESCRIBE MATERIALIZED VIEW <view name>
DESCRIBE TYPES
DESCRIBE TYPE <type name>
DESCRIBE FUNCTIONS
DESCRIBE FUNCTION <function name>
DESCRIBE AGGREGATES
DESCRIBE AGGREGATE <aggregate function name>
In any of the commands, ``DESC`` may be used in place of ``DESCRIBE``.
The ``DESCRIBE CLUSTER`` command prints the cluster name and partitioner::
cqlsh> DESCRIBE CLUSTER
Cluster: Test Cluster
Partitioner: Murmur3Partitioner
The ``DESCRIBE SCHEMA`` command prints the DDL statements needed to recreate the entire schema. This is especially
useful for dumping the schema in order to clone a cluster or restore from a backup.
``COPY TO``
~~~~~~~~~~~
Copies data from a table to a CSV file.
`Usage`::
COPY <table name> [(<column>, ...)] TO <file name> WITH <copy option> [AND <copy option> ...]
If no columns are specified, all columns from the table will be copied to the CSV file. A subset of columns to copy may
be specified by adding a comma-separated list of column names surrounded by parenthesis after the table name.
The ``<file name>`` should be a string literal (with single quotes) representing a path to the destination file. This
can also the special value ``STDOUT`` (without single quotes) to print the CSV to stdout.
See :ref:`shared-copy-options` for options that apply to both ``COPY TO`` and ``COPY FROM``.
Options for ``COPY TO``
```````````````````````
``MAXREQUESTS``
The maximum number token ranges to fetch simultaneously. Defaults to 6.
``PAGESIZE``
The number of rows to fetch in a single page. Defaults to 1000.
``PAGETIMEOUT``
By default the page timeout is 10 seconds per 1000 entries
in the page size or 10 seconds if pagesize is smaller.
``BEGINTOKEN``, ``ENDTOKEN``
Token range to export. Defaults to exporting the full ring.
``MAXOUTPUTSIZE``
The maximum size of the output file measured in number of lines;
beyond this maximum the output file will be split into segments.
-1 means unlimited, and is the default.
``ENCODING``
The encoding used for characters. Defaults to ``utf8``.
``COPY FROM``
~~~~~~~~~~~~~
Copies data from a CSV file to table.
`Usage`::
COPY <table name> [(<column>, ...)] FROM <file name> WITH <copy option> [AND <copy option> ...]
If no columns are specified, all columns from the CSV file will be copied to the table. A subset
of columns to copy may be specified by adding a comma-separated list of column names surrounded
by parenthesis after the table name.
The ``<file name>`` should be a string literal (with single quotes) representing a path to the
source file. This can also the special value ``STDIN`` (without single quotes) to read the
CSV data from stdin.
See :ref:`shared-copy-options` for options that apply to both ``COPY TO`` and ``COPY FROM``.
Options for ``COPY TO``
```````````````````````
``INGESTRATE``
The maximum number of rows to process per second. Defaults to 100000.
``MAXROWS``
The maximum number of rows to import. -1 means unlimited, and is the default.
``SKIPROWS``
A number of initial rows to skip. Defaults to 0.
``SKIPCOLS``
A comma-separated list of column names to ignore. By default, no columns are skipped.
``MAXPARSEERRORS``
The maximum global number of parsing errors to ignore. -1 means unlimited, and is the default.
``MAXINSERTERRORS``
The maximum global number of insert errors to ignore. -1 means unlimited. The default is 1000.
``ERRFILE`` =
A file to store all rows that could not be imported, by default this is ``import_<ks>_<table>.err`` where ``<ks>`` is
your keyspace and ``<table>`` is your table name.
``MAXBATCHSIZE``
The max number of rows inserted in a single batch. Defaults to 20.
``MINBATCHSIZE``
The min number of rows inserted in a single batch. Defaults to 2.
``CHUNKSIZE``
The number of rows that are passed to child worker processes from the main process at a time. Defaults to 1000.
.. _shared-copy-options:
Shared COPY Options
```````````````````
Options that are common to both ``COPY TO`` and ``COPY FROM``.
``NULLVAL``
The string placeholder for null values. Defaults to ``null``.
``HEADER``
For ``COPY TO``, controls whether the first line in the CSV output file will contain the column names. For COPY FROM,
specifies whether the first line in the CSV input file contains column names. Defaults to ``false``.
``DECIMALSEP``
The character that is used as the decimal point separator. Defaults to ``.``.
``THOUSANDSSEP``
The character that is used to separate thousands. Defaults to the empty string.
``BOOLSTYlE``
The string literal format for boolean values. Defaults to ``True,False``.
``NUMPROCESSES``
The number of child worker processes to create for ``COPY`` tasks. Defaults to a max of 4 for ``COPY FROM`` and 16
for ``COPY TO``. However, at most (num_cores - 1) processes will be created.
``MAXATTEMPTS``
The maximum number of failed attempts to fetch a range of data (when using ``COPY TO``) or insert a chunk of data
(when using ``COPY FROM``) before giving up. Defaults to 5.
``REPORTFREQUENCY``
How often status updates are refreshed, in seconds. Defaults to 0.25.
``RATEFILE``
An optional file to output rate statistics to. By default, statistics are not output to a file.

View File

@ -0,0 +1,26 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Cassandra Tools
===============
This section describes the command line tools provided with Apache Cassandra.
.. toctree::
:maxdepth: 1
cqlsh
nodetool

View File

@ -0,0 +1,22 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
.. _nodetool:
Nodetool
--------
.. todo:: Try to autogenerate this from Nodetools help.

View File

@ -0,0 +1,20 @@
.. Licensed to the Apache Software Foundation (ASF) under one
.. or more contributor license agreements. See the NOTICE file
.. distributed with this work for additional information
.. regarding copyright ownership. The ASF licenses this file
.. to you under the Apache License, Version 2.0 (the
.. "License"); you may not use this file except in compliance
.. with the License. You may obtain a copy of the License at
..
.. http://www.apache.org/licenses/LICENSE-2.0
..
.. Unless required by applicable law or agreed to in writing, software
.. distributed under the License is distributed on an "AS IS" BASIS,
.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
.. See the License for the specific language governing permissions and
.. limitations under the License.
Troubleshooting
===============
.. TODO: todo

View File

@ -246,13 +246,16 @@ public class CreateViewStatement extends SchemaAlteringStatement
throw new InvalidRequestException(String.format("Unable to include static column '%s' which would be included by Materialized View SELECT * statement", identifier));
}
if (includeDef && !targetClusteringColumns.contains(identifier) && !targetPartitionKeys.contains(identifier))
boolean defInTargetPrimaryKey = targetClusteringColumns.contains(identifier)
|| targetPartitionKeys.contains(identifier);
if (includeDef && !defInTargetPrimaryKey)
{
includedColumns.add(identifier);
}
if (!def.isPrimaryKeyColumn()) continue;
if (!targetClusteringColumns.contains(identifier) && !targetPartitionKeys.contains(identifier))
if (!defInTargetPrimaryKey)
{
if (missingClusteringColumns)
columnNames.append(',');