ClickHouse Quick Start ClickHouse快速入门
This page helps you set up open-source ClickHouse on your own machine. The fastest way to deploy ClickHouse and to get access to our exclusive SQL Console is to use ClickHouse Cloud.
此页面可帮助您在自己的计算机上设置开源 ClickHouse。部署 ClickHouse 并访问我们专有的 SQL 控制台的最快方法是使用 ClickHouse Cloud。
New users get $300 in free trial credits. Click here to sign up.
新用户可获得 300 美元的免费试用积分。点击此处注册。
1: Download the binary
1:下载二进制文件
ClickHouse runs natively on Linux, FreeBSD and macOS, and runs on Windows via the WSL.
The simplest way to download ClickHouse locally is to run the following curl
command. It determines if your operating system is supported,
then downloads an appropriate ClickHouse binary:
ClickHouse 在 Linux、FreeBSD 和 macOS 上本机运行,并通过WSL在 Windows 上运行。在本地下载 ClickHouse 最简单的方法是运行以下curl
命令。它确定您的操作系统是否受支持,然后下载适当的 ClickHouse 二进制文件:
curl https://clickhouse.com/ | sh
2: Start the server
2:启动服务器
Run the following command to start the ClickHouse server:
运行以下命令启动ClickHouse服务器:
./clickhouse server
3: Start the client
3:启动客户端
Use the clickhouse-client
to connect to your ClickHouse service. Open a new Terminal, change directories to where your clickhouse
binary is saved, and run the following command:
使用clickhouse-client
连接到您的 ClickHouse 服务。打开一个新的终端,将目录更改为保存clickhouse
二进制文件的位置,然后运行以下命令:
./clickhouse client
You should see a smiling face as it connects to your service running on localhost:
当它连接到在本地主机上运行的服务时,您应该看到一张笑脸:
my-host :)
4: Create a table
4:创建表
Use CREATE TABLE
to define a new table. Typical SQL DDL commands work in ClickHouse with one addition - tables in ClickHouse require
an ENGINE
clause. Use MergeTree
to take advantage of the performance benefits of ClickHouse:
使用CREATE TABLE
定义一个新表。典型的 SQL DDL 命令在 ClickHouse 中工作,但有一项补充 - ClickHouse 中的表需要ENGINE
子句。使用MergeTree
来利用 ClickHouse 的性能优势:
CREATE TABLE my_first_table
(
user_id UInt32,
message String,
timestamp DateTime,
metric Float32
)
ENGINE = MergeTree
PRIMARY KEY (user_id, timestamp)
5. Insert data
5.插入数据
You can use the familiar INSERT INTO TABLE
command with ClickHouse, but it is important to understand that each insert into a
MergeTree
table causes a part (folder) to be created in storage. To minimize parts, bulk insert lots of rows at a time (tens of
thousands or even millions at once).
您可以在 ClickHouse 中使用熟悉的INSERT INTO TABLE
命令,但重要的是要了解每次插入MergeTree
表都会导致在存储中创建一个部分(文件夹)。为了最大限度地减少部件,请一次批量插入大量行(一次插入数万甚至数百万行)。
INSERT INTO my_first_table (user_id, message, timestamp, metric) VALUES
(101, 'Hello, ClickHouse!', now(), -1.0 ),
(102, 'Insert a lot of rows per batch', yesterday(), 1.41421 ),
(102, 'Sort your data based on your commonly-used queries', today(), 2.718 ),
(101, 'Granules are the smallest chunks of data read', now() + 5, 3.14159 )
6. Query your new table
6. 查询你的新表
You can write a SELECT
query just like you would with any SQL database:
您可以像使用任何 SQL 数据库一样编写SELECT
查询:
SELECT *
FROM my_first_table
ORDER BY timestamp
Notice the response comes back in a nice table format:
请注意,响应以良好的表格格式返回:
┌─user_id─┬─message────────────────────────────────────────────┬───────────timestamp─┬──metric─┐
│ 102 │ Insert a lot of rows per batch │ 2022-03-21 00:00:00 │ 1.41421 │
│ 102 │ Sort your data based on your commonly-used queries │ 2022-03-22 00:00:00 │ 2.718 │
│ 101 │ Hello, ClickHouse! │ 2022-03-22 14:04:09 │ -1 │
│ 101 │ Granules are the smallest chunks of data read │ 2022-03-22 14:04:14 │ 3.14159 │
└─────────┴────────────────────────────────────────────────────┴─────────────────────┴─────────┘
4 rows in set. Elapsed: 0.008 sec.
7: Insert your own data
7:插入您自己的数据
The next step is to get your current data into ClickHouse. We have lots of table functions
and integrations for ingesting data. We have some examples in the tabs below, or check out our Integrations
for a long list of technologies that integrate with ClickHouse.
下一步是将当前数据导入 ClickHouse。我们有很多用于摄取数据的表函数和集成。我们在下面的选项卡中提供了一些示例,或者查看我们的集成以获取与 ClickHouse 集成的一长串技术。
- S3
- GCS 地面站
- Web 网络
- Local 当地的
- PostgreSQL
- MySQL
- ODBC/JDBC
- Message Queues 消息队列
- Data Lakes 数据湖
- Other 其他
Use the s3
table function to read files from S3. It's a table function - meaning that the
result is a table that can be:
使用s3
表函数从 S3 读取文件。它是一个表函数 - 意味着结果是一个表,可以是:
- used as the source of a
SELECT
query (allowing you to run ad-hoc queries and leave your data in S3), or...
用作SELECT
查询的源(允许您运行即席查询并将数据保留在 S3 中),或者... - insert the resulting table into a
MergeTree
table (when you are ready to move your data into ClickHouse)
将结果表插入MergeTree
表(当您准备好将数据移至 ClickHouse 时)
An ad-hoc query looks like:
临时查询如下所示:
SELECT
passenger_count,
avg(toFloat32(total_amount))
FROM s3(
'https://datasets-documentation.s3.eu-west-3.amazonaws.com/nyc-taxi/trips_0.gz',
'TabSeparatedWithNames'
)
GROUP BY passenger_count
ORDER BY passenger_count;
Moving the data into a ClickHouse table looks like the following, where nyc_taxi
is a MergeTree
table:
将数据移动到 ClickHouse 表中如下所示,其中nyc_taxi
是MergeTree
表:
INSERT INTO nyc_taxi
SELECT * FROM s3(
'https://datasets-documentation.s3.eu-west-3.amazonaws.com/nyc-taxi/trips_0.gz',
'TabSeparatedWithNames'
)
SETTINGS input_format_allow_errors_num=25000;
View our collection of AWS S3 documentation pages for lots more details and examples of using S3 with ClickHouse.
查看我们的AWS S3 文档页面集合,了解更多详细信息以及将 S3 与 ClickHouse 结合使用的示例。
The s3
table function used for reading data in AWS S3 also works on files in Google Cloud Storage. For example:
SELECT
*
FROM s3(
'https://storage.googleapis.com/my-bucket/trips.parquet',
'MY_GCS_HMAC_KEY',
'MY_GCS_HMAC_SECRET_KEY',
'Parquet'
)
LIMIT 1000
Find more details on the s3
table function page.
The url
table function reads files accessible from the web:
--By default, ClickHouse prevents redirects to protect from SSRF attacks.
--The URL below requires a redirect, so we must set max_http_get_redirects > 0.
SET max_http_get_redirects=10;
SELECT *
FROM url(
'http://prod2.publicdata.landregistry.gov.uk.s3-website-eu-west-1.amazonaws.com/pp-complete.csv',
'CSV'
);
Find more details on the url
table function page.
Use the file
table engine to read a local file. For simplicity, copy the file to the user_files
directory (which is
found in the directory where you downloaded the ClickHouse binary).
DESCRIBE TABLE file('comments.tsv')
Query id: 8ca9b2f9-65a2-4982-954a-890de710a336
┌─name──────┬─type────────────────────┬─default_type─┬─default_expression─┬─comment─┬─codec_expression─┬─ttl_expression─┐
│ id │ Nullable(Int64) │ │ │ │ │ │
│ type │ Nullable(String) │ │ │ │ │ │
│ author │ Nullable(String) │ │ │ │ │ │
│ timestamp │ Nullable(DateTime64(9)) │ │ │ │ │ │
│ comment │ Nullable(String) │ │ │ │ │ │
│ children │ Array(Nullable(Int64)) │ │ │ │ │ │
└───────────┴─────────────────────────┴──────────────┴────────────────────┴─────────┴──────────────────┴────────────────┘
Notice ClickHouse infers the names and data types of your columns by analyzing a large batch of rows. If ClickHouse can not determine the storage type from the filename, you can specify it as the second argument:
SELECT count()
FROM file(
'comments.tsv',
'TabSeparatedWithNames'
)
View the file
table function docs page for more details.
Use the postgresql
table function to read data from a table in PostgreSQL:
SELECT *
FROM
postgresql(
'localhost:5432',
'my_database',
'my_table',
'postgresql_user',
'password')
;
View the postgresql
table function docs page for more details.
Use the mysql
table function to read data from a table in MySQL:
SELECT *
FROM
mysql(
'localhost:3306',
'my_database',
'my_table',
'postgresql_user',
'password')
;
View the mysql
table function docs page for more details.
ClickHouse can read data from any ODBC or JDBC data source:
SELECT *
FROM
odbc(
'DSN=mysqlconn',
'my_database',
'my_table'
);
View the odbc
table function and the jdbc
table function docs pages for more details.
Message queues can stream data into ClickHouse using the corresponding table engine, including:
- Kafka: integrate with Kafka using the
Kafka
table engine - Amazon MSK: integrate with Amazon Managed Streaming for Apache Kafka (MSK)
- RabbitMQ: integrate with RabbitMQ using the
RabbitMQ
table engine
ClickHouse has table functions to read data from the following sources:
- Hadoop: integrate with Apache Hadoop using the
hdfs
table function - Hudi: read from existing Apache Hudi tables in S3 using the
hudi
table function - Iceberg: read from existing Apache Iceberg tables in S3 using the
iceberg
table function - DeltaLake: read from existing Delta Lake tables in S3 using the
deltaLake
table function
Check out our long list of ClickHouse integrations to find how to connect your existing frameworks and data sources to ClickHouse.
What's Next?
接下来是什么?
- Check out the Advanced Tutorial which takes a much deeper dive into the key concepts and capabilities of ClickHouse
查看高级教程,该教程更深入地探讨了 ClickHouse 的关键概念和功能 - Continue your learning by taking our free on-demand training courses at the ClickHouse Academy
通过参加ClickHouse Academy的免费点播培训课程继续您的学习 - We have a list of example datasets with instructions on how to insert them
我们有一个示例数据集列表,其中包含有关如何插入它们的说明 - If your data is coming from an external source, view our collection of integration guides for connecting to message queues, databases, pipelines and more
如果您的数据来自外部源,请查看我们的集成指南集合,以连接到消息队列、数据库、管道等 - If you are using a UI/BI visualization tool, view the user guides for connecting a UI to ClickHouse
如果您使用的是 UI/BI 可视化工具,请查看有关将 UI 连接到 ClickHouse 的用户指南 - The user guide on primary keys is everything you need to know about primary keys and how to define them
主键用户指南是您需要了解的有关主键以及如何定义主键的所有信息