Clickhouse 的interface(访问接口)

clickhouse部署完毕之后有哪些访问接口呢?
clickhouse的底层访问接口支持TCP和HTTP两种协议。其中TCP协议拥有更好的性能,默认是9000端口,主要用于集群间的内部通信和CLI客户端。
HTTP协议则拥有更好的兼容性 可以通过REST服务的形式而被广泛用于java Python Golang等编程语言的客户端,默认的端口为8123.通常推荐使用CLI和JDBC这些封装接口,他们更加简单易用。

命令行CLI
底层是基于TCP接口进行的通信,是通过clickhouse-client脚本运行的。
1.交互模式:


 广泛应用于开发 测试 调试 运维等场景。
$ clickhouse-client
hadoop101 :) select bar(number,0,4) from numbers(4);

SELECT bar(number, 0, 4)
FROM numbers(4)

┌─bar(number, 0, 4)────────────────────────────────────────────┐
│                                                              │
│ ████████████████████                                         │
│ ████████████████████████████████████████                     │
│ ████████████████████████████████████████████████████████████ │
└──────────────────────────────────────────────────────────────┘

4 rows in set. Elapsed: 0.003 sec. 

通过交互式查询的语句会默认被记录到~/.clickhouse-client-history文件中,该记录可以作为审计之用。

# cat /root/.clickhouse-client-history 
### 2020-04-18 15:29:19.419
select now();
### 2020-04-18 16:40:39.688
create database datasets;
### 2020-06-09 00:05:19.393
show databases;
### 2020-06-09 00:05:29.002
exit;
### 2020-06-09 00:25:07.593
select version();
### 2020-06-09 00:40:44.278
select bar(number,0,4) from numbers(4);
### 2020-06-09 00:42:18.468
exit

可以看到有时间和执行的命令。

2.非交互式:
非交互模式主要适用于批处理场景。如数据的导入导出等操作。
导入:
# cat t.tsv| clickhouse-client --query "insert into t from tsv"
导出:
#clickhouse-client --query "select * from t" > t.tsv

默认情况下clickhouse-client 一次只能运行一条SQL语句若需要查询则需要在循环中重复执行,显示不是高效的操作。
此时可以利用参数 --multiquery 则可以支持一次运行多条SQL查询,多条SQL语句之间使用分号间隔:

[root@hadoop101 ~]# clickhouse-client -h 192.168.8.101 --multiquery --query="select 10;select 20;select 30;"  
10
20
30

clickhouse-client的常用参数:
Main options:
  --help                                                           produce help message
  -C [ --config-file ] arg                                         config-file path
  -c [ --config ] arg                                              config-file path (another shorthand)
  -h [ --host ] arg (=localhost)                                   server host
  --port arg (=9000)                                               server port
  -s [ --secure ]                                                  Use TLS connection
  -u [ --user ] arg (=default)                                     user
  --password arg                                                   password
  --ask-password                                                   ask-password
  --query_id arg                                                   query_id
  -q [ --query ] arg                                               query
  -d [ --database ] arg                                            database
  --pager arg                                                      pager
  -A [ --disable_suggestion ]                                      Disable loading suggestion data. Note that suggestion data is loaded asynchronously through a second connection to ClickHouse server. Also it is reasonable to 
                                                                   disable suggestion if you want to paste a query with TAB characters. Shorthand option -A is for those who get used to mysql client.
  --suggestion_limit arg (=10000)                                  Suggestion limit for how many databases, tables and columns to fetch.
  -m [ --multiline ]                                               multiline
  -n [ --multiquery ]                                              multiquery
  -f [ --format ] arg                                              default output format
  -T [ --testmode ]                                                enable test hints in comments
  --ignore-error                                                   do not stop processing in multiquery mode
  -E [ --vertical ]                                                vertical output format, same as --format=Vertical or FORMAT Vertical or \G at end of command
  -t [ --time ]                                                    print query execution time to stderr in non-interactive mode (for benchmarks)
  --stacktrace                                                     print stack traces of exceptions
  --progress                                                       print progress even in non-interactive mode
  -V [ --version ]                                                 print version information and exit
  --version-clean                                                  print version in machine-readable format and exit
  --echo                                                           in batch mode, print query before execution


# clickhouse-client  --host 192.168.8.101 --port 9000 --user default --password --database default --query "select version();select now();" --multiquery --time
Password for user (default): 
20.4.4.18
0.003
2020-06-09 01:00:17
0.003
host默认值为localhost
port默认为9000
user 默认为default
password默认为空
 简写版本:
# clickhouse-client  -h 192.168.8.101 --port 9000 -u default --password -d default -q "select version();select now();" -n -t
Password for user (default): 
20.4.4.18
0.002
2020-06-09 01:02:59
0.003


支持http协议的web方式:
curl命令或者echo命令

驱动程序:
JDBC
clickhouse支持标准的JDBC协议,底层基于HTTP接口通信.使用maven依赖即可为java程序引入官方提供的数据库驱动:
<!-- https://mvnrepository.com/artifact/ru.yandex.clickhouse/clickhouse-jdbc -->
<dependency>
    <groupId>ru.yandex.clickhouse</groupId>
    <artifactId>clickhouse-jdbc</artifactId>
    <version>0.2.4</version>
</dependency>



ODBC

client:支持JDBC或者ODBC的驱动程序的客户端
Tabix
HouseOps
LightHouse

DataGrip 
DBeaver 

和其他编程语言的交互:
如python、java、golang等

如大数据组件:
zeppelin、superset等都支持clickhouse


参考:
https://clickhouse.yandex/docs/en/interfaces/third-party/integrations/

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值