繁体   English   中英

如何使用 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 文件由事件中心捕获存储在包含按年/月/日/小时/分钟命名的文件夹的典型目录结构下。 我正在使用 azure.storage.filedatalake package。

首先,我使用以下方式获得数据湖服务客户端:

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: 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 门户中的“访问密钥”刀片中获取它,现在它工作正常。 我设法正确运行 get_paths() 等等。 先前的连接字符串取自 Storage Explorer,它对应于从“共享访问签名”刀片中检索到的连接字符串。 感谢@MartinJaffer-MSFT ( MSDN )。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM