clickhouse 客户端的访问接口

clickhouse 的底层访问接口支持TCP和HTTP两种协议,其中,TCP协议拥有更好的性能,其默认端口为9000,主要用于集群见的内部通信及CLI客户端;而http协议则拥有更好的兼容性。通常不建议用户直接使用底层接口访问clickhouse,更为推荐的方式是通过cli 和jdbc这些封装接口,因为他们更为简单易用。

CLI

CLI基于TCP协议进行通信的,是通过clickhouse-client 脚本运行的,它拥有两种模式。

clickhouse-client --help
 -C --config-file 配置文件目录
 -c --config 配置文件目录
 -h --host 服务器ip地址
 –port 指定端口
 -s use TLS 链接
 -u --user 指定用户名
 –password 指定密码
 –ask-password 询问密码
 -q --query 指定SQL
 -d --database 指定数据库
 -m --multiline 多行组合成一个sql执行。
 -n --multiquery 一次执行多个SQL,SQL之间使用’;'分号隔离
 –queries-file 指定SQL文件(file1 file2)
 -f --format 输出格式
 –ignore-error 在多查询模式下不停止处理
 -t --time 在非交互式格式中将执行时间打印到错误日志中。
 -E --vertical 垂直显示 \G
 -V --version 打印版本信息–format_csv_delimiter arg 指定csv格式的分隔符,分隔符只能是一个字符。
 –format_csv_allow_single_quotes arg(true |flase) 设置为true 则允许输出的列内容使用单引号’'引起来
 –format_csv_allow_double_quotes arg(true|flase) 设置为true 则允许数据的列内容使用双引号“”引起来
 –output_format_csv_crlf_end_of_line arg 如果设置为true, CSV格式的行尾将是\r\n而不是\n。
 –input_format_csv_unquoted_null_literal_as_null arg 将未引用的NULL字面量考虑为\N
 –input_format_csv_enum_as_number arg Treat inserted enum values in CSV formats as enum indices \N
 –input_format_csv_arrays_as_nested_csv arg
 –input_format_skip_unknown_filelds arg 从输入数据中跳过未知名称的列(它适用于JSONEachRow,
 CSVWithNames、TSVWithNames和TSKV格式)。–input_format_with_names_use_header arg 对于TSVWithNames和CSVWithNames输入格式,它控制格式解析器是否为
 方法中指定的列数据在输入中的显示完全相同
 头。(把文件中的第一行当成数据库表中的列名)。–input_format_defaults_for_omitted_fields arg 对于输入数据,计算省略字段的默认表达式(它适用于JSONEachRow、CSV、TSV格式)。当文件中的第一行数据不是列名时使用次参数。
 –input_format_import_nested_json arg 将嵌套的JSON数据映射到嵌套的表(适用于JSONEachRow格式)。–import_format_tsv_empty_as_default 对待tsv格式的文件中的空列,在插入时会插入默认值。
–date_time_input_format arg Method to read DateTime from text input formats. Possible values: ‘basic’ and
 ‘best_effort’.
 –date_time_output_format arg Method to write DateTime to text output. Possible values: ‘simple’, ‘iso’,
 ‘unix_timestamp’.–output_format_tsv_crlf_end_of_line arg If it is set true, end of line in TSV format will be \r\n instead of \n. 指定行分割符
 –output_format_tsv_null_representation arg Custom NULL representation in TSV format–format_schema arg Schema identifier (used by schema-based formats)
 –format_template_resultset arg Path to file which contains format string for result set (for Template format)
 –format_template_row arg Path to file which contains format string for rows (for Template format)
 –format_template_rows_between_delimiter arg Delimiter between rows (for Template format)
 –format_custom_escaping_rule arg Field escaping rule (for CustomSeparated format)
 –format_custom_field_delimiter arg Delimiter between fields (for CustomSeparated format)
 –format_custom_row_before_delimiter arg Delimiter before field of the first column (for CustomSeparated format)
 –format_custom_row_after_delimiter arg Delimiter after field of the last column (for CustomSeparated format)
 –format_custom_row_between_delimiter arg Delimiter between rows (for CustomSeparated format)
 –format_custom_result_before_delimiter arg Prefix before result set (for CustomSeparated format)
 –format_custom_result_after_delimiter arg Suffix after result set (for CustomSeparated format)
 –format_regexp arg Regular expression (for Regexp format)
 –format_regexp_escaping_rule arg Field escaping rule (for Regexp format)
 –format_regexp_skip_unmatched arg Skip lines unmatched by regular expression (for Regexp format
 –output_format_enable_streaming arg Enable streaming in output formats that support it.
 –output_format_write_statistics arg Write statistics about read rows, bytes, time elapsed in suitable output formats.
 –output_format_pretty_row_numbers arg Add row numbers before each row for pretty output format
 –insert_distributed_one_random_shard arg If setting is enabled, inserting into distributed table will choose a random shard to
 write when there is no sharding key
 –cross_to_inner_join_rewrite arg Use inner join instead of comma/cross join if possible
 –output_format_arrow_low_cardinality_as_dictionary arg Enable output LowCardinality type as Dictionary Arrow typeExternal tables options:
 –file arg data file or - for stdin
 –name arg (=_data) name of the table
 –format arg (=TabSeparated) data format
 –structure arg structure
 –types arg typesIn addition, --param_name=value can be specified for substitution of parameters for parametrized queries.
1. 交换式执行
clickhouse -h -u --password -m (-m 选项是可以允许一个SQL语句分成多行已;号结尾。)默认一行一个SQL。否则报错。
2. 非交互式执行
cat test.tsv | clickhouse-client -h 192.168.1.1 -uadmin --password 123.com --query “insert into t1 format tsv”
cat device_center_model_mac_info.csv | clickhouse-client -h 10.20.30.40 -u “data_analysis” --password ‘123.com’ --query=“insert into data_analysis.t_device FORMAT CSVWithNames”
一次执行多个sql
clickhouse-client -h 10.20.30.40 --multiquery --query “select 1;select2;select 3”
导入表数据
 clickhouse-client --query "select * from test_fetch " > .test_fetch.tsv