cassandra數據庫


配置java環境

#下載相應的jdk軟件包,然后解壓安裝,我這里包名稱為:jdk-8u91-linux-x64.tar.gz

tar -xzf  jdk-8u91-linux-x64.tar.gz  ; mkdir -p /usr/java/ ; mv jdk1.8.0_25/ /usr/java/jdk1.8

#然后配置環境變量,這樣可以任何地方引用jdk,如下配置:

#vi /etc/profile 最后面加入以下語句:

export JAVA_HOME=/usr/java/jdk1.8

export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin

#source /etc/profile   #使環境變量馬上生效

#java -version    #查看java版本,看到jdk1.8.0版本即代表java安裝成功。

 

搭建cassandra

tar xf apache-cassandra-3.11.4-bin.tar.gz  && mv apache-cassandra-3.11.4 /usr/local/cassandra

cd /usr/local/cassandra/bin/ && ./cassandra -R

 

啟動cql客戶端

python cql.sh

keyspace鍵空間操作 keyspace名不超過32個字符

create keyspace ks1 with replication = { 'class':'SimpleStrategy','replication_factor':1 }; 創建 with后接鍵空間屬性,class:復制協議 replication_factor 幾個副本

alter keyspace ks1 with replication = { 'class':'SimpleStrategy','replication_factor':2 }; 修改

desc keyspace ks1; 查詢鍵空間結構

drop keyspace ks1; 刪除

 

cql創建

創建列族 table 列族名不超過32字符

create table testtable( name text, age int, profile text, PRIMARY KEY (name), );  textstrings PRIMARY KEY指定主鍵

desc table testtable; 查看列族結構

alter table testtable with comment='test'; 修改列族屬性 comment描述信息

alter table testtable add sex text; 添加列

alter table testtable drop sex; 刪除列

drop table testtable; 刪除列族

 

創建index 第二索引

create table student(orderid int,name text,age int,sex text,primary key(orderid));

create index on student(name); student name創建作為第二索引

desc table student;  注釋:CREATE INDEX student_name_idx ON ks1.student (name);  student_name_idx即為默認索引名

drop index student_name_idx; 刪除第二索引

 

創建自定義數據類型

create type address(country text,provinace text,city text,road text);

desc type address; 查看自定義數據類型

alter type address add roomno int; 修改增加列

drop type address; 刪除自定義數據類型

 

cql操作

insert into student(name,age,orderid,sex) values('zhangshan',20,10001,'man');

select * from student; 查詢是否插入

update student set name='lisi' where orderid=10001; 修改數據

delete age from student where orderid=10001; 刪除agevalue數據 但age列不刪除

delete from student where orderid=10001;  刪除orderid=10001整行數據

 

cql查詢操作

insert into student(name,age,orderid,sex) values('zhangshan',20,10001,'man');

desc student;

select * from student where orderid=10001;  primary-key 可以作為where查詢條件

 

select * from student where name='zhangshan'; 會報錯 提示使用第二索引或 ALLOW FILTERING

create index on student(name);

select * from student where name='zhangshan';

 

select count(*) from student; 統計查詢到有多少行數據

select count(*) as totalrow from student; 給輸出顯示的count起一個別名totalrow

 

map和復合主鍵

create table scores(name text,score map<text,int>,orderid int, primary key(orderid,name));  nameorderid都為主鍵

select * from scores where name=' ' allow filtering; 復合主鍵除了第一個主鍵之外使用其他主鍵作為where查詢條件會報錯,除非加上allow filtering

 

insert into scores(name,orderid,score) values ('zhanshan',10001,{'yuwen':89,'shuxue':99});  

score map<text,int>  map表示scorevaluekey(text): value(int)

 

contains條件 包含的意思 包含這個關鍵字的都查出來

insert into scores(name,orderid,score) values ('lisi',10002,{'yuwen':89,'shuxue':99,'yinyu':100});

create index on scores(keys(score)); map數據類型鍵作為索引 將score的關鍵字作為索引

select * from scores where score contains key 'yinyu';  查詢score列包含key里面含有yinyu關鍵字的數據

 

in條件查詢 in里面含有的都查出來

select * from scores where orderid in (10001,10002);

 

