http://docs.datastax.com/en/cql/3.1/pdf/cql31.pdf

CQL是Cassandra Query Language的缩写,目前作为Cassandra默认并且主要的交互接口。CQL和SQL比较类似,主要的区别是Cassandra不支持join或子查询,除了支持通过Hive进行批处理分析。要说这个Cassandra以前的接口主要是Thrift API,这个没有用过,不做评价。

Cassandra在CQL语言层面支持多种数据类型[12]

CQL类型 对应Java类型 描述
ascii String ascii字符串
bigint long 64位整数
blob ByteBuffer/byte[] 二进制数组
boolean boolean 布尔
counter long 计数器,支持原子性的增减,不支持直接赋值
decimal BigDecimal 高精度小数
double double 64位浮点数
float float 32位浮点数
inet InetAddress ipv4ipv6协议的ip地址
int int 32位整数
list List 有序的列表
map Map 键值对
set Set 集合
text String utf-8编码的字符串
timestamp Date 日期
uuid UUID UUID类型
timeuuid UUID 时间相关的UUID
varchar string text的别名
varint BigInteger 高精度整型

cqlsh语法

cqlsh [options] [host [port]]
python cqlsh [options] [host [port]] 

Options

-C, --color
Always use color output.
--debug
Show additional debugging information.
--cqlshrc path
Use an alternative cqlshrc file location, path. (Cassandra 2.1.1)
-e cql_statement, --execute cql_statement
Accept and execute a CQL command in Cassandra 2.1 and later. Useful for saving CQL output to a file.
-f file_name, --file=file_name
Execute commands from file_name, then exit.
-h, --help
Show the online help about these options and exit.
-k keyspace_name
Use the given keyspace. Equivalent to issuing a USE keyspace command immediately after starting cqlsh.
--no-color
Never use color output.
-p password
Authenticate using password. Default = cassandra.
-t transport_factory_name, --transport=transport_factory_name
Use the provided Thrift transport factory function.
-u user_name
Authenticate as user. Default = cassandra.
--version
Show the cqlsh version.

启动CQL命令是cqlsh,我下面的例子是window上的,cassandra版本是2.1.14

示例:

#debug
D:\soft\cassandra\apache-cassandra-2.1.-bin\bin>cqlsh.bat --debug
Using CQL driver: <module 'cassandra' from 'D:\soft\cassandra\apache-cassandra-2.1.14-bin\bin\..\lib\cassandra-driver-in
ternal-only-2.7..zip\cassandra-driver-2.7.\cassandra\__init__.py'>
Using connect timeout: seconds
Connected to Test Cluster at 127.0.0.1:.
[cqlsh 5.0. | Cassandra 2.1. | CQL spec 3.2. | Native protocol v3]
Use HELP for help.
WARNING: pyreadline dependency missing. Install to enable tab completion. #version
D:\soft\cassandra\apache-cassandra-2.1.-bin\bin>cqlsh.bat --version
cqlsh 5.0.
#Saving CQL output in a file导出
D:\soft\cassandra\apache-cassandra-2.1.-bin\bin>cqlsh.bat -e "select * from duansf.users">myoutput.txt

导出的文件如下:

D:\soft\cassandra\apache-cassandra-2.1.14-bin\bin>cqlsh.bat

结果:cqlsh Can't detect Python version!

安装python,我安装的是64位的2.7版本,并配置下环境变量path中增加python的安装根路径。安装好后再执行cqlsh.bat

D:\soft\cassandra\apache-cassandra-2.1.14-bin\bin>cqlsh.bat

D:\soft\cassandra\apache-cassandra-2.1.-bin\bin>cqlsh.bat
Connected to Test Cluster at 127.0.0.1:.
[cqlsh 5.0. | Cassandra 2.1. | CQL spec 3.2. | Native protocol v3]
Use HELP for help.
WARNING: pyreadline dependency missing. Install to enable tab completion.

一、创建keyspace

作为对照,你可以把keyspace理解成一个SQL数据库实例,当然它们毕竟是不同的:Cassandra的keyspace是用来定义数据是如何在节点间复制的。通常情况下,应该为一个应用程序建立一个keyspace。

CREATE KEYSPACE IF NOT EXISTS pimin_net
WITH REPLICATION = {'class': 'SimpleStrategy','replication_factor':};

上面语句的意思是判断是否存在keyspace,如果不存在则建立keyspace;使用的副本策略是简单策略,复制因子是1。暂时先不管里面深层次的东西,我们先按照简单原则实现。

二、创建表

虽然说Cassandra是面向列的分布式数据库,但是它也有表的概念。创建之前先use pimin_net。

USE pimin_net;

CREATE TABLE users (
id int,
user_name varchar,
PRIMARY KEY (id) );
 

这样就建立了一张用户表,为了简单起见,就只有两个字段,看起来和oracle、mysql这些是不是很像?

三、对表的CRUD

已经有了一张用户表,我们就向里面插入一些数据,对它进行查询、更新和删除操作。

INSERT INTO users (id,user_name) VALUES (,'china');
INSERT INTO users (id,user_name) VALUES (,'taiwan');
SELECT * FROM users;
结果:
cqlsh:pimin_net> SELECT * FROM users;

 id | user_name
----+----------- ( rows)
cqlsh:pimin_net> INSERT INTO users (id,user_name) VALUES (,'china');
cqlsh:pimin_net> INSERT INTO users (id,user_name) VALUES (,'taiwan');
cqlsh:pimin_net> SELECT * FROM users; id | user_name
----+-----------
| china
| taiwan ( rows)
cqlsh:pimin_net>
UPDATE users SET user_name = 'china2014' WHERE id = ;
SELECT * FROM users;
DELETE FROM users WHERE id = ;
SELECT * FROM users;

结果:

cqlsh:pimin_net> UPDATE users SET user_name = 'china2014' WHERE id = ;
cqlsh:pimin_net> SELECT * FROM users; id | user_name
----+-----------
| china2014
| taiwan ( rows)
cqlsh:pimin_net> DELETE FROM users WHERE id = ;
cqlsh:pimin_net> SELECT * FROM users; id | user_name
----+-----------
| taiwan ( rows)
cqlsh:pimin_net>

重要:不同于传统的RDBMS,Cassandra不能使用DELETE FROM users;这样的表达式,必须有WHERE条件!

重要:不同于传统的RDBMS,Cassandra不能使用DELETE FROM users;这样的表达式,必须有WHERE条件!

示例2:

cqlsh:usermanager> use duansf

1.创建keyspace

cqlsh:usermanager> create keyspace duansf WITH REPLICATION = {'class': 'SimpleStrategy','replication_factor':};

创建一个名为duansf的keyspace,副本策略SimpleStrategy,复制因子为1.

2.创建Column family

cqlsh>use duansf;
cqlsh:duansf> create columnfamily users(
key varchar primary key,
password varchar,
gender varchar,
session_token varchar,
state varchar,
birth_year bigint);

创建一个名为users的column family

...  KEY varchar PRIMARY KEY,该columnfamily下有一个Key

和5列

...  password varchar,

...  gende rvarchar,

...  session_token varchar,

...  state varchar,

...  birth_year bigint);

3.插入和检索Columns

cqlsh:duansf> insert into users(key,password) values('jsmith','chadsfl') using ttl 86400;

向passwod这一列插入数据

cqlsh:duansf> select * from users where key='jsmith';

 key    | birth_year | gender | password | session_token | state
--------+------------+--------+----------+---------------+-------
jsmith | null | null | chadsfl | null | null ( rows)
cqlsh:duansf>

向session_token这一列插入数据

cqlsh:duansf> insert into users(key,session_token) values('jsmith','test') using ttl ;
cqlsh:duansf> select * from users where key='jsmith'; key | birth_year | gender | password | session_token | state
--------+------------+--------+----------+---------------+-------
jsmith | null | null | chadsfl | test | null

3.向Column family中增加Column

cqlsh:duansf> alter table user add coupon_code varchar;

注意:其他已经存在的列不会进行更新。

4. 更改Column的元数据

cqlsh:duansf> alter table users alter coupon_code type int;
ConfigurationException: <ErrorMessage code= [Query invalid because of configuration issue] message="Cannot change co
upon_code from type text to type int: types are incompatible.">

注意:已经存在的数据不会转成此类型,新插入的数据才是该类型的。

5.使用TTL属性设置列的到期时间

cqlsh:duansf> update users using ttl  set password='asldkjsfsdf' where key = 'jsmith';

更新密码列的到期时间为5天。

6.删除列元数据

cqlsh:duansf> alter table users drop coupon_code;

7.索引Column

cqlsh:duansf> create index state_key on users(state);
cqlsh:duansf> create index birth_year_key on users(birth_year);

8.删除列或者行

cqlsh:duansf> delete session_token from users where key='jsmith';  //删除session_token列
cqlsh:duansf> select * from users; key | birth_year | gender | password | session_token | state
--------+------------+--------+-------------+---------------+-------
jsmith | null | null | asldkjsfsdf | null | null ( rows)
cqlsh:duansf> delete from users where key='jsmith'; //删除key=jsmith的行
cqlsh:duansf> select * from users; key | birth_year | gender | password | session_token | state
-----+------------+--------+----------+---------------+------- ( rows)
cqlsh:duansf>

9. 删除columnfamily和keyspace

cqlsh:duansf> drop columnfamily users;
cqlsh:duansf> insert into users(key,password) values('jsmith','chadsfl') using ttl ;
InvalidRequest: code= [Invalid query] message="unconfigured columnfamily users"
cqlsh:duansf>

删除keyspace

cqlsh:duansf> drop keyspace duansf;
cqlsh:duansf> use duansf;
InvalidRequest: code= [Invalid query] message="Keyspace 'duansf' does not exist"
cqlsh:duansf>

10.查看结构信息

cqlsh:usermanager> desc users;

