首页 > 解决方案 > 如何使用 Python 从 Azure Data Lake Storage Gen2 中的事件中心访问捕获的数据

问题描述

我正在使用 connection_string 访问 Azure Data Lake Gen2 存储,其中大量 Avro 文件由事件中心捕获存储在包含按年/月/日/小时/分钟命名的文件夹的典型目录结构下。我正在使用 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()

它返回一个迭代器(azure.core.paging.ItemPaged 对象),但从这里我无法看到文件夹和文件。我尝试了一个简单的列表理解,[x.name for x in file_system_client.get_paths()]但我得到了错误StorageErrorException:操作返回了无效状态“指定的容器不存在。”

关于如何按照此过程访问 Avro 文件的任何想法?

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

在此处输入图像描述

谢谢

标签: azureazure-blob-storageazure-data-lakeazure-eventhubazure-sdk-python

解决方案


更新:

用你的代码测试它:

在此处输入图像描述


原答案:

调用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.

测试结果:

在此处输入图像描述


推荐阅读