linux怎么跨节点访问数据,Apache Cassandra多节点跨数据中心集群配置以及日常操作...

Cassandra是去中心化的集群架构,没有传统集群的中心节点,各个节点地位都是平等的,通过Gossip协议维持集群中的节点信息。为了使集群中的各节点在启动时能发现其他节点,需要指定种子节点(seeds),各节点都先和种子节点通信,通过种子节点获取其他节点列表,然后和其他节点通信。种子节点可以指定多个,通过在 conf/ cassandra.yaml中的seeds属性配置。

环境介绍

主机信息如下表所示:

602ee564f311745e3e3826855fed64cb.png

所有节点已安装了jdk 8。如下:

[root@db03 ~]# Java -version

java version "1.8.0_212"

Java(TM) SE Runtime Environment (build 1.8.0_212-b10)

Java HotSpot(TM) 64-Bit Server VM (build 25.212-b10, mixed mode)

安装cassandra

这里使用的是二进制rpm包进行安装。在各个节点创建yum仓库,内容如下:

[root@db03 ~]# vi /etc/yum.repos.d/cass.repo

[cassandra]

name=Apache Cassandra

baseurl=https://www.apache.org/dist/cassandra/RedHat/311x/

gpgcheck=1

repo_gpgcheck=1

gpgkey=https://www.apache.org/dist/cassandra/KEYS

然后通过yum命令在各个节点进行安装:

[root@db03 ~]# yum -y install cassandra

编辑cassandra配置文件

更改各个节点的配置文件内容如下:

[root@db03 ~]# vi /etc/cassandra/default.conf/cassandra.yaml

cluster_name: 'TCS01'

num_tokens: 256

seed_provider:

- class_name: org.apache.cassandra.locator.SimpleSeedProvider

parameters:

- seeds: "192.168.120.83,192.168.120.85"

listen_address:192.168.120.83

endpoint_snitch: GossipingPropertyFileSnitch

start_rpc: true

rpc_address: 192.168.120.83

其中,db04、db05以及db06需要更改listen_address以及rpc_address,将其设置为本机的IP,其他参数保持和db03一致。

endpoint_snitch 对于跨数据中心的集群,此参数的值必须为GossipingPropertyFileSnitch;如果为SimpleSnitch,所有节点都会加入一个数据中心。

配置节点的datacenter名称

编辑cassandra-rackdc.properties文件,设置dc参数,如下:

[root@db03 ~]# vi /etc/cassandra/default.conf/cassandra-rackdc.properties

dc=dc1

rack=rack1

根据之前的规划,db03和db04属于dc1;db05和db06属于dc2。

启动cassandra服务

先启动种子节点,再启动其他支节点。

启动种子节点

[root@db03 ~]# systemctl enable cassandra

[root@db03 ~]# systemctl start cassandra

[root@db05 ~]# systemctl enable cassandra

[root@db05 ~]# systemctl start cassandra

启动支节点

[root@db04 ~]# systemctl enable cassandra

[root@db04 ~]# systemctl start cassandra

[root@db06 ~]# systemctl enable cassandra

[root@db06 ~]# systemctl start cassandra

验证节点状态信息

cassandra提供了nodetool命令,可以查看集群节点的状态信息,如下:

[root@db03 ~]# nodetool status

831170b26271c8cee9dc832aff9c5ada.png

管理keyspace

键空间(Keyspace)是用于保存列族,用户定义类型的对象。 键空间(Keyspace)就像RDBMS中的数据库,其中包含列族,索引,用户定义类型,数据中心意识,键空间(Keyspace)中使用的策略,复制因子等。

查看系统中默认存在的keyspace:

[root@db03 ~]# cqlsh 192.168.120.83

Connected to TCS01 at 192.168.120.83:9042.

[cqlsh 5.0.1 | Cassandra 3.11.4 | CQL spec 3.4.4 | Native protocol v4]

Use HELP for help.

cqlsh> desc keyspaces;

system_traces system_schema system_auth system system_distributed

创建keyspace:

cqlsh> CREATE KEYSPACE spacewalk WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 4};

cqlsh> desc keyspaces;

system_schema system_auth spacewalk system system_distributed system_traces

cqlsh>

要删除自定义的keyspace,使用下面的命令:

cqlsh> drop keyspace spacewalk;

管理表

在spacewalk键空间上创建表以及导入数据:

创建表

9aa7b25db74ea24849f338d9798c2b69.png

cqlsh:spacewalk> desc tables;

rhnpackagecapability

导入数据

cqlsh:spacewalk> copy rhnpackagecapability(id,name,version,created,modified) from '/tmp/d.csv' with delimiter=',' and header=false;

b9d85ec3208415f4f50491ee6b35a500.png

删除表

cqlsh:spacewalk> drop table rhnpackagecapability;

问题总结

在导入数据过程中,会遇到各种各样的报错,下面是我遇到的两种问题:

错误处理1(大于字段限制)

:1:Failed to import 5000 rows: Error - field larger than field limit (131072), given up after 1 attempts

创建cqlshrc文件:

[root@db03 ~]# cp /etc/cassandra/default.conf/cqlshrc.example ~/.cassandra/cqlshrc

[root@db03 ~]# vi ~/.cassandra/cqlshrc

[csv]

--加大filed_size_limit的值,默认为131072

field_size_limit = 13107200000

错误处理2

Failed to import 20 rows: InvalidRequest - Error from server: code=2200 [Invalid query] message="Batch too large", will retry later, attempt 1 of 5

编辑cassandra.yaml文件,加大batch_size_fail_threshold_in_kb参数值,比如5120。然后在copy后面加maxbatchsize=1 and minbatchsize=1,如下:

cqlsh> copy mykeysp01.rhnpackagerepodata(id,primary_xml,filelist,other,created,modified) from '/u02/tmp/rhnpackagerepodata.csv' with maxbatchsize=1 and minbatchsize=1;

e42c4c10c4743bde1320fd58ecf5903e.png

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值