简体   繁体   English

使用 azure 数据块 pyspark 访问 azure 文件

[英]access azure files using azure databricks pyspark

I am trying to access a file which is Rds extension.我正在尝试访问一个扩展名为 Rds 的文件。 I am using the below code however it is not helping.我正在使用下面的代码,但它没有帮助。

import pandas as pd

url_sas_token = 'https://<my account name>.file.core.windows.net/test/test.rds?st=2020-01-27T10%3A16%3A12Z&se=2020-01-28T10%3A16%3A12Z&sp=rl&sv=2018-03-28&sr=f&sig=XXXXXXXXXXXXXXXXX'
# Directly read the file content from its url with sas token to get a pandas dataframe
pdf = pd.read_excel(url_sas_token )
# Then, to convert the pandas dataframe to a PySpark dataframe in Azure Databricks
df = spark.createDataFrame(pdf)

I created storage account and created file share and uploaded rds file into file share.我创建了存储帐户并创建了文件共享并将 rds 文件上传到文件共享中。 Image for reference:图片供参考:

在此处输入图像描述

I generated SAS key in storage account.我在存储帐户中生成了 SAS 密钥。 Image for reference:图片供参考:

在此处输入图像描述

I installed azure file shares in data bricks using我在数据块中安装了 azure 个文件共享

pip install azure-storage-file 

在此处输入图像描述

I installed pyreadr package to load rds file using我安装了 pyreadr package 来加载 rds 文件

pip install pyreadr

在此处输入图像描述

I tried to load the rds extension file in databrick using我尝试使用加载 databrick 中的 rds 扩展文件

from azure.storage.file import FilePermissions, FileService
from datetime import datetime, timedelta 
import pyreadr
from urllib.request import urlopen

url_sas_token="<File Service SAS URL>"

response = urlopen(url_sas_token)
content = response.read()
fhandle = open( 'counties.rds', 'wb')
fhandle.write(content)
fhandle.close()
result = pyreadr.read_r("counties.rds")
print(result)

In above code I have given File Service SAS URL at url_sas_token.在上面的代码中,我在 url_sas_token 处提供了文件服务 SAS URL。

image for reference:图片供参考:

在此处输入图像描述

Above code loaded rds file data successfully.以上代码成功加载rds文件数据。 Image for reference:图片供参考:

在此处输入图像描述

In this way I accessed rds extension file which is in azure blob file share from data bricks.通过这种方式,我从数据块访问了 azure blob 文件共享中的 rds 扩展文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM