One: Download cassandra
http://archive.apache.org/dist/cassandra/
I am using 2.1.8, 3.9 requires java8
wget http://archive.apache.org/dist/cassandra/2.1.8/apache-cassandra-2.1.8-bin.tar.gz
Two: decompress
tar -zxvf apache-cassandra-2.1.8-bin.tar.gz
Three: create a new directory
mkdir /usr/local/cassandra/data
mkdir /usr/local/cassandra/commitlog
mkdir /usr/local/cassandra/saved_caches
Four: Modify the configuration file
vi conf / cassandra.yaml
data_file_directories:
- /usr/local/cassandra/data
commitlog_directory: /usr/local/cassandra/commitlog
saved_caches_directory: /usr/local/cassandra/saved_caches
vi conf/cassandra-env.sh
MAX_HEAP_SIZE="2G"
144 HEAP_NEWSIZE="800M"
Five: run
cd bin
./cassandra
Six: start cql
cd bin
./cqlsh
Cassandra CQL usage
CREATE KEYSPACE
There are two strategies for keyspace, one is SimpleStrategy
and the other isNetworkTopologyStrategy
-
SimpleStrategy
In this mode, a replication strategy needs to be specified, and several copies of data are redundant, such as: -
create KEYSPACE demodb WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2};
-
NetworkTopologyStrategy
This mode is based on data center-rack. To create a KEYSPACE based on this mode, you must configure this mode in the configuration file, otherwise an exception will be reported:
Unable to complete request: one or more nodes were unavailable The
creation code is as follows: -
CREATE KEYSPACE demodb WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'dc1' :3 };
table operations
-
create table
-
create table blog(id timeuuid , title text, content text, tags list<text>, category text, primary key(id));
-
insert
-
INSERT INTO blog (id, title , tags ) VALUES ( now(), 'cassandra table test', ['cassandra','table']);
-
using ttl If used together with if not exists or if exists, if should be placed before ttl
-
INSERT INTO blog (id, title , tags ) VALUES ( now(), 'cassandra table test', ['cassandra','table'])
-
USING TTL 222; --单位为秒这个时间是针对整行起作用 UPDATE BLOG USING TTL=33 SET TITLE='SS' WHERE ID=1; --针对这个字段起作用,而不是整行数据
-
Only when the operations in the batch batch are successful will these operations be executed
-
BEGIN BATCH INSERT INTO blog (id, title , tags ) VALUES ( now(), 'cassandra table test', ['cassandra','table']) USING TTL 222; --单位为秒这个时间是针对整行起作用 UPDATE BLOG USING TTL=33 SET TITLE='SS' WHERE ID=1; --针对这个字段起作用,而不是整行数据 APPLY BATCH;