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.

 

 

 







免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM