当前位置:   article > 正文

macOS - 安装配置使用 Cassandra_mac brew cassandra

mac brew cassandra

在这里插入图片描述



macOS 使用交流 QQ 群:658095824,V : ez-code


一、关于 Cassandra

官网:https://cassandra.apache.org
githubhttps://github.com/apache/cassandra

Cassandra是一套开源分布式NoSQL数据库系统。

它最初由Facebook开发,用于储存收件箱等简单格式数据,集Google BigTable的数据模型与Amazon Dynamo的完全分布式的架构于一身。

2008年7月由Facebook开放,2009年3月被Apache Incubator接受收编,2010年2月起作为Apache的顶级项目。


二、安装

1、使用 brew 安装


$ brew install cassandra
  • 1

会被安装到路径 /usr/local/Cellar/cassandra


2、查看版本

$ 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]
  • 1
  • 2
  • 3
  • 4
  • 5

3、相关文件地址

  • Properties:/usr/local/etc/cassandra
  • Logs:/usr/local/var/log/cassandra
  • Data:/usr/local/var/lib/cassandra/data
  • LaunchAgents:~/Library/LaunchAgents/homebrew.mxcl.cassandra.plist

4、启动和停止

# 启动
$ 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

以上启动的方式,可能没有正确启动(使用 cqlsh 报错),可以执行下面命令来启动,报错也更详细:

$ cassandra -f
  • 1

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 的结构

和关系型数据库相对比,主要元素如下:

关系型数据库cassandra
databasekeyspace
tablecf(column family)
Primary KeyPrimary Key
Column NameKey / Column Name
Column ValueColumn Value

四、GUI 客户端

mac 客户端可使用 RazorSQL(付费可试用),下载地址:
https://www.razorsql.com/download_mac.html


连接

在这里插入图片描述


查看
在这里插入图片描述


五、终端命令交互(cqlsh)

CQL 是 Cassandra Query Language,Cassandra查询语言;
(neo4j 使用的 CQL 与此不同,是 Cypher Query Language)

cqlsh 是命令行shell,通过 CQL 来和 Cassandra 交互通信;


依赖项

  • Python 2.7
  • Java 8

查看上述软件版本

$ 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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

如果你装有多个 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
  • 1
  • 2

$ 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.
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

连接服务器

$ cqlsh 12.34.56.78 1234 -u username -p password --cqlversion="3.2.0"
cqlsh (IP ADDR)  (PORT)  (DB_USERN)  (DB_PASS)               (VER)
  • 1
  • 2

$ 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  # 退出
$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

$ 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
$ 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

OperationTimedOut

Connection error: ('Unable to connect to any servers', {'1xx.xx.xx.xx': OperationTimedOut('errors=None, last_host=None',)})
  • 1

在 安装路径下的 bin/cqlsh.py 文件中设置

DEFAULT_CONNECT_TIMEOUT_SECONDS = 3600
DEFAULT_REQUEST_TIMEOUT_SECONDS = 3600
  • 1
  • 2

查看数据

$ 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);   # 创建表

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

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 # 退出
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

会话信息

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.
  • 1
  • 2
  • 3
  • 4


六、Python 操作 Cassandra(使用cassandra-driver)

1、python 的 Cassandra 库

python 中可以使用以下库来操作 Cassandra:


2、安装 cassandra-driver

$ pip install cassandra-driver
  • 1

3、使用示例

这里以查询为例

记得开启 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()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

WARNING 信息有很多,可以暂时不管。


Docker + Cassandra

https://hub.docker.com/_/cassandra/


相关资料


伊织 2021-05-24(一) 热

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/126298
推荐阅读
相关标签
  

闽ICP备14008679号