Cassandra之安装

这里没有按apache官网上的根据各系统而采用的各种安装。(可以参考:https://cassandra.apache.org/doc/latest/cassandra/getting_started/installing.html)

[hadoop@datax cassandra]$ git clone https://github.com/apache/cassandra.git (二进制安装)
Cloning into 'cassandra'...
remote: Enumerating objects: 376728, done.
remote: Counting objects: 100% (122/122), done.
remote: Compressing objects: 100% (78/78), done.
remote: Total 376728 (delta 27), reused 92 (delta 15), pack-reused 376606
Receiving objects: 100% (376728/376728), 378.17 MiB | 6.32 MiB/s, done.
Resolving deltas: 100% (219054/219054), done.

 

[hadoop@datax ~]$ sudo cat /etc/profile

export CASSANDRA_HOME=/data/cassandra/apache-cassandra-4.0.0
export PATH=$PATH:$CASSANDRA_HOME/bin

 

[root@datax cassandra]# su - hadoop
Last login: Tue Dec 20 14:38:15 CST 2022 on pts/0
[hadoop@datax ~]$ cassandra -f
Error opening zip file or JAR manifest missing : /data/cassandra/lib/jamm-0.3.2.jar
Error occurred during initialization of VM
agent library failed to init: instrument

 

修改配置目录

 

data_file_directories:
- /var/lib/cassandra/data
commitlog_directory: /var/lib/cassandra/commitlog
cdc_enabled: false
disk_failure_policy: stop
commit_failure_policy: stop
prepared_statements_cache_size_mb:
key_cache_size_in_mb:
key_cache_save_period: 14400
row_cache_size_in_mb: 0
row_cache_save_period: 0
counter_cache_size_in_mb:
counter_cache_save_period: 7200
saved_caches_directory: /var/lib/cassandra/saved_caches

 

https://archive.apache.org/dist/cassandra/4.0.0/  各个版本

  ./cassandra #开启

[hadoop@datax ~]$ cqlsh
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.0.0 | Cassandra 4.0.0 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cqlsh>

 

cqlsh> show VERSION
[cqlsh 6.0.0 | Cassandra 4.0.0 | CQL spec 3.4.5 | Native protocol v5]
cqlsh>

 

[hadoop@datax ~]$ netstat -antlp
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9042 0.0.0.0:* LISTEN 5199/java
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN -
tcp 0 0 127.0.0.1:7000 0.0.0.0:* LISTEN 5199/java
tcp 0 0 127.0.0.1:44538 0.0.0.0:* LISTEN 5199/java
tcp 0 0 127.0.0.1:7199 0.0.0.0:* LISTEN 5199/java
tcp 0 36 192.168.159.150:22 192.168.159.110:55562 ESTABLISHED -
tcp6 0 0 :::22 :::* LISTEN -

 关闭进程:

[hadoop@datax bin]$ ls -la
total 152
drwxr-xr-x 2 hadoop hadoop 230 Dec 20 18:25 .
drwxr-xr-x 10 hadoop hadoop 200 Dec 20 16:09 ..
-rwxr-xr-x 1 hadoop hadoop 10542 Jul 23 2021 cassandra
-rw-r--r-- 1 hadoop hadoop 5641 Jul 23 2021 cassandra.in.sh
-rwxr-xr-x 1 hadoop hadoop 2995 Jul 23 2021 cqlsh
-rwxr-xr-x 1 hadoop hadoop 95408 Jul 23 2021 cqlsh.py
-rwxr-xr-x 1 hadoop hadoop 1894 Jul 23 2021 debug-cql
-rwxr-xr-x 1 hadoop hadoop 3491 Jul 23 2021 nodetool
-rwxr-xr-x 1 hadoop hadoop 1770 Jul 23 2021 sstableloader
-rwxr-xr-x 1 hadoop hadoop 1778 Jul 23 2021 sstablescrub
-rwxr-xr-x 1 hadoop hadoop 1778 Jul 23 2021 sstableupgrade
-rwxr-xr-x 1 hadoop hadoop 1781 Jul 23 2021 sstableutil
-rwxr-xr-x 1 hadoop hadoop 1778 Jul 23 2021 sstableverify
-rwxr-xr-x 1 hadoop hadoop 1171 Dec 20 18:17 stop-server
[hadoop@datax bin]$ pwd
/data/cassandra/apache-cassandra-4.0.0/bin
[hadoop@datax bin]$ cat stop-server
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


echo "please read the stop-server script before use"

# if you are using the cassandra start script with -p, this
# is the best way to stop:

# kill `cat <pidfile>`


# otherwise, you can run something like this, but
# this is a shotgun approach and will kill other processes
# with cassandra in their name or arguments too:

user=`whoami`
pgrep -u $user -f cassandra | xargs kill -9
[hadoop@datax bin]$

 

以下是docker安装方式

第 1 步:使用 DOCKER 获取 CASSANDRA

您需要在计算机上安装适用于 Mac 的 Docker Desktop、适用于 Windows 的 Docker Desktop 或类似软件。

Apache Cassandra 也可以作为 tarball 或包下载

docker pull cassandra:latest

第 2 步:启动卡桑德拉

Docker 网络允许我们访问容器的端口,而无需将它们暴露在主机上。

docker network create cassandra

docker run --rm -d --name cassandra --hostname cassandra --network cassandra cassandra

第 3 步:创建文件

Cassandra 查询语言 (CQL) 与 SQL 非常相似,但适用于 Cassandra 的 JOINless 结构。

创建一个名为 data.cql 的文件并将以下 CQL 脚本粘贴到其中。该脚本将创建一个键空间,即 Cassandra 复制其数据的层,一个用于保存数据的表,并将一些数据插入该表:

-- Create a keyspace
CREATE KEYSPACE IF NOT EXISTS store WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : '1' };

-- Create a table
CREATE TABLE IF NOT EXISTS store.shopping_cart (
userid text PRIMARY KEY,
item_count int,
last_update_timestamp timestamp
);

-- Insert some data
INSERT INTO store.shopping_cart
(userid, item_count, last_update_timestamp)
VALUES ('9876', 2, toTimeStamp(now()));
INSERT INTO store.shopping_cart
(userid, item_count, last_update_timestamp)
VALUES ('1234', 5, toTimeStamp(now()));

第 4 步:使用 CQLSH 加载数据

CQL shell 或 cqlsh 是一种用于与数据库交互的工具。我们将使用它使用您刚刚保存的脚本将一些数据加载到数据库中。

docker run --rm --network cassandra -v "$(pwd)/data.cql:/scripts/data.cql" -e CQLSH_HOST=cassandra -e CQLSH_PORT=9042 -e CQLVERSION=3.4.5 nuvo/docker-cqlsh

注意:cassandra 服务器本身(您运行的第一个 docker run 命令)需要几秒钟才能启动。如果服务器还没有完成它的初始化序列,上面的命令会抛出一个错误,所以给它几秒钟的时间来启动。

第 5 步:交互式 CQLSH

与 SQL shell 非常相似,您当然也可以使用 CQLSH 以交互方式运行 CQL 命令。

docker run --rm -it --network cassandra nuvo/docker-cqlsh cqlsh cassandra 9042 --cqlversion='3.4.5'

这应该会给你这样的提示:

Connected to Test Cluster at cassandra:9042.
[cqlsh 5.0.1 | Cassandra 4.0.4 | CQL spec 3.4.5 | Native protocol v5]
Use HELP for help.
cqlsh>


[root@datax tools]# docker exec -it cassandra cqlsh
Connected to Test Cluster at 127.0.0.1:9042
[cqlsh 6.1.0 | Cassandra 4.1.0 | CQL spec 3.4.6 | Native protocol v5]
Use HELP for help.
cqlsh> help

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> select * from store.shopping_cart ;

userid | item_count | last_update_timestamp
--------+------------+---------------------------------
1234 | 5 | 2022-12-20 07:40:12.488000+0000
9876 | 2 | 2022-12-20 07:40:11.444000+0000

(2 rows)

第 6 步:读取一些数据

 SELECT * FROM store.shopping_cart;

第 7 步:写入更多数据

 INSERT INTO store.shopping_cart (userid, item_count) VALUES ('4567', 20);

第 8 步:清理

docker kill cassandra
docker network rm cassandra
posted @ 2022-12-20 15:30  青空如璃  阅读(302)  评论(0编辑  收藏  举报