通过Shell 脚本向CK集群群分发SQL指令

参考链接:ClickHouse官方文档

存在节点目录:CK集群某个节点下的   比如:/user/test_user/  目录

使用方式说明:sycn_ck.sh是一个可执行脚本文件,只执行此脚本且在脚本后面添加上SQL即可;

特色功能说明:

  1. 支持同样的SQL在CK所有节点同步执行;无法单独在指定的节点上执行;
  2. 支持节点执行分行显示;采用颜色高亮分割;
  3. 支持在CK集群显示执行完成进度,以1/6方式显示;

脚本示例:

#节点调用脚本,即每个节点都执行同样的指令
#!/bin/bash
pcount=$#
if((pcount==0));then
        tput setaf 1
        tput bold
        echo no args;
        tput setaf 7
        exit;
fi

params=$@

curr_job=1
total_jobs=6
for ip in host_ip1 host_ip2 host_ip3 host_ip4 host_ip5 host_ip6
do
    echo $(tput setaf 3)"--------[ 在此节点 $ip 开始执行 ]--------"$(tput sgr0)
    clickhouse client -m -n -h $ip --port 9000  -u user_name --password pass_word --query "$params"


    echo $(tput setaf 2; tput bold)"--------[ 在此节点 $ip 完成执行, 完成进度 $curr_job/$total_jobs ]--------"$(tput sgr0)
    curr_job=$[$curr_job+1]
done

2、分发工具运行示例

提示:如果想要查询效果好一点,建议在SQL尾部添加上 format Pretty

-- 查询库
./sycn_ck.sh "show databases;"

-- 查询有哪些表
./sycn_ck.sh "use musicad_superset; show tables;"

-- 查询表
./sycn_ck.sh "use music_ad_income; select fdate, s_date, sum(total_income) as total_income from dws_datatalk_splash_ad_res_site_all where fdate = 20211208 and s_date = 20211208 group by fdate, s_date limit 1"

-- 创建local表
./sycn_ck.sh "use musicad_superset; CREATE TABLE city_local (fdate Int64,city_code Int32,city_name String,total_cnt Int64) ENGINE = ReplicatedMergeTree('/clickhouse/tables/{shard}/city_local', '{replica}')
PARTITION BY fdate ORDER BY (fdate, city_code, city_name) SETTINGS index_granularity = 8192, storage_policy = 'ssd_to_hdd'"

-- 创建all表
./sycn_ck.sh "use musicad_superset; CREATE TABLE IF NOT EXISTS city_all AS city_local ENGINE = Distributed(musicad_ck_cluster, musicad_superset, city_local, rand());"

-- 查询all表数据
./sycn_ck.sh "use musicad_superset; select * from city_all;"

-- 向all表插入数据
./sycn_ck.sh "use musicad_superset; insert into city_all (fdate, city_code, city_name, total_cnt) values (20211208, 100, 'guangzhou', 1000);"

-- 查询all表数据
./sycn_ck.sh "use musicad_superset; select * from city_all;"

-- 删除表
./sycn_ck.sh "use musicad_superset; drop table city_all;"
./sycn_ck.sh "use musicad_superset; drop table city_local;"

tput 命令行使用说明

什么是 tput?

tput 命令将通过 terminfo 数据库对您的终端会话进行初始化和操作。通过使用 tput,您可以更改几项终端功能,如移动或更改光标、更改文本属性,以及清除终端屏幕的特定区域。

什么是 terminfo 数据库?

UNIX 系统上的 terminfo 数据库用于定义终端和打印机的属性及功能,包括各设备(例如,终端和打印机)的行数和列数以及要发送至该设备的文本的属性。UNIX 中的几个常用程序都依赖 terminfo 数据库提供这些属性以及许多其他内容,其中包括 vi 和 emacs 编辑器以及 curses 和 man 程序。

命令行使用说明:

1.文本属性

tput Color Capabilities:

tput setab [0-7] – Set a background color using ANSI escape

tput setb [0-7] – Set a background color

tput setaf [0-7] – Set a foreground color using ANSI escape

tput setf [0-7] – Set a foreground color
Color Code for tput:

0 – Black
1 – Red
2 – Green
3 – Yellow
4 – Blue
5 – Magenta
6 – Cyan
7 – White


tput Text Mode Capabilities:

tput bold – Set bold mode
tput dim – turn on half-bright mode
tput smul – begin underline mode
tput rmul – exit underline mode
tput rev – Turn on reverse mode
tput smso – Enter standout mode (bold on rxvt)
tput rmso – Exit standout mode
tput sgr0 – Turn off all attributes

例子:使输出的字符串有颜色,底色,加粗

#!/bin/bash

printf $(tput setaf 2; tput bold)'color shownn'$(tput sgr0)

for((i=0; i<=7; i++)); do
    echo $(tput setaf $i)"show me the money"$(tput sgr0)
done

printf 'n'$(tput setaf 2; tput setab 0; tput bold)'background color show'$(tput sgr0)'nn'

for((i=0,j=7; i<=7; i++,j--)); do
    echo $(tput setaf $i; tput setab $j; tput bold)"show me the money"$(tput sgr0)
done

exit 0

运行效果截图: 

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇

)">
下一篇>>