[英]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.