简体   繁体   English

Azure 数据湖 - 使用 Python 读取

[英]Azure data lake - read using Python

I am trying to read a file from Azure Data lake using Python in a Databricks notebook.我正在尝试使用 Databricks 笔记本中的 Python 从 Azure 数据湖读取文件。 this is the code I used,这是我使用的代码,

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)

The output I get is 0. Can you some point what I am doing wrong.我得到的 output 是 0。你能指出我做错了什么吗? my expectation is to print the file content.我的期望是打印文件内容。

The from_connection_string method returns a DataLakeFileClient , you could not use it to download the file. from_connection_string方法返回一个DataLakeFileClient ,您不能使用它来下载文件。

If you want to download a file to local, you could refer to my below code.如果你想下载一个文件到本地,你可以参考我下面的代码。

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()

If you want more sample code, you could refer to this doc: Azure Data Lake Storage Gen2 .如果您需要更多示例代码,可以参考此文档: Azure Data Lake Storage Gen2

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

相关问题 如何使用 python 对 azure-data-lake 中的文件应用 elasticsearch? - how to apply elasticsearch using python on files in azure-data-lake? 使用 Python 将 Power BI 报告导出到 Azure 数据湖 - Export Power BI report to Azure Data Lake using Python Azure function 绑定 Azure 数据湖(python) - Azure function binding for Azure data lake (python) 用于访问 Azure Data Lake Store 的 Python 代码 - Python code to access Azure Data Lake Store Azure 数据湖 + Python:身份验证失败 - Azure Data Lake + Python: Auth failure 可以使用 azure python ZEAE18BC41E14314DD98FA2DD9889 根据上次修改时间过滤 azure 数据湖文件吗? - Can azure data lake files be filtered based on Last Modified time using azure python sdk? 使用 Python 仅获取 Azure Data Lake 中目录中的子文件夹名称列表 - To get only list of subfolder names in Directory in Azure Data Lake using Python 使用 python 将文件从 azure 数据湖 Gen 1 移动到临时目录 - move file from azure data lake Gen 1 to a temp directory using python 如何使用 Python 从 Windows 共享网络驱动器获取文件并上传到 Azure Data Lake Storage 位置? - How to fetch files from Windows Shared Network Drive and upload to Azure Data Lake Storage location using Python? 上传到 Azure Data Lake gen 2 后 Parquet 文件不可读(Python) - Parquet file after upload to Azure Data Lake gen 2 not readable (Python)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM