赞
踩
macOS 使用交流 QQ 群:658095824,V : ez-code
官网:https://cassandra.apache.org
github:https://github.com/apache/cassandra
Cassandra是一套开源分布式NoSQL数据库系统。
它最初由Facebook开发,用于储存收件箱等简单格式数据,集Google BigTable的数据模型与Amazon Dynamo的完全分布式的架构于一身。
2008年7月由Facebook开放,2009年3月被Apache Incubator接受收编,2010年2月起作为Apache的顶级项目。
$ brew install cassandra
会被安装到路径 /usr/local/Cellar/cassandra
$ cassandra -v # 查看版本
3.11.8
$ cassandra -h # 帮助、常用命令
Usage: /usr/local/Cellar/cassandra/3.11.8/libexec/bin/cassandra [-f] [-h] [-p pidfile] [-H dumpfile] [-E errorfile]
/usr/local/etc/cassandra
/usr/local/var/log/cassandra
/usr/local/var/lib/cassandra/data
~/Library/LaunchAgents/homebrew.mxcl.cassandra.plist
# 启动
$ brew services start cassandra
==> Successfully started `cassandra` (label: homebrew.mxcl.cassandra)
# 停止
$ brew services stop cassandra
Stopping `cassandra`... (might take a while)
==> Successfully stopped `cassandra` (label: homebrew.mxcl.cassandra)
$ brew services list # 查看brew 已开启的服务列表
Name Status User Plist
cassandra started xx /Users/xx/Library/LaunchAgents/homebrew.mxcl.cassandra.plist
$ ps -ef | grep cassandra # 查看 cassandra 进程
501 9782 9080 0 4:51下午 ttys001 0:00.00 grep cassandra
以上启动的方式,可能没有正确启动(使用 cqlsh 报错),可以执行下面命令来启动,报错也更详细:
$ cassandra -f
If you start up Cassandra without the “-f” option, it will run in the background.
You can stop the process by killing it, using ‘pkill -f CassandraDaemon’
和关系型数据库相对比,主要元素如下:
关系型数据库 | cassandra |
---|---|
database | keyspace |
table | cf(column family) |
Primary Key | Primary Key |
Column Name | Key / Column Name |
Column Value | Column Value |
mac 客户端可使用 RazorSQL(付费可试用),下载地址:
https://www.razorsql.com/download_mac.html
连接
查看
CQL 是 Cassandra Query Language,Cassandra查询语言;
(neo4j 使用的 CQL 与此不同,是 Cypher Query Language)
cqlsh 是命令行shell,通过 CQL 来和 Cassandra 交互通信;
查看上述软件版本
$ python --version
Python 2.7.12
$ java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
如果你装有多个 python 和 java,需要注意环境变量(~/.bash_profile
)中的配置
依据:https://cassandra.apache.org/doc/latest/new/java11.html
如果不是上述版本,执行 cqlsh
可能会报如下错误:
python2.7: command not found
Could not create the Java Virtual Machine
Connection error: ('Unable to connect to any servers', {'127.0.0.1:9042': error(61, "Tried connecting to [('127.0.0.1', 9042)]. Last error: Connection refused")})
$ cqlsh --version
cqlsh 5.0.1
$ cqlsh --help
Usage: cqlsh.py [options] [host [port]]
CQL Shell for Apache Cassandra
Options:
--version show program's version number and exit
-h, --help show this help message and exit
-C, --color Always use color output
--no-color Never use color output
--browser=BROWSER The browser to use to display CQL help, where BROWSER
can be:
- one of the supported browsers in
https://docs.python.org/2/library/webbrowser.html.
- browser path followed by %s, example: /usr/bin
/google-chrome-stable %s
--ssl Use SSL
--no_compact No Compact
-u USERNAME, --username=USERNAME
Authenticate as user.
-p PASSWORD, --password=PASSWORD
Authenticate using password.
-k KEYSPACE, --keyspace=KEYSPACE
Authenticate to the given keyspace.
-f FILE, --file=FILE Execute commands from FILE, then exit
--debug Show additional debugging information
--encoding=ENCODING Specify a non-default encoding for output. (Default:
utf-8)
--cqlshrc=CQLSHRC Specify an alternative cqlshrc file location.
--cqlversion=CQLVERSION
Specify a particular CQL version, by default the
highest version supported by the server will be used.
Examples: "3.0.3", "3.1.0"
--protocol-version=PROTOCOL_VERSION
Specify a specific protcol version otherwise the
client will default and downgrade as necessary
-e EXECUTE, --execute=EXECUTE
Execute the statement and quit.
--connect-timeout=CONNECT_TIMEOUT
Specify the connection timeout in seconds (default: 5
seconds).
--request-timeout=REQUEST_TIMEOUT
Specify the default request timeout in seconds
(default: 10 seconds).
-t, --tty Force tty mode (command prompt).
Connects to 127.0.0.1:9042 by default. These defaults can be changed by
setting $CQLSH_HOST and/or $CQLSH_PORT. When a host (and optional port number)
are given on the command line, they take precedence over any defaults.
$ cqlsh 12.34.56.78 1234 -u username -p password --cqlversion="3.2.0"
cqlsh (IP ADDR) (PORT) (DB_USERN) (DB_PASS) (VER)
$ cqlsh 201.120.90.21 # 连接远程
Connected to Test Cluster at 201.120.90.21:9042.
[cqlsh 5.0.1 | Cassandra 3.11.6 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> exit # 退出
$
$ cqlsh # 连接本地
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.11.8 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> exit
$
Connection error: ('Unable to connect to any servers', {'1xx.xx.xx.xx': OperationTimedOut('errors=None, last_host=None',)})
在 安装路径下的 bin/cqlsh.py
文件中设置
DEFAULT_CONNECT_TIMEOUT_SECONDS = 3600
DEFAULT_REQUEST_TIMEOUT_SECONDS = 3600
$ cqlsh # 连接本地 cassandra
Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.11.8 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cqlsh> describe cluster # 查看集群信息
Cluster: Test Cluster
Partitioner: Murmur3Partitioner
cqlsh> describe keyspaces; # 列出集群中的所有键空间
system_traces system_schema system_auth system system_distributed
cqlsh> describe tables # 列出键空间所有表
Keyspace system_traces
----------------------
events sessions
Keyspace system_schema
----------------------
tables triggers views keyspaces dropped_columns
functions aggregates indexes types "columns"
...
cqlsh> use bandao ; # 进入 bandao 这个 keyspace
cqlsh:bandao> describe tables;
whole
cqlsh:bandao> CREATE TABLE IF NOT EXISTS bandao.marked( artid varchar PRIMARY KEY , url text, marked text, trans text, status text); # 创建表
cqlsh> help # 查看 cqlsh 常用命令等
Documented shell commands:
===========================
CAPTURE CLS COPY DESCRIBE EXPAND LOGIN SERIAL SOURCE UNICODE
CLEAR CONSISTENCY DESC EXIT HELP PAGING SHOW TRACING
CQL help topics:
================
AGGREGATES CREATE_KEYSPACE DROP_TRIGGER TEXT
ALTER_KEYSPACE CREATE_MATERIALIZED_VIEW DROP_TYPE TIME
ALTER_MATERIALIZED_VIEW CREATE_ROLE DROP_USER TIMESTAMP
ALTER_TABLE CREATE_TABLE FUNCTIONS TRUNCATE
ALTER_TYPE CREATE_TRIGGER GRANT TYPES
ALTER_USER CREATE_TYPE INSERT UPDATE
APPLY CREATE_USER INSERT_JSON USE
ASCII DATE INT UUID
BATCH DELETE JSON
BEGIN DROP_AGGREGATE KEYWORDS
BLOB DROP_COLUMNFAMILY LIST_PERMISSIONS
BOOLEAN DROP_FUNCTION LIST_ROLES
COUNTER DROP_INDEX LIST_USERS
CREATE_AGGREGATE DROP_KEYSPACE PERMISSIONS
CREATE_COLUMNFAMILY DROP_MATERIALIZED_VIEW REVOKE
CREATE_FUNCTION DROP_ROLE SELECT
CREATE_INDEX DROP_TABLE SELECT_JSON
cqlsh> describe table peers # 查看表信息(错误)
No keyspace specified and no current keyspace
cqlsh> describe table system.peers # 查看表结构信息
CREATE TABLE system.peers (
peer inet PRIMARY KEY,
data_center text,
host_id uuid,
preferred_ip inet,
rack text,
release_version text,
rpc_address inet,
schema_version uuid,
tokens set<text>
) WITH bloom_filter_fp_chance = 0.01
AND caching = {'keys': 'ALL', 'rows_per_partition': 'NONE'}
AND comment = 'information about known peers in the cluster'
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.0
AND default_time_to_live = 0
AND gc_grace_seconds = 0
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 3600000
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99PERCENTILE';
cqlsh> select * from system.peers # 查看表内容
... ;
peer | data_center | host_id | preferred_ip | rack | release_version | rpc_address | schema_version | tokens
------+-------------+---------+--------------+------+-----------------+-------------+----------------+--------
(0 rows)
cqlsh> exit # 退出
cqlsh> show version
[cqlsh 5.0.1 | Cassandra 3.11.8 | CQL spec 3.4.4 | Native protocol v4]
cqlsh> show host
Connected to Test Cluster at 127.0.0.1:9042.
python 中可以使用以下库来操作 Cassandra:
$ pip install cassandra-driver
这里以查询为例
记得开启 Cassandra 之后再连接
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
cluster = Cluster(
# 此处填写数据库连接点地址(公网或者内网的),不要填端口!
contact_points=["127.0.0.1"]
)
session = cluster.connect()
cql01 = "SELECT * FROM news.article WHERE aid='WS6bac1419';"
rows = session.execute(cql01)
# 打印每行信息到控制台
for row in rows:
print("# row: {}".format(row))
# row: Row(artid='WS6bac1419', src='1232', tgt='dsada', title='Quora:有哪些 ', url='https://xxx.html')
print(type(row)) # cassandra.io.libevreactor.Row
print(row.artid, row.tgt, row.src) # 使用 . 读取属性
# 关闭Session
session.shutdown()
# 关闭
cluster.shutdown()
WARNING 信息有很多,可以暂时不管。
https://hub.docker.com/_/cassandra/
伊织 2021-05-24(一) 热
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。