azure - 如何使用 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。这是代码的屏幕截图:
谢谢
解决方案
更新:
用你的代码测试它:
原答案:
调用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.
测试结果:
推荐阅读
- python - 如何从数据框中的列中输出一系列数字?
- javascript - 将最终的 json 从重复请求写入到文件中以休息 API
- python - Keras 训练神经网络维度值错误:预期有 2 个维度,但得到了形状为 (32, 1, 4) 的数组
- python - 如何为每个 .py 文件创建单独的 zip 文件
- javascript - 根据 Ruby on Rails 中的另一个下拉菜单更改下拉菜单
- google-workspace - Google Classroom API 错误代码 403 调用者没有权限 PERMISSION_DENIED
- aws-sdk - Amazon SWF ActivityWorker - workerForCommonTaskList 与 workerForHostSpecificTaskList
- python - 查找 ANOVA 第一线性模型的 F 统计值
- c# - 将秒表上传并提取到 SQL Server 数据库 C#
- mysql - 有多个记录的案例