Cassandra安装部署详解

1、概述

1.1.官方文档

https://cassandra.apache.org/_/index.html

https://cassandra.apache.org/_/download.html

1.2、下载 cassandra-4.0.1

https://archive.apache.org/dist/cassandra/

https://archive.apache.org/dist/cassandra/4.0.1/

注意:只有3.11版本及其以下支持在windows下直接启动。

1.3配置服务器(看情况可以不用执行此步骤)

# 修改IP地址(看情况可以不用执行此步骤)

cd /etc/sysconfig/network-scripts

vim ifcfg-ens33

192.168.202.156

# 关闭防火墙(看情况可以不用执行此步骤)

systemctl stop firewalld

systemctl disable firewalld

# 设置主机名

hostnamectl set-hostname cassandra

2.安装准备

2.1安装JDK 8,最好是JDK11

注意:Cassandra 使用 JAVA 语言开发,首先在计算机中安装 JDK8或者JDK11

2.2、安装 Python

注意:Cassandra的客户端的使用需要用的Python2.X版本。需要先安装Python2.X

[root@cassandra cassandra]# python -V

Python 2.7.5

2.3、下载文件

# 下载 4.0.1

wget https://archive.apache.org/dist/cassandra/4.0.1/apache-cassandra-4.0.1-bin.tar.gz

# 解压

tar -zxvf apache-cassandra-4.0.1-bin.tar.gz

2.4、注意事项

cassandra很可能在windows系统中无法正常启动,测试的时候,请使用hyper-vmware虚拟机内纯linux系统来安装部署和启动cassandra数据库,在msys2、cygwin里面运行会有很大概率会出错。

同时,只有3.11版本及其以下支持在windows下直接启动。

3、安装部署

3.1配置 Cassandra

1.配置Cassandra

进入到Cassandra根目录,然后新建如下三个目录:

mkdir data

mkdir commitlog

mkdir saved-caches

设置CASSANDRA_HOME:

export CASSANDRA_HOME=/opt/softwares/install/cassandra

export CASSANDRA_HOME=D:\cassandra

3.2修改配置文件

Linux:

在 conf 目录中找到 cassandra.yaml 配置文件,配置上面创建的3个数据目录

配置 data_file_directories

data_file_directories:

    -  /opt/softwares/install/cassandra/data

配置 commitlog_directory

commitlog_directory:  /opt/softwares/install/cassandra/commitlog

配置 saved_caches_directory

saved_caches_directory:  /opt/softwares/install/cassandra/saved_caches

配置 RPC

rpc_address: localhost

listen_addres: loalhost

rpc_address就是所在的服务器的ip地址。

Windows:

配置 data_file_directories

data_file_directories:

    -  D:\cassandra\data

配置 commitlog_directory

commitlog_directory:  D:\cassandra\commitlog

配置 saved_caches_directory

saved_caches_directory:  D:\cassandra\saved_caches

配置 RPC

rpc_address: localhost

3.3启动 Cassandra

cd  /opt/softwares/install/cassandra/bin

./cassandra -R

[root@cassandra bin]# ./cassandra -R

[root@cassandra bin]# OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release.

CompileCommand: dontinline org/apache/cassandra/db/Columns$Serializer.deserializeLargeSubset(Lorg/apache/cassandra/io/util/DataInputPlus;Lorg/apache/cassandra/db/Columns;I)Lorg/apache/cassandra/db/Columns;

CompileCommand: dontinline org/apache/cassandra/db/Columns$Serializer.serializeLargeSubset(Ljava/util/Collection;ILorg/apache/cassandra/db/Columns;ILorg/apache/cassandra/io/util/DataOutputPlus;)V

CompileCommand: dontinline org/apache/cassandra/db/Columns$Serializer.serializeLargeSubsetSize(Ljava/util/Collection;ILorg/apache/cassandra/db/Columns;I)I

输入命令来查看正在运行的cassandra的 pid

ps -ef|grep cassandra

假设显示的pid 是 2000

3.4关闭Cassandra

上面已经查到了 pid,下面使用命令杀掉这个pid对应的进程:

kill -9  2000

3.5、查看状态

运行bin 目录下的 nodetool

[root@localhost bin]# ./nodetool status

如果cassandra启动出错,可以在bin目录下 使用 journalctl -u cassandra 命令查看

[root@localhost bin]# journalctl -u cassandra

# 问题

[root@cassandra bin]# ./nodetool status

nodetool: Failed to connect to '127.0.0.1:7199' - URISyntaxException: 'Malformed IPv6 address at index 7: rmi://[127.0.0.1]:7199'.

# 解决办法

