Cassandra安装和cql测试

一:下载cassandra

http://archive.apache.org/dist/cassandra/

我用的是2.1.8,3.9需要java8

wget http://archive.apache.org/dist/cassandra/2.1.8/apache-cassandra-2.1.8-bin.tar.gz

二:解压

tar -zxvf  apache-cassandra-2.1.8-bin.tar.gz

三:新建目录

mkdir /usr/local/cassandra/data

mkdir /usr/local/cassandra/commitlog

mkdir /usr/local/cassandra/saved_caches

四:修改配置文件

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"

五:运行

cd bin

./cassandra 

六:启动cql

cd bin

./cqlsh 

Cassandra CQL 使用

创建KEYSPACE

keyspace有两种策略,一个是 SimpleStrategy,另一个是 NetworkTopologyStrategy

  • SimpleStrategy
    该模式下需要指定复制策略,冗余几份数据,如:

  • create KEYSPACE demodb WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 2};
  • NetworkTopologyStrategy
    该模式是基于数据中心-机架的,创建基于此模式的KEYSPACE必须在配置文件中配置成该模式,否则将会报异常:
    Unable to complete request: one or more nodes were unavailable
    创建代码如下:

  • CREATE KEYSPACE demodb WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'dc1' :3 };

表操作

  • 创建表

  • create table blog(id timeuuid , title text, content text, tags list<text>, category text, primary key(id));
  • 插入

  • INSERT INTO  blog (id, title , tags ) VALUES ( now(), 'cassandra table test', ['cassandra','table']);
  • using ttl 如果和if not exists 或者 if exists 一起用, if 要放到 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; --针对这个字段起作用,而不是整行数据
  • batch batch内的操作都成功时候才会真正执行这些操作

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

http://ju.outofmemory.cn/entry/210437

猜你喜欢

转载自longzhun.iteye.com/blog/2343174