我正在使用connection_string来访问Azure Data Lake Gen2存储,在其中典型的目录结构下,Event Hubs Capture在其中存储了许多Avro文件,该目录结构包含以年/月/日/小时/分钟命名的文件夹。我正在使用azure.storage.filedatalake软件包。
首先,我使用以下方法获得了Data Lake服务客户端:
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文件的任何想法?
谢谢
get_paths()
方法后,可以使用is_directory
属性确定它是目录还是文件。如果它是文件,那么您可以使用它来做一些事情。示例代码:
#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.