跳到主要内容
版本:0.4.x

基本操作

本教程指导用户如何在Docker部署环境下,使用ByConity创建表、导入数据、查询数据等基本操作。

建表

连接数据库

./clickhouse-client -m -h HOST --port PORT

创建数据库

CREATE DATABASE IF NOT EXISTS helloworld;
use helloworld;

创建库表

CREATE TABLE helloworld.my_first_table
(
user_id UInt32,
message String,
timestamp DateTime
)
ENGINE = CnchMergeTree()
PARTITION BY timestamp
ORDER BY (user_id, timestamp);

查看表信息

SHOW TABLES;
DESC my_first_table;
写入数据
INSERT INTO helloworld.my_first_table (user_id, message, timestamp) VALUES
(101, 'Hello, ByConity!', now()),
(102, 'Insert a lot of rows per batch', yesterday()),
(102, 'Sort your data based on your commonly-used queries', today()),
(101, 'Granules are the smallest chunks of data read', now() + 5);

导入数据

根据不同的数据导入需求,ByConity支持了5种不同的导入方式,本地文件导入、HDFS导入、Kafka导入、Spark导入、MySQL导入、Hive外表导入,具体可以参考文档#数据导入#部分。这里以本地文件导入为例简单说明下ByConity如何写入一个数据文件。

写入文件

假设我们有一个数据文件 data.csv

102,This is data in a file,2022-02-22 10:43:28
101,It is comma-separated,2022-02-23 00:00:00
103,Use FORMAT to specify the format,2022-02-21 10:43:30
...
./clickhouse-client -h {HOST} --port {PORT} --query='INSERT INTO helloworld.my_first_table FORMAT CSV' < data.csv

./clickhouse-client -h {HOST} --port {PORT} --query='SELECT * FROM helloworld.my_first_table'

查询数据

ByConity支持通过简单的查询语句

SELECT * FROM helloworld.my_first_table;

SELECT * FROM helloworld.my_first_table ORDER BY timestamp;

SELECT * FROM helloworld.my_first_table ORDER BY timestamp FORMAT JSON;

查看历史查询

https://clickhouse.com/docs/en/operations/system-tables/query_log/

SELECT * FROM system.query_log;