12.3 Cassandra數據定義

12.3 Cassandra數據定義

卜算子·大數據”一個開源、成體系的大數據學習教程。——每週日更新

本節主要內容:

  • 數據定義

12.3.1 Cassandra Query Language (CQL)

CQL是Cassandra提供的接近SQL的模型,因爲數據包含在行列的表中,CQL中的表,行,列的定義與SQL是相同的。

12.3.2 啓動cqlsh

cqlsh

12.3.3 Clusters 集羣

集羣是Cassandra集羣部署的名稱標誌,避免集羣A中的機器加入其它的集羣(如B)。

12.3.4 Keyspaces 鍵空間

鍵空間(Keyspace)是表的容器。

查看當前有哪些鍵空間可用。

DESCRIBE KEYSPACES

創建鍵空間

CREATE KEYSPACE busuanzi_org WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1};

選擇鍵空間

USE busuanzi_org;

刪除鍵空間

Drop keysapce_name

這裏寫圖片描述

12.3.5 tables 表

創建 busuanzi.org 創建表

CREATE TABLE top_n_url (
    username text,
    projects text,
    star_number int,
    comment text static,
    PRIMARY KEY ((username, projects), star_number)
);

插入數據

INSERT INTO top_n_url (username, projects, star_number, comment) VALUES ('wangxiaolei', 'big-data', 89, 'old');
INSERT INTO top_n_url (username, projects, star_number, comment) VALUES ('wangxiaolei', 'big-data', 101, 'new');
INSERT INTO top_n_url (username, projects, star_number, comment) VALUES ('wangxiaolei', 'machine-learning', 10, 'good');
INSERT INTO top_n_url (username, projects, star_number, comment) VALUES ('wangxiaolei', 'machine-learning', 78, 'nice');
INSERT INTO top_n_url (username, projects, star_number, comment) VALUES ('zhangsan', 'machine-learning', 1, 'ok');

查詢top_n_url

cqlsh:busuanzi_org> SELECT * FROM top_n_url ;

 username    | projects         | star_number | comment
-------------+------------------+-------------+---------
 wangxiaolei |         big-data |          89 |     new
 wangxiaolei |         big-data |         101 |     new
    zhangsan | machine-learning |           1 |      ok
 wangxiaolei | machine-learning |          10 |    nice
 wangxiaolei | machine-learning |          78 |    nice

(5 rows)

這裏寫圖片描述

12.3.6 Cloumns 列

列和關係型數據有類似的地方爲可以定義字段類型,區別在還可以定義主鍵和靜態。

12.3.6.1 主鍵(The Primay key )

主鍵和關係型數據庫的主鍵類似,具有唯一與檢索等屬性。在Cassandra中,主鍵的區別是,主鍵由兩部分組成:

  • 分區鍵(partition key),主鍵第一個或者第一組是分區鍵。
  • 聚類鍵(clustering cloumns),主鍵的第二個及之後的鍵爲聚類鍵。

busuanzi.org案例表中,查詢出,row1,row2具有相同分區,row4,ro5具有相同分區,row3單獨分區。

12.3.6.2 靜態(static)

靜態的列將會將數值分享給該分區其他的行。(就是說一個分區中,所有行的靜態列的值相同)
靜態的限制:

  • 表中沒有聚類鍵,不可以有靜態(因爲每一個分區都是唯一的行,所以每個列本質上是靜態)的列。
  • 主鍵的列,不可以是靜態。
  • 表的屬性是COMPACT STORAGE的不能有靜態的列。
    總結,只有存在聚類的時候,非主鍵的列可以是靜態的列,並且該表不是COMPACT STORAGE屬性。

busuanzi.org案例表中,查詢出,在同一分區中,靜態列”comment”中,”new”替換了”old”,”nice”替換了”good”。

這裏寫圖片描述本節完成

長按關注從入門到精通

這裏寫圖片描述

開源、源碼獲取 https://github.com/wangxiaoleiAI/big-data


分割線以下內容可忽略

這裏寫圖片描述 拓展部分

這裏寫圖片描述 No appropriate python interpreter found.

cqlsh 需要系統安裝python2.7,而ubuntu18.04 server版默認安裝的是Python3
cqlsh localhost
No appropriate python interpreter found.

解決

sudo apt install python
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章