最近更新时间:2023.05.29 10:13:50
首次发布时间:2022.12.19 15:10:59
当仅希望测试导入,且导入文件不大时,可以使用 clickhouse-client 进行直接的文件导入。相比起上一章节的“从对象存储导入”,对象存储导入方式因其需要调度 Spark 资源而会比较慢(即便几 kb 的文件也需要分钟级导入),而直接通过 Insert into
导入会很快。
不过,在参考此示例前,有以下注意点:
文件导入注意点:
每次 Insert into 都会占用 ByteHouse 集群的 CPU 资源,会抢占正在进行的查询;而上一章所示的对象存储离线导入功能则采用旁路写入,使用 Spark 集群的 CPU 资源,因此不会发生抢占。
示例直接插入到了 Distributed 表,在集群 > 1 个分片的情况下,这种方式性能较差。一般建议拆分数据后分别插入不同节点的 local 表(即示例中的 HaMergeTree 表)。
基于上述两点,这种方式仅适合于导入测试,不适合于实际生产。
导入步骤如下:
$ cat /test.csv 1,2,3 3,2,1 78,43,45
create table test.test_local on cluster default_cluster ( `column1` UInt32, `column2` UInt32, `column3` UInt32 ) engine = HaMergeTree('/clickhouse/tables/test/test/{shard}', '{replica}') order by column1;
create table test.test on cluster default AS test.test engine = Distributed('default_cluster', 'test', 'test', rand());
cat data.csv | clickhouse-client --query="INSERT INTO test_local FORMAT CSV"
select * from test;
可直接使用 clickhouse 函数 s3()
。
INSERT INTO test_local SELECT * from s3(path, [aws_access_key_id, aws_secret_access_key,] [format, [structure, [compression]]])
path — Bucket 路径与文件。例如tos-s3-cn-beijing.volces.co``m/test_bucket/test.csv
。
Format — 文件格式,如 CSV。
structure — 表格式,填写示例 'column1_name column1_type, column2_name column2_type, ...'
。
compression — 压缩格式,可选,支持 none
, gzip/gz
, brotli/br
, xz/LZMA
, zstd/zst
。
可直接使用 clickhouse 函数 hdfs()
。
INSERT INTO test_local SELECT * from hdfs(path, format, structure)
path — HDFS 路径与文件。
Format — 文件格式,如 CSV。
structure — 表格式,填写示例 'column1_name column1_type, column2_name column2_type, ...'
。