[root@cassandra bin]# ./nodetool -Dcom.sun.jndi.rmiURLParsing=legacy status

[root@cassandra bin]# ./nodetool -h ::FFFF:127.0.0.1 status

3.6、客户端连接服务器

进入Cassandra的 bin 目录,输入

./cqlsh 192.168.202.156 9042

[root@cassandra bin]# ./cqlsh 192.168.202.156 9042

Python 2.7 support is deprecated. Install Python 3.6+ or set CQLSH_NO_WARN_PY2 to suppress this message.

Connected to Test Cluster at 192.168.77.21:9042

[cqlsh 6.0.0 | Cassandra 4.0.1 | CQL spec 3.4.5 | Native protocol v5]

Use HELP for help.

cqlsh>

3.7、服务运行脚本

为了方便管理,可以编写脚本来管理,在 /usr/local/apache-cassandra-4.0.1 下创建一个 startme.sh,输入一下内容:

#!/bin/sh

CASSANDRA_DIR="/usr/local/apache-cassandra-4.0.1"

 echo "************cassandra***************"

case "$1" in

        start)

                echo "*                                  *"

                echo "*            starting              *"

                nohup $CASSANDRA_DIR/bin/cassandra -R >> $CASSANDRA_DIR/logs/system.log 2>&1 &

                echo "*            started               *"

                echo "*                                  *"

                echo "************************************"

                ;;

        stop)

                echo "*                                  *"

                echo "*           stopping               *"

                PID_COUNT=`ps aux |grep CassandraDaemon |grep -v grep | wc -l`

                PID=`ps aux |grep CassandraDaemon |grep -v grep | awk {'print $2'}`

                if [ $PID_COUNT -gt 0 ];then

                        echo "*           try stop               *"

                        kill -9 $PID

                        echo "*          kill  SUCCESS!          *"

                else

                        echo "*          there is no !           *"

                echo "*                                  *"

                echo "************************************"

                fi

                ;;

        restart)

                echo "*                                  *"

                echo "*********     restarting      ******"

                $0 stop

                $0 start

                echo "*                                  *"

                echo "************************************"

                ;;

        status)

                $CASSANDRA_DIR/bin/nodetool status

                ;;

        *)

        echo "Usage:$0 {start|stop|restart|status}"

        exit 1

esac

接下来就可以使用这个脚本进行 启动,重启,关闭 的操作

[root@cassandra apache-cassandra-4.0.1]# ./startme.sh start

[root@cassandra apache-cassandra-4.0.1]# ./startme.sh restart

[root@cassandra apache-cassandra-4.0.1]# ./startme.sh stop

# chmod +x startme.sh

[root@cassandra apache-cassandra-4.0.1]# ./startme.sh start

************cassandra***************

*                                  *

*            starting              *

*            started               *

*                                  *

************************************

[root@cassandra apache-cassandra-4.0.1]# ./startme.sh restart

************cassandra***************

*                                  *

*********     restarting      ******

************cassandra***************

*                                  *

*           stopping               *

*           try stop               *

*          kill  SUCCESS!          *

************cassandra***************

*                                  *

*            starting              *

*            started               *

*                                  *

************************************

*                                  *

************************************

[root@cassandra apache-cassandra-4.0.1]# ./startme.sh stop

************cassandra***************

*                                  *

*           stopping               *

*           try stop               *

*          kill  SUCCESS!          *

4、cqlsh.bat登录数据库

在开一个命令提示符窗口,执行cqlsh.bat登录数据库

./cqlsh 192.168.202.156 9042

可以使用默认用户cassandra 默认密码cassandra登陆

5、数据库增删改查语法

语法跟mysql几乎一样

下面贴上几个常用的命令:

查询数据库

describe keyspaces;

desc keyspaces;

创建数据库

CREATE KEYSPACE IF NOT EXISTS renhaoCasDB WITH REPLICATION = {‘class’: ‘SimpleStrategy’,‘replication_factor’:1};

删除数据库

drop keyspace <数据库名称>

进入指定数据库 (这个后面的分号一定要加上,不然进不去)

use renhaoCasDB;

创建表

create table user(id int, name varchar, primary key(id));

查询表

describe tables;

desc tables;

插入数据(涉及中文的话,要去设置数据库的编码)

INSERT INTO user (id, name) VALUES (1,‘renhao’);

INSERT INTO user (id, name) VALUES (2,‘gaoming’);

INSERT INTO user (id, name) VALUES (3,‘qianchen’);

查询数据

select * from user;

删除数据(删除一定要带上条件,不然会报错)

delete from user where id = 1;

数据备份与迁移

单点备份

1.备份单个keyspace schema

cqlsh -e "DESC KEYSPACE user" > user_schema.cql

2.备份整个database schema

cqlsh -e "DESC SCHEMA" > db_schema.cql

3.导入keyspace schema

在 user_schema.cql所在的目录下打开cqlsh:

source 'user_schema.cql'

4.导入database schema:

在db_schema.cql所在的目录下打开cqlsh:

source 'db_schema.cql'

5.备份单个keyspace数据

bin/nodetool snapshot -t 20171130 user

6.备份所有的keyspace

bin/nodetool snapshot -t 20171130

-t后面指定快照的名字,备份好的数据在这个目录下

$CASSANDRA_HOME/data/yourkeyspace/table-uuid/snapshots/20171130

7.使用脚本将快照拷贝出来

将如下脚本保存为copy.sh,注意修改keyspacePath和dstKeyspacePath的值。 这个脚本只拷贝某一个keyspace的快照

#!/bin/bashkeyspacePath="cassandra-3.11.1/data/data/your_keysapce_name"currentIp=`ip addr | grep 'state UP' -A2 | tail -n1 | awk '{print $2}' | cut -f1 -d'/'`dstKeyspacePath="/home/master/beifen/$currentIp/my_backup"echo $currentIpif [ ! -x "$dstKeyspacePath" ]; then mkdir -p $dstKeyspacePathfi for dir in `ls $keyspacePath`; do ( currentTableName=`basename $dir` echo "currentTableName=$currentTableName" tableDstPath="$dstKeyspacePath/$currentTableName" echo "tableDstPath=$tableDstPath" if [ ! -x "$tableDstPath" ]; then mkdir -p $tableDstPath fi cp -r "$keyspacePath/$dir/snapshots/" $tableDstPath); done

给脚本赋予执行权限:

chmod +x ./copy.sh

执行脚本:

./copy.sh

8.恢复快照

将dstKeyspacePath下的SSTable(table-uuid)目录拷贝到新的cassandra的Data目录下,执行如下命令即可:bin/nodetool refresh

集群备份

直接从一个集群导到另一个集群:

1.memTable数据刷新到SSTable

bin/nodetool flush

2.源集群导出schema

cqlsh 192.168.40.x -e "DESC KEYSPACE your_keyspace" > your_keyspace_schema.cql

将your_keyspace_schema.cql 拷贝到目录集群某个目录下,然后进入到这个目录,打开cqlsh。

source 'your_keyspace_schema.cql'

3.使用脚本直接导数据

该脚本只能导一个keyspace

将如下脚本保存为 migrate.sh,注意修改keyspacePath和dstClusterIP。keyspacePath是源集群的keyspace所在目录,dstClusterIP是目标集群的IP,可以是多个。

#!/bin/bashkeyspacePath="cassandra-3.11.1/data/data/your_keyspace"dstClusterIP=192.168.40.1,192.168.40.2,192.168.40.3for dir in `ls $keyspacePath`; do ( sstableloader -d $dstClusterIP -t 100 "$keyspacePath/$dir"); done

脚本赋上执行权限:

chmod +x ./migrate.sh

执行脚本,(请确保你的PATH环境变量里配置过Cassandra的路径)

./migrate.sh

如果要备份所有的数据:

#!/bin/bashsrcDataPath="/data/janus/apache-cassandra-3.11.1/data/data"# 可以是多个IP,逗号分隔dstClusterIP=192.168.40.13 for keyspacePath in `ls $srcDataPath`; do ( echo "currentkeyspace=$keyspacePath" for table in `ls $srcDataPath/$keyspacePath`; do ( echo "$srcDataPath/$keyspacePath/$table" sstableloader -d $dstClusterIP -t 100 "$srcDataPath/$keyspacePath/$table" );done);done

dstClusterIP 后面指定的是目标集群的IP,可以是多个IP。根据你的实际情况修改之。

srcDataPath 是源集群的数据目录。根据你的实际情况修改之。

执行脚本的时候,控制台会打印出当前备份的进度。

5、数据库特点

Apache Cassandra的其他特点

数据备份

数据库里的数据会自动备份,并存在多个不同的节点(电脑)上,当一个电脑炸了,依然可以从别的电脑读取出来。

高性能

简单来说, 就。。很快。 尤其是输入的时候。

去中心化

没有主节点,从任何节点读写都一样的。

可扩展

硬盘不够用了?可以随时往Cassandra里加入新的电脑。 (比如,图1中可以随时多加几个朋友的电脑,分享他们的硬盘的同时,共享你们的小电影)

什么是列数据库

列数据是按照表格中每一列进行存储的数据库,这样的结构设计极大的优化了数据库在对列数据的读写效率。下面的表格给出一个列数据库的例子。

为什么要使用这个数据库?mysql不香了?

一个字,快!

说仔细点?就是cassandra是非关系型数据库,基于列检索,而mysql是基于行检索的全表扫描

6、列数据库行数据库

什么是列数据库

列数据是按照表格中每一列进行存储的数据库,这样的结构设计极大的优化了数据库在对列数据的读写效率。下面的表格给出一个列数据库的例子。

在列数据库中,该表格中的数据将会被按以下格式存储:

李云龙:1, 赵刚:2,丁伟:3,孔捷:4,张大彪:5,魏大勇:6

独立团:1, 独立团:2,新一团:3,新二团:4,独立团:5,独立团:6

团长:1, 团长:2,团长:3,团长:4,营长:5,警卫员:6

赵大政委:2,孔二愣子:4,和尚:6

优化范围搜索

可以看到,每一列的值都被映射到了rowid上,使其对于范围搜索变的非常高效,比如,我要寻找所有属于‘独立团’的人物, 只需要检索‘部队番号’这一列,如果值为独立团,则返回相应的rowid,然后在人物这一列,找到对应的人物名字。

如果对于行数据库,则需要遍历每一行,即遍历整个表格。

优化存储

1 不用存储‘空’值,比如‘外号’这一列,只存了赵大政委,孔二愣子,和尚。而对于行数据库,则需要在‘空’值处标记‘NULL’。

2 对连续相同的值,便于压缩,比如对于‘部队番号’这一列,可以压缩为

独立团2:1,新一团:3,新二团:4,独立团2:5

即,对于第一,二行的值,都是独立团。

什么是行数据库

为了比较一下区别,我们也简单看一下行数据库的特点。

在行数据库中 (比如大家熟悉的关系性数据库MySQL), 数据会被按行来存储,比如,改表格中的数据将会被按一下格式存储:

1: 李云龙,独立团, 团长, NULL

2:赵刚,独立团, 团长, 赵大政委

3:丁伟, 新一团,团长, NULL

4:孔捷,新二团,团长,孔二愣子

5:张大彪,独立团,营长,NULL

6:魏大勇,独立团,警卫员,和尚

其中,rowid是一个系统内置的key,通过它我们可以找到每一行的数据。这样的设计使得行数据库对于按行搜索非常的高效,比如在上表中,我想找李云龙的所有信息,数据库将会利用rowid = 1 直接返回第一行的所有内容。

然而,对与大范围的搜索,行数据则变得不那么高效。在上面的例子中,如果我要寻找所有属于独立团的人物,则需要遍历整个数据库进行检索。

(当然,由于类似的范围搜索在实际使用中非常常见,大部分行数据库也有自己的解决方法, 比如在原表格的基础上另建一个些索引,比如对于‘部队番号’这一列,

独立团:1,独立团:2 ,独立团:5,独立团:6,新二团:4,新二团:3

注意在这个索引中,‘部队番号’按拼音首字母排了序,优化了之后的搜索。)

7、windows中使用注意事项

Cassandra使用的Java文件I/O库在Windows上一直存在问题,尤其是在NTFS上。对于用户来说,这一直是一个问题的来源,我们认为继续支持项目是不好的(参见discussion in this thread)。

我们最终在Cassandra 4.0(CASSANDRA-16171)中完全放弃了Windows支持。

此外,Cassandra 3.1x(及更早版本)仅适用于Java 8(请参见Cassandra installation prerequisites)。Cassandra 4.0.2最近才添加了对Java 11的完全支持(以前是实验性的)(CASSANDRA-16894)。这意味着C*4.x可以与Java8或Java11一起工作。

建议Windows用户的解决方法是:

部署Cassandra in Docker

使用VirtualBox等软件在VM中部署Cassandra

使用K8ssandra.io在Kubernetes集群中部署Cassandra

否则,如果你只想学习如何在Cassandra上构建应用程序,Astra DB有一个免费层,你只需点击5次即可启动集群。

8、配置的各种address分类

在Cassandra的配置文件里面存在以下的address:

listen_address

broadcast_address

listen_on_broadcast_address

rpc_address

broadcast_rpc_address

listen_address

默认是localhost。

该配置是为了告诉集群中其他节点如何连接到该节点上。一个集群中该节点的address必须唯一,因为该地址就像一个独一无二的身份id,只有知道该连接,Cassandra集群才可以建立连接

可以设置为空。Cassandra通过InetAddress.getLocalHost()可以从系统获取本地地址。

