跳转到主内容
跳转到主内容

azureBlobStorage 表函数

提供类似表的接口,用于在 Azure Blob Storage 中查询/插入文件。此表函数类似于 s3 函数

语法

凭证信息已嵌入到连接字符串中,因此无需单独提供 account_name/account_key 参数:

azureBlobStorage(connection_string, container_name, blobpath [, format, compression, structure])

参数

ArgumentDescription
connection_string包含嵌入凭证(账户名称 + 账户密钥或 SAS 令牌)的连接字符串。使用此形式时,account_nameaccount_key 不应单独传入。参见配置连接字符串
storage_account_url存储账户的终结点 URL,例如 https://myaccount.blob.core.windows.net/。使用此形式时,必须同时传入 account_nameaccount_key
container_name容器名称。
blobpath文件路径。在只读模式下支持以下通配符:***?{abc,def} 以及 {N..M},其中 NM 为数字,'abc''def' 为字符串。
account_name存储账户名称。使用不带 SAS 的 storage_account_url 时为必需;使用 connection_string不得传入。
account_key存储账户密钥。使用不带 SAS 的 storage_account_url 时为必需;使用 connection_string不得传入。
format文件的格式
compression支持的取值:nonegzip/gzbrotli/brxz/LZMAzstd/zst。默认情况下,会根据文件扩展名自动检测压缩格式(等同于设置为 auto)。
structure表结构。格式:'column1_name column1_type, column2_name column2_type, ...'
partition_strategy可选。支持的取值:WILDCARDHIVEWILDCARD 要求路径中包含 {_partition_id},该占位符会被分区键替换。HIVE 不允许使用通配符,假定路径为表根目录,并生成 Hive 风格的分区目录,使用 Snowflake ID 作为文件名,并以文件格式作为扩展名。默认值为 WILDCARD
partition_columns_in_data_file可选。仅在 HIVE 分区策略下使用。用于告知 ClickHouse 数据文件中是否会写入分区列。默认值为 false
extra_credentials使用 client_idtenant_id 进行认证。如果提供了 extra_credentials,则其优先级高于 account_nameaccount_key

命名集合

参数也可以通过命名集合传递。在这种情况下支持以下键:

KeyRequiredDescription
containerYes容器名称。对应位置参数 container_name
blob_pathYes文件路径(可带通配符)。对应位置参数 blobpath
connection_stringNo*带有内嵌凭证的连接字符串。*必须提供 connection_stringstorage_account_url 之一。
storage_account_urlNo*存储账户端点 URL。*必须提供 connection_stringstorage_account_url 之一。
account_nameNo使用 storage_account_url 时必填。
account_keyNo使用 storage_account_url 时必填。
formatNo文件格式。
compressionNo压缩类型。
structureNo表结构。
client_idNo用于认证的 Client ID。
tenant_idNo用于认证的 Tenant ID。
注意

命名集合的键名与位置参数名不同:container(不是 container_name)和 blob_path(不是 blobpath)。

示例:

CREATE NAMED COLLECTION azure_my_data AS
    storage_account_url = 'https://myaccount.blob.core.windows.net/',
    container = 'mycontainer',
    blob_path = 'data/*.parquet',
    account_name = 'myaccount',
    account_key = 'mykey...==',
    format = 'Parquet';

SELECT *
FROM azureBlobStorage(azure_my_data)
LIMIT 5;

你还可以在执行查询时重写命名集合中的值:

SELECT *
FROM azureBlobStorage(azure_my_data, blob_path = 'other_data/*.csv', format = 'CSVWithNames')
LIMIT 5;

返回值

具有指定结构的表,用于在指定文件中读写数据。

示例

使用 storage_account_url 形式读取数据

SELECT *
FROM azureBlobStorage(
    'https://myaccount.blob.core.windows.net/',
    'mycontainer',
    'data/*.parquet',
    'myaccount',
    'mykey...==',
    'Parquet'
)
LIMIT 5;

使用 connection_string 形式读取数据

SELECT *
FROM azureBlobStorage(
    'DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey...==;EndPointSuffix=core.windows.net',
    'mycontainer',
    'data/*.csv',
    'CSVWithNames'
)
LIMIT 5;

使用分区写入

INSERT INTO TABLE FUNCTION azureBlobStorage(
    'DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey...==;EndPointSuffix=core.windows.net',
    'mycontainer',
    'test_{_partition_id}.csv',
    'CSV',
    'auto',
    'column1 UInt32, column2 UInt32, column3 UInt32'
) PARTITION BY column3
VALUES (1, 2, 3), (3, 2, 1), (78, 43, 3);

然后再读取特定的分区:

