赞
踩
摘要:
新增了 10 项功能
实现了 12 个性能优化
修复了 31 处缺陷
发布列车继续前进。
我们非常激动地分享了23.6版本中的一系列令人惊叹的功能。
以下是一小部分突出功能。但值得注意的是,一些功能现在已经准备就绪,或者已默认启用。您可以在本文末尾找到这些功能。
如果说在现代Web应用程序堆栈中有一个几乎无处不在的数据存储,那就是MongoDB。MongoDB是一种面向文档的数据库,旨在存储和检索类似JSON的数据。由于其专注于JSON并且易于使用,自2009年诞生以来,Mongo已经证明自己是数据存储的瑞士军刀,也是Web应用程序中常见的状态存储组件。
虽然ClickHouse已经支持使用表函数查询MongoDB已有一段时间了,但是Mongo v5.1引入了协议更改,需要更新此集成。我们很高兴地宣布,现在支持Mongo的最新版本v6。
对于不熟悉ClickHouse表函数的用户来说,这些函数提供了查询外部数据源的能力。这在数据迁移任务中非常有用,其中数据从数据源中提取,然后通过 INSERT INTO SELECT 命令插入到ClickHouse中,还可用于填充ClickHouse中的字典。或者,可以使用等效的表引擎,在ClickHouse中公开由外部存储支持的表,这些表反过来可以与存储在MergeTree表中的数据连接。
Mongo示例数据集提供了一些有用的示例来测试此功能。假设您已将这些数据加载到Mongo或Atlas实例中,那么使用表函数查询这些数据将变得非常简单。在下面的示例中,我们查询了Airbnb列表数据集(出于简洁起见对字段进行了子集抽样),指定了“sample_airbnb”数据库和“listingsAndReviews”集合:
提示:如果查询Mongo Atlas,则用户需要包括如下设置: connectTimeoutMS=10000&ssl=true&authSource=admin 。Atlas的连接端点也不是有效的主机名。可以使用atlas cli命令和命令 atlas process list -o json 来确定数据库的主机名。
- SELECT listing_url, name, summary
- FROM mongodb('<host>:27017', 'sample_airbnb', 'listingsAndReviews', 'default', '<password>', 'listing_url String, name String, summary String, space String, description String, room_type UInt32, bed_type UInt32, minimum_nights UInt32, maximum_nights UInt32, bedrooms UInt16, beds UInt16,number_of_reviews UInt16, amenities Array(String)', 'connectTimeoutMS=10000&ssl=true&authSource=admin')
- LIMIT 1
- FORMAT Vertical
-
- Row 1:
- ──────
- listing_url: https://www.airbnb.com/rooms/10006546
- name: Ribeira Charming Duplex
- summary: Fantastic duplex apartment with three bedrooms, located in the historic area of Porto, Ribeira (Cube) - UNESCO World Heritage Site. Centenary building fully rehabilitated, without losing their original character.
-
- 1 row in set. Elapsed: 0.483 sec.
注意:目前不支持Mongo中的Decimal128字段,这导致我们无法在此处使用价格。这将在未来的版本中得到解决,同时还会进行模式推断。
这个相同的函数可以用于计算Mongo中的聚合,其中过滤器被下推以最小化数据传输。在下面的示例中,我们计算按房间类型和卧室数量计算床的平均数。
- SELECT room_type, bedrooms, round(avg(beds), 2) AS avg_beds
- FROM mongodb('<host>:27017', 'sample_airbnb', 'listingsAndReviews', 'default', '<password>', 'listing_url String, name String, summary String, space String, description String, room_type String, bed_type UInt32, minimum_nights UInt32, maximum_nights UInt32, bedrooms UInt16, beds UInt16,number_of_reviews UInt16, amenities Array(String)', 'connectTimeoutMS=10000&ssl=true&socketTimeoutMS=10000&authSource=admin')
- GROUP BY 1, 2
- ORDER BY room_type ASC, bedrooms ASC
-
- ┌─room_type───────┬─bedrooms─┬─avg_beds─┐
- │ Entire home/apt │ 0 │ 1.34 │
- │ Entire home/apt │ 1 │ 1.53 │
- │ Entire home/apt │ 2 │ 2.81 │
- │ Entire home/apt │ 3 │ 4.13 │
- │ Entire home/apt │ 4 │ 5.56 │
- │ Entire home/apt │ 5 │ 6.26 │
- │ Entire home/apt │ 6 │ 8.14 │
- │ Entire home/apt │ 7 │ 8.5 │
- │ Entire home/apt │ 8 │ 8.67 │
- │ Entire home/apt │ 9 │ 15.5 │
- │ Entire home/apt │ 10 │ 14.5 │
- │ Private room │ 0 │ 1.23 │
- │ Private room │ 1 │ 1.25 │
- │ Private room │ 2 │ 2.45 │
- │ Private room │ 3 │ 2.3 │
- │ Private room │ 4 │ 3.62 │
- │ Private room │ 5 │ 7.5 │
- │ Private room │ 6 │ 7 │
- │ Private room │ 7 │ 1 │
- │ Private room │ 15 │ 2 │
- │ Private room │ 20 │ 25 │
- │ Shared room │ 1 │ 2.53 │
- └─────────────────┴──────────┴──────────┘
-
- 22 rows in set. Elapsed: 0.760 sec. Processed 5.55 thousand rows, 149.26 KB (7.31 thousand rows/s., 196.32 KB/s.)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
如果我们为Mongo实例创建一个表引擎,上述查询可以被简化。这允许我们将Mongo集合暴露为ClickHouse中的表。在下面的示例中,我们使用这种方法将数据集加载到Mergetree表引擎中。
- CREATE TABLE listings_mongo
- (
- `listing_url` String,
- `name` String,
- `summary` String,
- `space` String,
- `description` String,
- `room_type` String,
- `bed_type` UInt32,
- `minimum_nights` UInt32,
- `maximum_nights` UInt32,
- `bedrooms` UInt16,
- `beds` UInt16,
- `number_of_reviews` UInt16,
- `amenities` Array(String)
- )
- ENGINE = MongoDB('<host>:27017', 'sample_airbnb', 'listingsAndReviews', 'default', '<password>', 'connectTimeoutMS=10000&ssl=true&socketTimeoutMS=10000&authSource=admin')
-
- — simpler syntax now possible
- SELECT room_type, bedrooms, round(avg(beds), 2) AS avg_beds
- FROM listings_mongo
- GROUP BY 1, 2
- ORDER BY room_type ASC, bedrooms ASC
-
- — migrate data to ClickHouse
- CREATE TABLE listings_merge
- ENGINE = MergeTree
- ORDER BY listing_url AS
- SELECT *
- FROM listings
-
- Ok.
-
- 0 rows in set. Elapsed: 1.369 sec. Processed 5.55 thousand rows, 11.42 MB (4.06 thousand rows/s., 8.34 MB/s.)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
最后,Mongo数据集通常用作字典的数据源。有关详细信息,请参阅此处。(https://clickhouse.com/docs/en/sql-reference/dictionaries#mongodb)
数据处理中的一个常见问题是需要将值映射为更有意义的值,通常是将代码映射为某些含义。在SQL中,最适合使用转换函数来执行此任务。这个函数在ClickHouse中已经支持了一段时间,用于数字、日期和字符串(前提是源值和目标值是相同类型),甚至可以用它来加载流行的英国房价数据集,如下所示。在这里,我们将房屋类型的代码映射到更可读的值以便在插入时使用。
- CREATE TABLE uk_price_paid
- (
- price UInt32,
- date Date,
- postcode1 LowCardinality(String),
- postcode2 LowCardinality(String),
- type Enum8('terraced' = 1, 'semi-detached' = 2, 'detached' = 3, 'flat' = 4, 'other' = 0),
- is_new UInt8,
- duration Enum8('freehold' = 1, 'leasehold' = 2, 'unknown' = 0),
- addr1 String,
- addr2 String,
- street LowCardinality(String),
- locality LowCardinality(String),
- town LowCardinality(String),
- district LowCardinality(String),
- county LowCardinality(String)
- )
- ENGINE = MergeTree
- ORDER BY (postcode1, postcode2, addr1, addr2);
-
- INSERT INTO uk_price_paid
- WITH
- splitByChar(' ', postcode) AS p
- SELECT
- toUInt32(price_string) AS price,
- parseDateTimeBestEffortUS(time) AS date,
- p[1] AS postcode1,
- p[2] AS postcode2,
- transform(a, ['T', 'S', 'D', 'F', 'O'], ['terraced', 'semi-detached', 'detached', 'flat', 'other']) AS type,
- b = 'Y' AS is_new,
- transform(c, ['F', 'L', 'U'], ['freehold', 'leasehold', 'unknown']) AS duration, addr1, addr2, street, locality, town, district, county
- FROM url(
- 'http://prod.publicdata.landregistry.gov.uk.s3-website-eu-west-1.amazonaws.com/pp-complete.csv', 'CSV','uuid_string String, price_string String, time String, postcode String, a String, b String, c String, addr1 String, addr2 String, street String, locality String, town String, district String, county String, d String, e String'
- ) SETTINGS max_http_get_redirects=10;
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
在23.6版本中,我们已经为所有数据类型添加了对此函数的支持。转换函数现在可以用于将列转换为其他类型。支持的类型包括DateTime64和Decimal,还可以处理Null值。举个简单的例子,对比一下下面的23.5和23.6版本的响应的差异:
- --23.5
- SELECT transform(number, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'], NULL) AS numbers
- FROM system.numbers
- LIMIT 10
-
- ┌─numbers─┐
- │ ᴺᵁᴸᴸ │
- │ ᴺᵁᴸᴸ │
- │ ᴺᵁᴸᴸ │
- │ ᴺᵁᴸᴸ │
- │ ᴺᵁᴸᴸ │
- │ ᴺᵁᴸᴸ │
- │ ᴺᵁᴸᴸ │
- │ ᴺᵁᴸᴸ │
- │ ᴺᵁᴸᴸ │
- │ ᴺᵁᴸᴸ │
- └─────────┘
-
- 10 rows in set. Elapsed: 0.003 sec.
-
- --23.6
- SELECT transform(number, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'], NULL) AS numbers
- FROM system.numbers
- LIMIT 10
-
- ┌─numbers─┐
- │ zero │
- │ one │
- │ two │
- │ three │
- │ four │
- │ five │
- │ six │
- │ seven │
- │ eight │
- │ nine │
- └─────────┘
-
- 10 rows in set. Elapsed: 0.001 sec.
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
ClickHouse热衷于有序数据。作为一种列式数据库,在插入数据时对数据进行排序是查询性能的基础,也是用户在创建表时需要指定 ORDER BY 子句时遇到的早期概念之一。此子句指定了一个列的列表,数据将按照这些列在磁盘上的顺序进行排序,这应该与用户的访问模式保持一致,以确保查询性能最佳。虽然这些排序键是每个ClickHouse用户调优性能的第一个工具,但在此处指定所有列仍然是不切实际的。尽管如此,一些列自然是排序并与这些排序键保持一致的。
在23.6版本中,ClickHouse现在将利用数据中的任何自然排序模式来提高查询性能。这在某些情况下特别有效,例如某一列在大多数情况下都是单调递增的,但不是排序键的一部分。
特别欢迎所有新加入23.6版本的贡献者!ClickHouse的受欢迎程度在很大程度上要归功于社区的努力贡献。看到这个社区不断壮大总是让人感到谦卑。
如果你在这里看到了你的名字,请与我们联系,当然我们也会在Twitter等地找到你。
Chang Chen、Dmitry Kardymon、Hongbin Ma、Julian Maicher、Thomas Panetti、YalalovSM、kevinyhzou、tpanetti、郭小龙
联系我们
手机号:13910395701
邮箱:Tracy.Wang@clickhouse.com
满足您所有的在线分析列式数据库管理需求
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。