> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Fabric OneLake

> 本指南将逐步介绍如何查询 Microsoft OneLake 中的数据。

export const BetaBadge = ({link, galaxyTrack, galaxyEvent}) => {
  if (link) {
    return <a href={link} target="_blank" rel="noopener noreferrer" className="betaBadge" onClick={galaxyTrack && galaxyEvent ? galaxyOnClick(galaxyEvent) : undefined}>
                <Icon />
                <span>Beta</span>
            </a>;
  }
  return <div className="betaBadge">
            <Icon />
            <span>
                Beta 版功能。 
                <u>
                    <a href="/docs/docs/beta-and-experimental-features#beta-features">
                        了解更多。
                    </a>
                </u>
            </span>
        </div>;
};

<BetaBadge />

ClickHouse 支持与多个目录集成 (如 OneLake、Unity、Glue、Polaris 等) 。本指南将介绍如何使用 ClickHouse 和 [OneLake](https://learn.microsoft.com/en-us/fabric/onelake/onelake-overview) 查询存储在 Microsoft OneLake 中的数据。

Microsoft OneLake 湖仓支持多种表格式。借助 ClickHouse，您可以查询 Iceberg 表。

<Note>
  由于该功能目前处于 Beta 阶段，您需要使用以下命令启用：
  `SET allow_database_iceberg = 1;`
</Note>

<div id="gathering-requirements">
  ## 收集 OneLake 所需信息
</div>

在 Microsoft Fabric 中查询您的表之前，您需要收集以下信息：

* 一个 OneLake 租户 ID (您的 Entra ID)
* 一个应用程序 (客户端) ID
* 一个客户端密钥
* 一个仓库 ID 和一个数据项 ID

您的仓库 ID 就是您的 Workspace ID。

对于 Data Item ID，我们建议使用您的 Lakehouse ID。根据我们的测试，使用 Warehouse ID 无法正常工作。

有关如何找到这些值，请参阅 [Microsoft OneLake 文档](http://learn.microsoft.com/en-us/fabric/onelake/table-apis/table-apis-overview#prerequisites)。

<div id="creating-a-connection-between-unity-catalog-and-clickhouse">
  ## 在 OneLake 与 ClickHouse 之间创建连接
</div>

有了上述所需信息后，您现在即可在 Microsoft OneLake 与 ClickHouse 之间创建连接；不过在此之前，您需要先启用目录：

```sql theme={null}
SET allow_database_iceberg=1
```

<div id="connect-onelake">
  ### 连接 OneLake
</div>

```sql theme={null}
CREATE DATABASE onelake_catalog
ENGINE = DataLakeCatalog('https://onelake.table.fabric.microsoft.com/iceberg')
SETTINGS
catalog_type = 'onelake',
warehouse = 'warehouse_id/data_item_id',
onelake_tenant_id = '<tenant_id>',
oauth_server_uri = 'https://login.microsoftonline.com/<tenant_id>/oauth2/v2.0/token',
auth_scope = 'https://storage.azure.com/.default',
onelake_client_id = '<client_id>',
onelake_client_secret = '<client_secret>'
```

<div id="querying-onelake-using-clickhouse">
  ## 使用 ClickHouse 查询 OneLake
</div>

连接建立后，您就可以开始查询 OneLake：

```sql theme={null}
SHOW TABLES FROM onelake_catalog
```

```response theme={null}
Query id: 8f6124c4-45c2-4351-b49a-89dc13e548a7

   ┌─name──────────────────────────┐
1. │ year_2017.green_tripdata_2017 │
2. │ year_2018.green_tripdata_2018 │
3. │ year_2019.green_tripdata_2019 │
4. │ year_2020.green_tripdata_2020 │
5. │ year_2022.green_tripdata_2022 │
   └───────────────────────────────┘
```

如果使用 Iceberg 客户端，则只会显示已启用 Uniform 的 Delta 表：

要查询某个表：

```sql theme={null}
SELECT *
FROM onelake_catalog.`year_2017.green_tripdata_2017`
LIMIT 1
```

```response theme={null}
Query id: db6b4bda-cc58-4ca1-8891-e0d14f02c890

Row 1:
──────
VendorID:              2
lpep_pickup_datetime:  2017-05-18 16:55:43.000000
lpep_dropoff_datetime: 2017-05-18 18:04:11.000000
store_and_fwd_flag:    N
RatecodeID:            2
PULocationID:          130
DOLocationID:          48
passenger_count:       2
trip_distance:         12.43
fare_amount:           52
extra:                 4.5
mta_tax:               0.5
tip_amount:            0
tolls_amount:          33
ehail_fee:             ᴺᵁᴸᴸ
improvement_surcharge: 0.3
total_amount:          90.3
payment_type:          2
trip_type:             1
congestion_surcharge:  ᴺᵁᴸᴸ
source_file:           green_tripdata_2017-05.parquet
```

<Info>
  **必须使用反引号**

  必须使用反引号，因为 ClickHouse 不支持多个命名空间。
</Info>

要查看该表的 DDL：

```sql theme={null}
SHOW CREATE TABLE onelake_catalog.`year_2017.green_tripdata_2017`
```

```response theme={null}
Query id: 8bd5bd8e-83be-453e-9a88-32de12ba7f24

   ┌─statement───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
1. │ CREATE TABLE onelake_catalog.`year_2017.green_tripdata_2017`                                                                                                               ↴│
   │↳(                                                                                                                                                                          ↴│
   │↳    `VendorID` Nullable(Int64),                                                                                                                                            ↴│
   │↳    `lpep_pickup_datetime` Nullable(DateTime64(6, 'UTC')),                                                                                                                 ↴│
   │↳    `lpep_dropoff_datetime` Nullable(DateTime64(6, 'UTC')),                                                                                                                ↴│
   │↳    `store_and_fwd_flag` Nullable(String),                                                                                                                                 ↴│
   │↳    `RatecodeID` Nullable(Int64),                                                                                                                                          ↴│
   │↳    `PULocationID` Nullable(Int64),                                                                                                                                        ↴│
   │↳    `DOLocationID` Nullable(Int64),                                                                                                                                        ↴│
   │↳    `passenger_count` Nullable(Int64),                                                                                                                                     ↴│
   │↳    `trip_distance` Nullable(Float64),                                                                                                                                     ↴│
   │↳    `fare_amount` Nullable(Float64),                                                                                                                                       ↴│
   │↳    `extra` Nullable(Float64),                                                                                                                                             ↴│
   │↳    `mta_tax` Nullable(Float64),                                                                                                                                           ↴│
   │↳    `tip_amount` Nullable(Float64),                                                                                                                                        ↴│
   │↳    `tolls_amount` Nullable(Float64),                                                                                                                                      ↴│
   │↳    `ehail_fee` Nullable(Float64),                                                                                                                                         ↴│
   │↳    `improvement_surcharge` Nullable(Float64),                                                                                                                             ↴│
   │↳    `total_amount` Nullable(Float64),                                                                                                                                      ↴│
   │↳    `payment_type` Nullable(Int64),                                                                                                                                        ↴│
   │↳    `trip_type` Nullable(Int64),                                                                                                                                           ↴│
   │↳    `congestion_surcharge` Nullable(Float64),                                                                                                                              ↴│
   │↳    `source_file` Nullable(String)                                                                                                                                         ↴│
   │↳)                                                                                                                                                                          ↴│
   │↳ENGINE = Iceberg('abfss://<warehouse_id>@onelake.dfs.fabric.microsoft.com/<data_item_id>/Tables/year_2017/green_tripdata_2017') │
   └─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

<div id="loading-data-from-onelake-into-clickhouse">
  ## 将数据从数据湖加载到 ClickHouse
</div>

如果您需要将数据从 OneLake 加载到 ClickHouse：

```sql theme={null}
CREATE TABLE trips
ENGINE = MergeTree
ORDER BY coalesce(VendorID, 0)
AS SELECT *
FROM onelake_catalog.`year_2017.green_tripdata_2017`
```

```response theme={null}
Query id: d15983a6-ef6a-40fe-80d5-19274b9fe328

Ok.

0 rows in set. Elapsed: 32.570 sec. Processed 11.74 million rows, 275.37 MB (360.36 thousand rows/s., 8.45 MB/s.)
Peak memory usage: 1.31 GiB.
```