CREATE TABLE usermanager.users (
key blob PRIMARY KEY,
age text,
name text
) WITH bloom_filter_fp_chance = 0.01
AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
AND comment = ''
AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'}
AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live =
AND gc_grace_seconds =
AND max_index_interval =
AND memtable_flush_period_in_ms =
AND min_index_interval =
AND read_repair_chance = 0.0
AND speculative_retry = '99.0PERCENTILE';

CQL操作的更多相关文章

  1. Cassandra 之 入门

    1.到官网下载压缩包. http://cassandra.apache.org/download/ 我下载的是最新的 apache-cassandra-2.1.2-bin.tar.gz 另外:语言支持 ...

  2. cassandra 在window上的demo

    Cassandra   window使用 1.        下载:http://cassandra.apache.org/download/. 2.        解压后,bin目录下,cassan ...

  3. Cassandra 数据库, python cqlsh命令

    ★  cql操作数据库(cqlsh.bat: python cqlsh命令操作<优缺点:https://blog.csdn.net/vbirdbest/article/details/77662 ...

  4. Atitit.nosql api 标准化 以及nosql数据库的实现模型分类差异

    Atitit.nosql api 标准化 以及nosql数据库的实现模型分类差异 1. 常用的nosql数据库MongoDB  Cassandra1 1.1. 查询> db.blogposts. ...

  5. cassandra CQL 常用操作

    1. CQL客户端链接 bin/cqlsh ip username password 2. (1)建立keyspace语句,keyspace类似于 mysql 中的数据库,一个数据库中可以有很多表: ...

  6. CQL和SQL的CRUD操作比较

    数据进行CRUD操作时,CQL语句和SQL语句的异同之处. 1.建表 2.CRUD语句比较 3.总结 1.建表 在此之前先分别创建两张表,插入数据,用来测试然后进行比较 在SQL数据库里面创建表 在C ...

  7. cassandra CQL 3.0 怎样实现 dynamic column;

    1. cassandra有一个好的特点是列之间可以按照column key进行排序:这样当rowkey确定以后,对于同一个“行”的范围(range query)查找是很方便的:官方说法,每一个“行”( ...

  8. PLSQL_闪回操作4_Flashback Drop

    2014-06-25 Created By BaoXinjian

  9. Cassandra 数据模型 (基于CQL,解决胖列数量限制及灵活性问题)(1.1及以上版本)

    文中主要交代Cassandra的编程模型及数据结构. 由于Cassandra版本数次更新,网上中文的资料已经有点过时,比较有代表性的比如ebuy那篇文章都已经过时了,于是自己找资料,结合官方博客写一篇 ...

随机推荐

  1. springMvc源码学习之:spirngMvc的参数注入的问题

    转载:https://my.oschina.net/lichhao/blog/172562 概述 在SpringMVC中,可以使用@RequestBody和@ResponseBody两个注解,分别完成 ...

  2. liunx之:rpm包安装

    使用rpm命令查询软件包: 1.查询系统中安装的所有RPM包 $ rpm -qa 查询当前linux系统中已经安装的软件包. 例:$ rpm -qa | grep -i x11 | head -3 察 ...

  3. 磁盘与目录的容量[转自vbird]

    磁盘与目录的容量 现在我们知道磁盘的整体数据是在 superblock 区块中,但是每个各别文件的容量则在 inode 当中记载的. 那在文字接口底下该如何叫出这几个数据呢?底下就让我们来谈一谈这两个 ...

  4. dump java

    http://www.gamlor.info/wordpress/2011/09/visualvm/ https://visualvm.java.net/zh_CN/gettingstarted.ht ...

  5. linux free命令中buffer与cache的区别

    linux free命令中buffer与cache的区别 2012-05-15      个评论       收藏    我要投稿 linux free命令中buffer与cache的区别   ~$ ...

  6. WCF入门教程二[WCF应用的通信过程]

    一.概述 WCF能够建立一个跨平台的安全.可信赖.事务性的解决方案,是一个WebService,.Net Remoting,Enterprise Service,WSE,MSMQ的并集,有一副很经典的 ...

  7. Druid是什么和用StatViewServlet用于展示Druid的统计信息

    Druid是一个JDBC组件,它包括三部分: DruidDriver 代理Driver,能够提供基于Filter-Chain模式的插件体系.  DruidDataSource 高效可管理的数据库连接池 ...

  8. libc-glibc

    glibc 和 libc 都是 Linux 下的 C 函数库. libc 是 Linux 下的 ANSI C 函数库:glibc 是 Linux 下的 GUN C 函数库. ANSI C 和 GNU ...

  9. 【转】用 PHP 内置函数 file_put_contents 写入文件

    PHP 内置函数 file_put_contents 用于写入文件. file_put_contents 函数最简单的写法,可以只用两个参数,一个是文件路径,一个是要写入的内容,语法如下: file_ ...

  10. REG_SZ和REG_EXPAND_SZ的区别

    REG_SZ类型的键值中存在的可扩展占位符%xxx%不会被系统解释: REG_EXPAND_SZ类型的键值中存在的%xxx%的部分会被系统解释.