SELECT *
FROM azureBlobStorage(
    'DefaultEndpointsProtocol=https;AccountName=myaccount;AccountKey=mykey...==;EndPointSuffix=core.windows.net',
    'mycontainer',
    'test_1.csv',
    'CSV',
    'auto',
    'column1 UInt32, column2 UInt32, column3 UInt32'
);
┌─column1─┬─column2─┬─column3─┐
│       3 │       2 │       1 │
└─────────┴─────────┴─────────┘

虚拟列

  • _path — 文件路径。类型:LowCardinality(String)
  • _file — 文件名。类型:LowCardinality(String)
  • _size — 文件大小(字节)。类型:Nullable(UInt64)。如果文件大小未知,则值为 NULL
  • _time — 文件最后一次修改时间。类型:Nullable(DateTime)。如果时间未知,则值为 NULL

分区写入

分区策略

仅支持 INSERT 语句。

WILDCARD(默认):将文件路径中的 {_partition_id} 通配符替换为实际的分区键。

HIVE 在读写时采用 Hive 风格分区。生成的文件格式如下:<prefix>/<key1=val1/key2=val2...>/<snowflakeid>.<toLower(file_format)>

HIVE 分区策略示例

INSERT INTO TABLE FUNCTION azureBlobStorage(
    azure_conf2,
    storage_account_url = 'https://myaccount.blob.core.windows.net/',
    container = 'cont',
    blob_path = 'azure_table_root',
    format = 'CSVWithNames',
    compression = 'auto',
    structure = 'year UInt16, country String, id Int32',
    partition_strategy = 'hive'
) PARTITION BY (year, country)
VALUES (2020, 'Russia', 1), (2021, 'Brazil', 2);
SELECT _path, * FROM azureBlobStorage(
    azure_conf2,
    storage_account_url = 'https://myaccount.blob.core.windows.net/',
    container = 'cont',
    blob_path = 'azure_table_root/**.csvwithnames'
)

   ┌─_path───────────────────────────────────────────────────────────────────────────┬─id─┬─year─┬─country─┐
1. │ cont/azure_table_root/year=2021/country=Brazil/7351307847391293440.csvwithnames │  2 │ 2021 │ Brazil  │
2. │ cont/azure_table_root/year=2020/country=Russia/7351307847378710528.csvwithnames │  1 │ 2020 │ Russia  │
   └─────────────────────────────────────────────────────────────────────────────────┴────┴──────┴─────────┘

use_hive_partitioning 设置

这是一个设置,用于让 ClickHouse 在读取时解析 Hive 风格分区文件。它对写入没有任何影响。若要在读写两侧保持对称,请使用 partition_strategy 参数。

当将 use_hive_partitioning 设置为 1 时,ClickHouse 会在路径中检测 Hive 风格分区 (/name=value/) ,并允许在查询中将分区列作为虚拟列来使用。这些虚拟列的名称将与分区路径中的名称相同。

示例

在查询中使用由 Hive 风格分区创建的虚拟列

SELECT * FROM azureBlobStorage(config, storage_account_url='...', container='...', blob_path='http://data/path/date=*/country=*/code=*/*.parquet') WHERE date > '2020-01-01' AND country = 'Netherlands' AND code = 42;

使用共享访问签名 (SAS)

共享访问签名 (Shared Access Signature,SAS) 是一个 URI,用于授予对 Azure Storage 容器或文件的受限访问权限。使用它可以在不共享存储账户密钥的情况下,为存储账户资源提供限定时间的访问权限。详细信息请参阅此处

azureBlobStorage 函数支持共享访问签名 (SAS)。

Blob SAS 令牌包含对请求进行身份验证所需的全部信息,包括目标 blob、权限以及有效期。要构造 blob URL,请将 SAS 令牌追加到 blob 服务端点之后。例如,如果端点为 https://clickhousedocstest.blob.core.windows.net/,则请求变为:

SELECT count()
FROM azureBlobStorage('BlobEndpoint=https://clickhousedocstest.blob.core.windows.net/;SharedAccessSignature=sp=r&st=2025-01-29T14:58:11Z&se=2025-01-29T22:58:11Z&spr=https&sv=2022-11-02&sr=c&sig=Ac2U0xl4tm%2Fp7m55IilWl1yHwk%2FJG0Uk6rMVuOiD0eE%3D', 'exampledatasets', 'example.csv')

┌─count()─┐
│      10 │
└─────────┘

1 row in set. Elapsed: 0.425 sec.

或者,您可以使用生成的 Blob SAS URL:

SELECT count()
FROM azureBlobStorage('https://clickhousedocstest.blob.core.windows.net/?sp=r&st=2025-01-29T14:58:11Z&se=2025-01-29T22:58:11Z&spr=https&sv=2022-11-02&sr=c&sig=Ac2U0xl4tm%2Fp7m55IilWl1yHwk%2FJG0Uk6rMVuOiD0eE%3D', 'exampledatasets', 'example.csv')

┌─count()─┐
│      10 │
└─────────┘

1 row in set. Elapsed: 0.153 sec.