Cassandra基础配置
一,集群部署
# conf/cassandra.yaml
#集群名称配置,默认Test Cluster
cluster_name: 'Test Cluster'
#种子节点配置,若为单节点也可配置为127.0.0.1
#下方示例为三节点都作为种子节点
seed_provider:
# Addresses of hosts that are deemed contact points.
# Cassandra nodes use this list of hosts to find each other and learn
# the topology of the ring. You must change this if you are running
# multiple nodes!
- class_name: org.apache.cassandra.locator.SimpleSeedProvider
parameters:
# seeds is actually a comma-delimited list of addresses.
# Ex: "<ip1>,<ip2>,<ip3>"
- seeds: "192.168.150.128,192.168.150.129,192.168.150.130"
# 当前节点的监听地址, 以192.168.150.128为例
listen_address: 192.168.150.128
# listen_interface: eth0
# listen_interface_prefer_ipv6: false
#远程调用机器(rpc)地址
rpc_address: 192.168.150.128
# rpc_interface: eth1
# rpc_interface_prefer_ipv6: false
# port for Thrift to listen for clients on
rpc_port: 9160
二,集群启动
#bin文件
./cassandra
#查看集群启动
[root@master bin]$./nodetool status
#数据中心:datacenter1 ,部署位置:rack1, 环上地址个数:256
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
-- Address Load Tokens Owns (effective) Host ID Rack
UN 192.168.150.128 317.42 KB 256 66.0% 23db0622-72f1-4e1a-a7e2-64d8a24933de rack1
UN 192.168.150.129 317.42 KB 256 65.2% 18eaf078-9037-410b-a563-747b676a4434 rack1
UN 192.168.150.130 319.25 KB 256 68.7% 1db719e0-620f-45c4-b08c-26613646eaab rack1
#查看集群信息
[root@master bin]$ ./nodetool info
ID : 23db0622-72f1-4e1a-a7e2-64d8a24933de
Gossip active : true
Thrift active : false
Native Transport active: true
Load : 317.42 KB
Generation No : 1668513274
Uptime (seconds) : 147
Heap Memory (MB) : 72.32 / 1004.00
Off Heap Memory (MB) : 0.00
Data Center : datacenter1
Rack : rack1
Exceptions : 0
Key Cache : entries 35, size 2.62 KB, capacity 50 MB, 65 hits, 103 requests, 0.631 recent hit rate, 14400 save period in seconds
Row Cache : entries 0, size 0 bytes, capacity 0 bytes, 0 hits, 0 requests, NaN recent hit rate, 0 save period in seconds
Counter Cache : entries 0, size 0 bytes, capacity 25 MB, 0 hits, 0 requests, NaN recent hit rate, 7200 save period in seconds
Token : (invoke with -T/--tokens to see all 256 tokens)
三,CQL和cqlsh
#进入shell
[root@master bin]$ ./cqlsh 192.168.150.128
Connected to Test Cluster at 192.168.150.128:9042.
[cqlsh 5.0.1 | Cassandra 3.0.8 | CQL spec 3.4.0 | Native protocol v4]
Use HELP for help.
cqlsh>
#查看键空间
cqlsh> desc keyspaces;
语法说明与mysql类似
#创建键空间
create keyspace "KeySpaceName" with replication = {'class':'Strategy name', 'replication_factor':n};
# replication 包含两个属性:Stratetgy name , replication_factor
#Stratrgy name 包含两个选项:SimpleStrategy(简单复制策略), NetworkTopologyStrategy(网络拓扑复制策略)
#SimpleStrategy(简单复制策略)
create keyspace simple_ks with replication = {'class':'SimpleStrategy','replication_factor':1};
#NetworkTopologyStrategy(网络拓扑复制策略),durable_writes = false关闭预写日志
create keyspace network_ks with replication = {'class':'NetworkTopologyStrategy','dc1':3,'dc2':2} and durable_writes = false;
#切换键空间
cqlsh:ks1> use simple_ks;
#修改键空间属性
cqlsh:ks1> alter keyspace simple_ks with replication = {'class':'NetworkTopologyStrategy','dc1':3,'dc2':2} and durable_writes = false;
#创建表 ,双主键
CREATE TABLE ks1.city_s (
id int,
name text,
city text,
phone list<text>,
salary int,
PRIMARY KEY (id, name)
);
#条件查询时要按主键顺序,不然报错
cqlsh:ks1> select * from city_S where name = 'tom';
InvalidRequest: code=2200 [Invalid query] message="Cannot execute this query as it might involve data filtering and thus may have unpredictable performance. If you want to execute this query despite the performance unpredictability, use ALLOW FILTERING"
cqlsh:ks1> select * from city_S where id = 1 and name = 'tom';
#无索引机制下但条件查询
select * from city_S where name = 'tom' allow filtering;
#索引机制,
create index city_a on city_s(city);
#IF轻量级事务
cqlsh:ks1> insert into ks1.city_s(id,name,city,phone,salary) values(4,'cxv','zhejiang',['123314','2352525'],8000) if not exists;