【问题标题】:Azure data lake - read using PythonAzure 数据湖 - 使用 Python 读取
【发布时间】:2020-05-26 14:12:33
【问题描述】:

我正在尝试在 Databricks 笔记本中使用 Python 从 Azure Data Lake 读取文件。 这是我使用的代码,

from azure.storage.filedatalake import DataLakeFileClient

file = DataLakeFileClient.from_connection_string("DefaultEndpointsProtocol=https;AccountName=mydatalake;AccountKey=******;EndpointSuffix=core.windows.net",file_system_name="files", file_path="/2020/50002")

with open("./sample.txt", "wb") as my_file:
    download = file.download_file()
    content = download.readinto(my_file)
    print(content)

我得到的输出是 0。你能指出我做错了什么吗?我的期望是打印文件内容。

【问题讨论】:

    标签: python azure azure-data-lake azure-databricks


    【解决方案1】:

    from_connection_string 方法返回一个DataLakeFileClient,你不能用它来下载文件。

    如果你想下载一个文件到本地,你可以参考我下面的代码。

    import os, uuid, sys
    from azure.storage.filedatalake import DataLakeServiceClient
    
    service_client = DataLakeServiceClient.from_connection_string("DefaultEndpointsProtocol=https;AccountName=***;AccountKey=*****;EndpointSuffix=core.windows.net")
    
    file_system_client = service_client.get_file_system_client(file_system="test")
    
    directory_client = file_system_client.get_directory_client("testdirectory")
    
    file_client = directory_client.get_file_client("test.txt")
    
    download=file_client.download_file()
    
    downloaded_bytes = download.readall()
    
    with open("./sample.txt", "wb") as my_file:
        my_file.write(downloaded_bytes)
        my_file.close()
    

    如果您需要更多示例代码,可以参考此文档:Azure Data Lake Storage Gen2

    【讨论】:

      猜你喜欢
      • 2019-06-11
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 2022-09-27
      相关资源
      最近更新 更多