如何使用 Python 从 Azure Data Lake Storage Gen2 中的事件中心访问捕获的数据

How to access captured data from Event Hub in Azure Data Lake Storage Gen2 using Python

我正在使用 connection_string 访问 Azure Data Lake Gen2 存储,其中大量 Avro 文件由 Event Hubs Capture 存储,在包含由 [=47 命名的文件夹的典型目录结构下=].我正在使用 azure.storage.filedatalake 包。

首先我得到一个数据湖服务客户端使用:

datalake_service_client = DataLakeServiceClient.from_connection_string(connection_string)

然后我通过以下方式获取湖中的文件系统:

file_systems = datalake_service_client.list_file_systems()
for file_system in file_systems:
    print(file_system.name)

本例中只有一个文件系统,名为"datalake1"。此时我想访问我希望在其中找到的所有 Avro 文件。我正在尝试首先获得一个文件系统客户端:

file_system_client = datalake_service_client.get_file_system_client("datalake1")

然后使用get_paths方法:

file_system_client.get_paths()

它 returns 一个迭代器(azure.core.paging.ItemPaged 对象),但是从这里我无法看到文件夹和文件。我尝试使用像 [x.name for x in file_system_client.get_paths()] 这样的简单列表理解,但我收到错误 StorageErrorException: Operation returned an invalid status 'The specified container does not exist.'

知道如何按照此过程访问 Avro 文件吗?

编辑:我使用的是 azure-storage-file-datalake 版本 12.0.0。这是代码的屏幕截图:

谢谢

更新:

用你的代码测试过:


原答案:

调用get_paths()方法后,可以用is_directory属性判断是目录还是文件。如果它是一个文件,那么你可以用它做一些事情。

示例代码(在这个示例中,我只是打印出.avro文件路径。请随意修改代码以满足您的需要):

#other code
paths = file_system_client.get_paths()

for path in paths:
    #determine if it is a directory or a file
    if not path.is_directory:
        #here, just print out the file name.
        print(path.name + '\n')
        #you can do other operations here.

测试结果:

问题出在连接字符串上。我再次尝试,但从 Azure 门户中的 "Access keys" blade 获取它,现在它工作正常。我设法 运行 正确 get_paths() 等等。先前的连接字符串取自存储资源管理器,它对应于从 "Shared access signature" blade 检索到的连接字符串。感谢@MartinJaffer-MSFT (MSDN).