cassandracql語言與傳統關系數據庫sql語言的相似對比

 

更多cql語法查詢:https://www.w3cschool.cn/cassandra/

 

主配置文件解讀

vim cassandra.yaml

cluster_name: 'Test Cluster' 集群名字,即當前節點在cassandra集群中叫什么名字,每個節點集群名不一樣

listen_address: localhost 集群監聽地址,一般0.0.0.0

seed_provider  需要聯系的節點地址

storage_port: 7000  節點間通訊端口

native_transport_port: 9042 本地客戶端通信端口

data_file_directories 數據文件的存放目錄

commitlog_directory  commitlog保存路徑

commit_failure_policy 提交失敗時采取的策略

disk_failure_policy 磁盤故障策略

rpc_address: localhost 監聽客戶端連接的地址

 

nodetool運維工具

./nodetool version 查看cassandra版本

./nodetool status 查看節點狀態

./nodetool stopdaemon 關閉cassandra服務

./nodetool clearsnapshot 刪除所有快照 刪除之前把所有有用的快照移到別的目錄

./nodetool -h 127.0.0.1 netstats 查看節點網絡連接信息

./nodetool compact 合並sstable文件

./nodetool compactionstats 顯示壓縮進度

./nodetool upgradesstables -a  更新sstable

./nodetool snapshot -t NAME  為鍵空間或列族創建快照 -t 指定快照名,不指定以當前時間戳作為快照名

./nodetool clearsnapshot -t SNAPSHOTNAME 指定要刪除快照的名字 不指定清理全部快照

./nodetool refresh +keyspace +TABLENAME  加載新的sstable文件到集群中 恢復快照

./nodetool decommission 關閉當前節點,並把數據復制到環中緊鄰的下一個節點

./nodetool describecluster 描述集群信息

./nodetool describering +KEYSPACE_NAME  顯示圓環的節點信息

./nodetool drain memtable中的數據刷新sstable,並且當前節點會終止與其他節點的聯系,執行完這條命令需要重啟節點,一般在cassandra版本升級的時候才使用這條命令

./nodetool flush 單純的把memtable中的數據刷新到sstable,不需要重啟節點

./nodetool getendpoints 查看key分布在哪個節點上,需要三個參數: keyspace_name,table_name,key_name

./nodetool getsstables 查看key分布在哪一個sstable上,需要三個參數:keyspace_name,table_name,key_name

./nodetool rebuild 當有新的數據中心加入,運行該命令復制數據到新數據中心

./nodetool repair 在刪除數據時候,cassandra並非真實的刪除,而是重新插入一條數據,記錄了刪除的記錄的信息和時間,叫做tombstone墓碑。使用nodetool repair,可以刪除tombstone數據。頻繁修改的數據節點可以使用這個命令節省空間,提高讀速度

./nodetool tpstats 列出cassandra維護的線程池的信息,可以看到每個階段的操作,以及他們的狀態是活動中,等待還是完成

./nodetool cfstats 列族名 -H   查看表的一些信息,包括讀的次數,寫的次數, sstable的數量,memtable信息,壓縮信息,bloomfilter信息

./nodetool cleanup 清理不需要的keyspace,當新增的數據節點或減少數據節點時,數據會重新分發,可以運行這個命令,清除不再分布在這個節點上的keyspace,唯一目的是為了節省磁盤空間

 

壓力測試

tools/bin/cassandra-stress write -n=1000000 插入1000000數據

tools/bin/cassandra-stress read n=200000 讀取200000數據

tools/bin/cassandra-stress read duration=3m 持續讀取3分鍾數據

 

列族參數

desc ks1.scores;

    AND bloom_filter_fp_chance = 0.01 指定bloom_filter算法的容錯率,一般 0.01-0.1

    AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'} 設置緩存方案

    AND comment = ''   描述信息

    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32', 'min_threshold': '4'} 數據壓縮策略

    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'} 數據壓縮算法

    AND crc_check_chance = 1.0

    AND dclocal_read_repair_chance = 0.1

    AND default_time_to_live = 0 數據存活時間,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  0-1之間的數值,與數據的一致性有關

AND speculative_retry = '99PERCENTILE';


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM