clickhouse有多种数据的导入导出方式,可以灵活使用,下面对这些方式分别做些介绍,导入导出的写法与格式和格式设置有关。
详情可查看官网,也可以在这里获取数据集
-- 建库建表
CREATE DATABASE git;
CREATE TABLE git.commits
(
hash String,
author LowCardinality(String),
time DateTime,
message String,
files_added UInt32,
files_deleted UInt32,
files_renamed UInt32,
files_modified UInt32,
lines_added UInt32,
lines_deleted UInt32,
hunks_added UInt32,
hunks_removed UInt32,
hunks_changed UInt32
) ENGINE = MergeTree ORDER BY time;
-- 导入数据
INSERT INTO git.commits SELECT *
FROM s3('https://datasets-documentation.s3.amazonaws.com/github/commits/clickhouse/commits.tsv.xz', 'TSV', 'hash String,author LowCardinality(String), time DateTime, message String, files_added UInt32, files_deleted UInt32, files_renamed UInt32, files_modified UInt32, lines_added UInt32, lines_deleted UInt32, hunks_added UInt32, hunks_removed UInt32, hunks_changed UInt32')
0 rows in set. Elapsed: 1.826 sec. Processed 62.78 thousand rows, 8.50 MB (34.39 thousand rows/s., 4.66 MB/s.)
s3的表达式如下
s3(path, [aws_access_key_id, aws_secret_access_key,] [format, [structure, [compression]]])
所以我们可以看出来上面的insert语句中,第一个参数是s3的url,第二个参数是说明文件为TSV文件,第三个参数把表的结构写下来了,这里没有给第四个参数,因为默认检测了扩展名为xz,所以不需要提供
这里可以通过官网获取更详细的内容
# 写法一:
clickhouse-client -q "INSERT INTO git.com FORMAT CSV" < out.csv
# 写法二:
INSERT INTO com
FROM INFILE '/data/tools/out.csv'
FORMAT CSV
# 写法三:
cat out.csv|clickhouse-client -q "INSERT INTO git.com FORMAT CSV"
ddl语句将被跳过
INSERT INTO com
FROM INFILE '/data/tools/dump.sql' FORMAT MySQLDump
ps: 也可以直接通过文件读取数据,但是要注意读取文件的位置要在这个配置里面:user_files_path,不然会没有权限
SELECT *
FROM file('dump.sql', MySQLDump)
LIMIT 5
详细内容可以在官网中查看
INSERT INTO com
FROM INFILE '/data/tools/data.clickhouse' FORMAT Native
INSERT INTO com
FROM INFILE '/data/tools/data.clickhouse' COMPRESSION 'lz4' FORMAT Native
INSERT INTO com
FROM INFILE '/data/tools/data.binary' FORMAT RowBinary
INSERT INTO com
FROM INFILE '/data/tools/data.msgpk' FORMAT MsgPack
ps: 导入之前可以用以下语句查看下文件内容,不过也有同样的限制,文件的位置要在user_files_path配置里
DESCRIBE file('data.clickhouse', Native);
SELECT *
FROM s3('https://datasets-documentation.s3.eu-west-3.amazonaws.com/nyc-taxi/trips_*.gz', 'TabSeparatedWithNames')
LIMIT 10;
SELECT _path, _file, trip_id
FROM s3('https://datasets-documentation.s3.eu-west-3.amazonaws.com/nyc-taxi/trips_0.gz', 'TabSeparatedWithNames')
LIMIT 5;
注意: 我们不需要列出列,因为 TabSeparatedWithNames 格式对第一行中的列名称进行编码。 其他格式(例如 CSV 或 TSV)将为此查询返回自动生成的列,例如 c1、c2、c3 等。
查询还支持虚拟列 _path 和 _file,它们分别提供有关存储桶路径和文件名的信息。
# 导出数据到s3的单个文件中
INSERT INTO FUNCTION
s3(
'https://datasets-documentation.s3.eu-west-3.amazonaws.com/csv/trips.csv.lz4',
's3_key',
's3_secret',
'CSV'
)
SELECT *
FROM trips
LIMIT 10000;
# 导出数据到s3的多个文件中
INSERT INTO FUNCTION
s3(
'https://datasets-documentation.s3.eu-west-3.amazonaws.com/csv/trips_{_partition_id}.csv.lz4',
's3_key',
's3_secret',
'CSV'
)
PARTITION BY rand() % 10
SELECT *
FROM trips
LIMIT 100000;
ps: clickhouse还有更多和s3联动的高级方式,这里先不做说明,后续讲解
SELECT *
FROM com
INTO OUTFILE '/data/tools/out.csv'
FORMAT CSVWithNames
涉及到的相关参数:
select * from commits limit 100 into outfile '/data/tools/dump.sql' truncate format SQLInsert;
SET output_format_sql_insert_table_name = 'out_table';
select * from table1 into outfile '/data/tools/dump.sql' [append|truncate] format SQLInsert;
# 可以看到/data/tools/dump.sql的文件内容如下,sql的名字变成了out_table
INSERT INTO out_table (`id`, `column1`) VALUES (1, 'abc');
SELECT * FROM com
INTO OUTFILE '/data/tools/data.clickhouse' FORMAT Native
SELECT * FROM com
INTO OUTFILE '/data/tools/data.clickhouse' COMPRESSION 'lz4' FORMAT Native
SELECT * FROM com
INTO OUTFILE '/data/tools/data.binary' FORMAT RowBinary
# 这里必须要用limit1,多导出的话文件格式就不对了,jpg图片无法正常打开
SELECT * FROM com limit 1
INTO OUTFILE '/data/tools/data.jpg'FORMAT RawBLOB
SELECT * FROM com
INTO OUTFILE '/data/tools/data.msgpk'FORMAT MsgPack