Cassandra簡介及基本操作
一、簡介
Apache Cassandra是一套開源分布式NoSQL資料庫系統。它最初由Facebook開發,用于儲存收件箱等簡單格式資料,集Google BigTable的資料模型與Amazon Dynamo的完全分布式的架構于一身。Facebook于2008将 Cassandra 開源,後面由于Cassandra良好的可擴放性,被Digg、Twitter等知名Web 2.0網站所采納,成為了一種流行的分布式結構化資料存儲方案。
它是一個開源的、分布式、無中心、支援水準擴充、高可用的key-value類型的NOSQL資料庫。系統架構是由亞馬遜的Dynamo與Google 的BigTable兩部分組成。
二、資料庫相關概念
一緻性
什麼叫資料庫的一緻性?讀操作一定會傳回最新寫入的結果。
Cassandra是最終一緻性(弱一緻性):成功寫入後,讀取的并不一定是最新資料,但過一段時間(毫秒級别,跨機房時間會更長)所有副本才會達成一緻。 Cassandra是最終一緻性原因:優化寫入性能,支援ONE、ALL等。Cassandra支援緻性調節:當要求成功寫入節點數與副本數一緻時,即ALL時,認為是強一緻性的。
CAP理論
CAP理論指出在一個分布式系統中,隻能強化其中兩個方面
- Consistent:一緻性,每次讀取都是最新資料
- Available:可用性,用戶端總是可以讀寫資料
- Partition Tolerance:分區耐受性,資料庫分散到多台機器,即使某台機器故障,也可以提供服務
三、基本操作
進入互動界面
cqlsh
3.1 keyspaces操作
-- 檢視keyspaces
desc keyspaecs;
-- 建立keyspaces
CREATE KEYSPACE patient WITH replication = { ‘class’: ‘SimpleStrategy’, ‘replication_factor’: 3 };
-- 進入keyspaces
use patient;
-- 檢視keyspaces
desc patient;
-- 修改keyspaces
ALTER KEYSPACE events WITH replication =
{ 'class':'NetworkTopologyStrategy','replication_factor' : 3};
-- 删除keyspaces
drop keyspaces patient;
3.2 table操作
-- 檢視tables
desc tables;
-- 删除table
drop table users;
-- 清空表
truncate users;
-- 建立table
-- The id is the partition-key
CREATE TABLE patient.exam(
id int,
patient_id int, date timeuuid, detail text, PRIMARY KEY (id));
-- The patient_id is the clustering column
CREATE TABLE patient.exam(
id int,
patient_id int,
date timeuuid,
detail text, PRIMARY KEY (id, patient_id)
) WITH CLUSTERING ORDER BY (patient_id ASC);
-- 修改表
ALTER TABLE it21.teams ALTER ID TYPE uuid;
ALTER TABLE it21.players ADD first_name text;
ALTER TABLE it21.calendar ADD events list<text>;
ALTER TABLE it21.customers DROP birth_year;
-- 增加、修改index
CREATE INDEX user_state ON it21.users (state);
CREATE INDEX ON it21.users (zip);
ALTER TABLE users ADD phones set<text>;
-- index on set
CREATE INDEX ON users (phones);
Select * from users where phones CONTAINS ‘789-xxx-1234’
ALTER TABLE users ADD titles map<text, text>;
-- index on map keys
CREATE INDEX ON users (KEYS(titles));
Select * from users where titles CONTAINS KEY ‘IT Dept’
3.3 CRUD
use patient;
INSERT INTO exam (patient_id, id, date, detail) values (1, 1, now(), 'first exam patient 1');
INSERT INTO exam (patient_id, id, date, detail) values (1, 2, now(), 'second exam patient 1');
INSERT INTO exam (patient_id, id, date, detail) values (2, 1, now(), 'first exam patient 2');
INSERT INTO exam (patient_id, id, date, detail) values (3, 1, now(), 'first exam patient 3');
UPDATE exam SET partient_id = 12 WHERE id = 6;
DELETE FROM exam WHERE id = 123;
3.4 導入導出資料
-- 建立KEYSPACE
CREATE KEYSPACE nml WITH replication = {
'class': 'SimpleStrategy',
'replication_factor': 3 };
-- 建立TABLE
CREATE TABLE airplanes (
name text PRIMARY KEY, manufacturer ascii, year int, mach float
);
-- 插入資料
INSERT INTO airplanes(name, manufacturer, year, mach)
VALUES ('J100', 'CN', 1997, 0.7);
-- 導出資料
COPY airplanes (name, manufacturer, year, mach) TO 'demo.csv';
TRUNCATE airplanes;
-- 導入資料
COPY airplanes (name, manufacturer, year, mach) FROM 'demo.csv';