clickhouse客户端使用
阅读原文时间:2023年07月09日阅读:1

测试初始化

clickhouse-client -m

create database if not exists test;
use test;
drop table test;
create table test(id UInt8, text String, created DateTime) ENGINE=TinyLog;

shell命令行执行

echo -ne "1, 'some text', '2016-08-14 00:00:00'\n2, 'some more text', '2016-08-14 00:00:01'" | clickhouse-client --database=test --query="INSERT INTO test FORMAT CSV";

cat <<_EOF | clickhouse-client --database=test --query="INSERT INTO test FORMAT CSV";
3, 'some text', '2016-08-14 00:00:00'
4, 'some more text', '2016-08-14 00:00:01'
_EOF

[root@ch2 /]# clickhouse-client --query="select * from test.test " > /tmp/test.tsv
[root@ch2 /]# cat /tmp/test.tsv
1 some text 2016-08-14 00:00:00
2 some more text 2016-08-14 00:00:01
3 some text 2016-08-14 00:00:00
4 some more text 2016-08-14 00:00:01

--multiquery 除了 insert

[root@ch2 /]# clickhouse-client --multiquery --query="select * from test.test limit 1;select * from test.test limit 1; "
1 some text 2016-08-14 00:00:00
1 some text 2016-08-14 00:00:00

批模式下的默认数据格式为Tab空格分隔,可使用FORMAT指定格式

使用--multiline或-m参数,允许执行多行的查询

退出客户端的方式,按Ctrl + D 或 Ctrl + C 或 q; quit;  exit;

指定参数,格式为: {:}

name:占位标识符。通过clickhouse-client参数指定,格式为--param_ = value 。
data type:指定参数值的数据类型,例如:UInt8、String等。

clickhouse-client --param_myid=1 --database=test --query="select * from test where id>{myid:UInt8}"

clickhouse-client --param_parName="[1, 2]" -q "SELECT * FROM table WHERE a = {parName:Array(UInt16)}"

clickhouse-client --query="select * from test.test FORMAT TabSeparated" > file.tsv

指定数据库

clickhouse-client --database=test --query="select * from test where id>1"

配置文件

clickhouse-client查找配置文件的顺序:
1)通过--config-file指定的配置文件。
2)./clickhouse-client.xml
3)~/.clickhouse-client/config.xml
4)/etc/clickhouse-client/config.xml

# ll /etc/clickhouse-client/
total 4
drwxr-xr-x 2 root root 6 Jul 31 09:46 conf.d
-rw-r--r-- 1 root root 1568 May 18 12:26 config.xml

修改默认配置端口

不指定端口时,默认有以下端口

# netstat -tunlp|grep clickhouse
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 340/clickhouse-serv
tcp 0 0 127.0.0.1:9004 0.0.0.0:* LISTEN 340/clickhouse-serv
tcp 0 0 127.0.0.1:9009 0.0.0.0:* LISTEN 340/clickhouse-serv
tcp 0 0 127.0.0.1:8123 0.0.0.0:* LISTEN 340/clickhouse-serv

vim /etc/clickhouse-server/config.xml

9300

重启clickhouse

systemctl restart clickhouse-server.service

再次连接需要指定--port

[root@ch2 /]# clickhouse-client --port 9300
ClickHouse client version 20.3.9.70 (official build).
Connecting to localhost:9300 as user default.
Connected to ClickHouse server version 20.3.9 revision 54433.

ch2 :) q;
Bye.
[root@ch2 /]#

设置默认的客户端连接端口

vim /etc/clickhouse-client/config.xml

<!--  
    It's a custom prompt settings for the clickhouse-client  
    Possible macros:  
        {host}  
        {port}  
        {user}  
        {database}  
        {display\_name}  
    Terminal colors: https://misc.flogisoft.com/bash/tip\_colors\_and\_formattii  

ng
See also: https://wiki.hackzine.org/development/misc/readline-color-promm
pt.html
-->
9300

这样就可以像以前一样,不用每次都输入端口;注释中给了五个可设置的荐,host/port/user/database… 都可以设置默认值,不需要每次都输入

[root@ch2 /]# clickhouse-client
ClickHouse client version 20.3.9.70 (official build).
Connecting to localhost:9300 as user default.
Connected to ClickHouse server version 20.3.9 revision 54433.

ch2 :)
ch2 :)
ch2 :) q;
Bye.

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器