1. 程式人生 > 實用技巧 >clickhouse客戶端使用

clickhouse客戶端使用

測試初始化

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>:<data type>}

name:佔位識別符號。通過clickhouse-client引數指定,格式為--param_<name> = 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

<tcp_port>9300</tcp_port>

重啟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
    -->
    <port>9300</port>

這樣就可以像以前一樣,不用每次都輸入埠;註釋中給了五個可設定的薦,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.