连接到Azure存储帐户以使用polars库通过托管身份读取镶木地板文件



我正在使用polars库的python版本来读取一个行数很大的镶木地板文件。这是图书馆的链接-https://github.com/pola-rs/polars

我正在尝试使用read_parquet方法从Azure存储帐户读取镶木地板文件。我可以看到有一个storage_options参数,可以用来指定如何连接到数据存储。以下是read_parquet方法的定义-

def read_parquet(
source: str | Path | BinaryIO | BytesIO | bytes,
columns: list[int] | list[str] | None = None,
n_rows: int | None = None,
use_pyarrow: bool = False,
memory_map: bool = True,
storage_options: dict[str, object] | None = None,
parallel: ParallelStrategy = "auto",
row_count_name: str | None = None,
row_count_offset: int = 0,
low_memory: bool = False,
pyarrow_options: dict[str, object] | None = None,
) -> DataFrame:

有人能告诉我,如果我使用系统分配的托管身份,作为连接到Azure存储帐户的storage_options的一部分,我需要提供哪些值吗。不幸的是,我找不到任何这样的例子。大多数示例都使用连接字符串和访问密钥,由于安全原因,我无法使用它们。

edit:我刚刚知道storage_options被传递到另一个名为ffspec的库。但我对此一无所知。

此代码应该可以工作:

import pandas as pd
storage_options = {'account_name' : '<account>', 'sas_token' : '<token>'}
df = pd.read_parquet('abfs://<container>@<account>.dfs.core.windows.net/<parquet path>', storage_options = storage_options)

我终于找到了解决方案,任何想要使用托管身份连接到azure数据湖存储gen2帐户的人都可以按照以下步骤操作。正如有人在评论中提到的,polars正在使用fsspec和adlfs-python库连接到Azure云中的远程文件。要使用托管身份进行连接,我们可以使用以下代码-

import polars as pl
storage_options={'account_name': ACCOUNT_NAME, 'anon': False}
df = pl.read_parquet(path=<remote-file-path>,columns=<list of columns>,storage_options=storage_options)

这将尝试使用azure.identity库中的DefaultAzureCredential连接到存储帐户。如果您已经为Azure资源启用了具有适当RBAC权限的托管身份,则应该能够进行连接。

文件:https://github.com/fsspec/adlfs#setting-凭证

相关内容

最新更新