日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

CQL操作

發布時間:2024/8/26 综合教程 23 生活家
生活随笔 收集整理的這篇文章主要介紹了 CQL操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 ipv4或ipv6協議的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, --colorAlways use color output.--debugShow additional debugging information.--cqlshrc pathUse an alternative cqlshrc file location, path. (Cassandra 2.1.1)-e cql_statement, --execute cql_statementAccept and execute a CQL command in Cassandra 2.1 and later. Useful forsaving CQL outputto a file.-f file_name, --file=file_nameExecute commands from file_name, then exit.-h, --helpShow the online help about these options and exit.-k keyspace_nameUse the given keyspace. Equivalent to issuing a USEkeyspacecommand immediately after starting cqlsh.--no-colorNever use color output.-p passwordAuthenticate using password. Default = cassandra.-t transport_factory_name, --transport=transport_factory_nameUse the provided Thrift transport factory function.-u user_nameAuthenticate as user. Default = cassandra.--versionShow the cqlsh version.

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

示例:

#debug
D:softcassandraapache-cassandra-2.1.14-binin>cqlsh.bat --debug
Using CQL driver: <module 'cassandra' from 'D:softcassandraapache-cassandra-2.1.14-binin..libcassandra-driver-in
ternal-only-2.7.2.zipcassandra-driver-2.7.2cassandra\__init__.py'>
Using connect timeout: 5 seconds
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 2.1.14 | CQL spec 3.2.1 | Native protocol v3]
Use HELP for help.
WARNING: pyreadline dependency missing.  Install to enable tab completion.

#version
D:softcassandraapache-cassandra-2.1.14-binin>cqlsh.bat --version
cqlsh 5.0.1
#Saving CQL output in a file導出
D:softcassandraapache-cassandra-2.1.14-binin>cqlsh.bat -e "select * from duansf.users">myoutput.txt

導出的文件如下:

D:softcassandraapache-cassandra-2.1.14-binin>cqlsh.bat

結果:cqlsh Can't detect Python version!

安裝python,我安裝的是64位的2.7版本,并配置下環境變量path中增加python的安裝根路徑。安裝好后再執行cqlsh.bat

D:softcassandraapache-cassandra-2.1.14-binin>cqlsh.bat

D:softcassandraapache-cassandra-2.1.14-binin>cqlsh.bat
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 2.1.14 | CQL spec 3.2.1 | 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':1};

上面語句的意思是判斷是否存在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 (1,'china');
INSERT INTO users (id,user_name) VALUES (2,'taiwan');
SELECT * FROM users;

結果:

cqlsh:pimin_net> SELECT * FROM users;

 id | user_name
----+-----------

(0 rows)
cqlsh:pimin_net> INSERT INTO users (id,user_name) VALUES (1,'china');
cqlsh:pimin_net> INSERT INTO users (id,user_name) VALUES (2,'taiwan');
cqlsh:pimin_net> SELECT * FROM users;

 id | user_name
----+-----------
  1 |     china
  2 |    taiwan

(2 rows)
cqlsh:pimin_net>

UPDATE users SET user_name = 'china2014' WHERE id = 1;
SELECT * FROM users;
DELETE FROM users WHERE id = 1;
SELECT * FROM users;

結果:

cqlsh:pimin_net> UPDATE users SET user_name = 'china2014' WHERE id = 1;
cqlsh:pimin_net> SELECT * FROM users;

 id | user_name
----+-----------
  1 | china2014
  2 |    taiwan

(2 rows)
cqlsh:pimin_net> DELETE FROM users WHERE id = 1;
cqlsh:pimin_net> SELECT * FROM users;

 id | user_name
----+-----------
  2 |    taiwan

(1 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':1};

創建一個名為duansf的keyspace,副本策略SimpleStrategy,復制因子為1.

2.創建Columnfamily

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的columnfamily

...KEYvarcharPRIMARYKEY,該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

(1 rows)
cqlsh:duansf>

向session_token這一列插入數據

cqlsh:duansf> insert into users(key,session_token) values('jsmith','test') using ttl 86400;
cqlsh:duansf> select * from users where key='jsmith';

 key    | birth_year | gender | password | session_token | state
--------+------------+--------+----------+---------------+-------
 jsmith |       null |   null |  chadsfl |          test |  null

3.向Columnfamily中增加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=2300 [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 432000 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

(1 rows)
cqlsh:duansf> delete from users where key='jsmith';  //刪除key=jsmith的行
cqlsh:duansf> select * from users;

 key | birth_year | gender | password | session_token | state
-----+------------+--------+----------+---------------+-------

(0 rows)
cqlsh:duansf>

9.刪除columnfamily和keyspace

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

刪除keyspace

cqlsh:duansf> drop keyspace duansf;
cqlsh:duansf> use duansf;
InvalidRequest: code=2200 [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 = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99.0PERCENTILE';

總結

以上是生活随笔為你收集整理的CQL操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。