如果Cassandra找不到正确的地址,你可以指定IP地址或者主机名称。

如果是单节点集群,你可以使用默认配置(localhost)。

永远不要指定0.0.0.0,总是错的。

broadcast_address

默认是被注释掉的。默认值1.2.3.4

广播地址,用来向其他Cassandra节点发送广播

如果设置为空,将采用与listen_address一样的地址

listen_on_broadcast_address

默认是注释掉的。默认值false

当使用多个物理网络接口时,把该项设置为true,监听除了listen_address之外的broadcast_address,从而运行节点可以在两个接口中通信

rpc_address

默认值localhost

Thrift RPC 服务和本地传输服务都会使用该地址,简单的可以理解为client与Cassandra服务端通信的地址

如果不设置该项,将会采用listen_address的设置

该配置可以设置为0.0.0.0,但是如果这样设置了,你必须设置broadcast_rpc_address为其他地址

broadcast_rpc_address

默认注释掉。默认值1.2.3.4

rpc地址广播到驱动和其他节点上。该节点不能被设置为0.0.0.0。

如果该配置设置为空将会默认使用rpc_address的地址。

如果rpc_address设置为0.0.0.0,该配置必须设置一个地址。

各有什么用处

上面基本上就是对配置文件里面的翻译,但是看完之后,还是不清楚到底是干什么用的。下面就简单的介绍一下

Cassandra节点之间的通信采用的是listen_address

Client连接到服务端用的是rpc_address。有两种方式,thrift和NativeTransport(CQL)这两个都是用的rpc_address。接口不一样。如果rpc_address没设置就用listen_address

上面两个是比较常用的,必须要区分清楚,否则会很混乱。其他的我还没有玩透,所以就不说了,用过的朋友可以讲一下。

9、springboot集成cassandra数据库demo

yml配置文件

server:

  port: 8080

  tomcat:

    uri-encoding: UTF-8

  servlet:

    context-path: /cassandra

#  compression:

#    enabled: true

#    mime-types: text/html,text/xml,text/plain,text/css,text/javascript,application/javascript

spring:

  data:

    cassandra:

      keyspace-name: ***

      contact-points: ***.***.***.***

      port: 9042

      username: cassandra

      password: cassandra

      session-name: Test Cluster

      local-datacenter: datacenter1 #默认的数据中心

domain类

package com.csf.cassandra.entity;

import lombok.Data;

import lombok.Getter;

import lombok.Setter;

import org.springframework.data.cassandra.core.mapping.PrimaryKey;

import org.springframework.data.cassandra.core.mapping.Table;

@Getter

@Setter

@Data

@Table("users")//Cassandra的表注解,value指定表名

public class UserEntity {

        @PrimaryKey

        private int userid;

        private String fname;

        private String lname;

}

Service编写

dao我没有写service 根据自己的结构自行调整

package com.csf.cassandra.dao;

import com.csf.cassandra.entity.UserEntity;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.cassandra.core.CassandraTemplate;

import org.springframework.stereotype.Repository;

import java.util.List;

@Repository

public class UserDao {

    @Autowired

    private CassandraTemplate template;

    public List getAll() {

        List<UserEntity> list = template.select("select * from users", UserEntity.class);

        return list;

    }

    public UserEntity insert(UserEntity user){

        return template.insert(user);

    }

}

Controller编写

package com.csf.cassandra.web;

import com.csf.cassandra.dao.UserDao;

import com.csf.cassandra.entity.UserEntity;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController

public class UserController {

    @Autowired

    private UserDao userDao;

    @RequestMapping("/users")

    public  List<UserEntity>  getAll() {

            List<UserEntity> list =userDao.getAll();

            return list;

    }

    @RequestMapping("/add")

    public UserEntity save() {

        UserEntity user= new  UserEntity();

        user.setUserid(4);

        user.setFname("鲸鱼");

        user.setLname("吴");

        return userDao.insert(user);

    }

}

pom依赖

<dependencies>

    <dependency>

<!--这是主要依赖  详细点进去看源码-->

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-data-cassandra</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <dependency>

        <groupId>org.projectlombok</groupId>

        <artifactId>lombok</artifactId>

        <optional>true</optional>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-test</artifactId>

        <scope>test</scope>

        <exclusions>

            <exclusion>

                <groupId>org.junit.vintage</groupId>

                <artifactId>junit-vintage-engine</artifactId>

            </exclusion>

        </exclusions>

    </dependency>

</dependencies>

B、开源中间件

# Cassandra

https://iothub.org.cn/docs/middleware/

https://iothub.org.cn/docs/middleware/cassandra/cassandra-deploy/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值