Cassandra简介及基本操作

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 CONTAINS789-xxx-1234ALTER 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';
发布了38 篇原创文章 · 获赞 46 · 访问量 1410

猜你喜欢

转载自blog.csdn.net/weixin_45568892/article/details/105533813