【问题标题】:Reading parquet file from ADLS gen2 using service principal使用服务主体从 ADLS gen2 读取镶木地板文件
【发布时间】:2021-04-07 14:29:14
【问题描述】:

我正在使用 azure-storage-file-datalake 包连接 ADLS gen2

from azure.identity import ClientSecretCredential

# service principal credential
tenant_id = 'xxxxxxx'
client_id = 'xxxxxxxxx'
client_secret = 'xxxxxxxx'
storage_account_name = 'xxxxxxxx'

credential = ClientSecretCredential(tenant_id, client_id, client_secret)

service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
        "https", storage_account_name), credential=credential) # I have also tried blob instead of dfs in account_url

ADLS gen2 中的文件夹结构,我必须从中读取 parquet 文件,如下所示。在 ADLS gen2 的容器内,我们的文件夹_a 包含文件夹_b,其中有镶木地板文件。

folder_a
  |-folder_b
      parquet_file1

从我们用来读取这样的 parquet 文件的 gen1 存储中。

from azure.datalake.store import lib
from azure.datalake.store.core import AzureDLFileSystem
import pyarrow.parquet as pq

adls = lib.auth(tenant_id=directory_id,
            client_id=app_id,
            client_secret=app_key)
adl = AzureDLFileSystem(adls, store_name=adls_name) 

f = adl.open(file, 'rb') # 'file is parquet file with path of parquet file folder_a/folder_b/parquet_file1'
table = pq.read_table(f)

我们如何进行 gen2 存储,我们被困在这一点上

http://peter-hoffmann.com/2020/azure-data-lake-storage-gen-2-with-python.html是我们关注的链接。

注意 - 我们没有使用 databrick 来执行此操作

【问题讨论】:

  • 您有权访问存储帐户密钥吗?我们可以尝试使用from azure.storage.file import FileService吗?
  • 不,我们必须使用服务主体凭据。

标签: python-3.x pandas azure-data-lake pyarrow azure-data-lake-gen2


【解决方案1】:

关于问题,请参考以下代码

from azure.identity import ClientSecretCredential
from azure.storage.filedatalake import DataLakeServiceClient
import pyarrow.parquet as pq
import io

client_id = ''
client_secret = ''
tenant_id = ''
credential = ClientSecretCredential(tenant_id, client_id, client_secret)

storage_account_name = 'testadls05'
service_client = DataLakeServiceClient(account_url="{}://{}.dfs.core.windows.net".format(
    "https", storage_account_name), credential=credential)
file_system = '<container name>'
file_system_client = service_client.get_file_system_client(file_system)

file_path = ''
file_client = file_system_client.get_file_client(file_path)
data = file_client.download_file(0)
with io.BytesIO() as b:
    data.readinto(b)
    table = pq.read_table(b)
    print(table)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 2021-10-26
    • 1970-01-01
    • 2018-08-13
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    • 2019-12-07
    相关资源
    最近更新 更多