使用 R 从 Microsoft Azure 读取 csv 文件

标签 r azure databricks

我最近开始使用 databricks 和 azure。

我有 Microsoft Azure 存储资源管理器。我在 databricks 上运行了一个 jar 程序 它在路径中的 azure storgae explorer 中输出许多 csv 文件

..../myfolder/subfolder/output/old/p/ 

我通常做的事情是进入文件夹 p 并下载所有 csv 文件 右键单击本地驱动器上的 p 文件夹,然后单击 download 以及 R 中的这些 csv 文件进行任何分析。

我的问题是,有时我的运行可能会生成超过 10000 个 csv 文件 下载到本地驱动器需要很长时间。

我想知道是否有教程/R 包可以帮助我阅读 来自上述路径的 csv 文件,无需下载。例如 有什么办法可以设置

..../myfolder/subfolder/output/old/p/  

作为我的工作目录并以与我相同的方式处理所有文件。

编辑: 路径的完整 url 看起来像这样:

https://temp.blob.core.windows.net/myfolder/subfolder/output/old/p/

最佳答案

根据官方文档 CSV Files Azure Databricks 的,您可以直接读取 Azure Databricks 笔记本的 R 中的 csv 文件,如 Read CSV files notebook example 部分的 R 示例如下图所示。

enter image description here

或者,我使用 R 包 reticulate和Python包azure-storage-blob使用 Azure Blob 存储的 sas token 直接从 blob url 读取 csv 文件。

这是我的步骤如下。

  1. 我在 Azure Databricks 工作区中创建了一个 R 笔记本。
  2. 安装 R 包 reticulate通过代码install.packages("reticulate") .

    enter image description here

  3. 安装Python包azure-storage-blob如下面的代码。

    %sh
    pip install azure-storage-blob
    

    enter image description here

  4. 要运行 Python 脚本来生成容器级别的 sas token 并使用它来获取带有 sas token 的 blob url 列表,请参阅下面的代码。

    library(reticulate)
    py_run_string("
    from azure.storage.blob.baseblobservice import BaseBlobService
    from azure.storage.blob import BlobPermissions
    from datetime import datetime, timedelta
    
    account_name = '<your storage account name>'
    account_key = '<your storage account key>'
    container_name = '<your container name>'
    
    blob_service = BaseBlobService(
        account_name=account_name,
        account_key=account_key
    )
    
    sas_token = blob_service.generate_container_shared_access_signature(container_name, permission=BlobPermissions.READ, expiry=datetime.utcnow() + timedelta(hours=1))
    
    blob_names = blob_service.list_blob_names(container_name, prefix = 'myfolder/')
    blob_urls_with_sas = ['https://'+account_name+'.blob.core.windows.net/'+container_name+'/'+blob_name+'?'+sas_token for blob_name in blob_names]
    ")
    blob_urls_with_sas <- py$blob_urls_with_sas
    

    enter image description here

  5. 现在,我可以在 R 中使用不同的方式使用 sas token 从 blob url 读取 csv 文件,如下所示。

    5.1。 df <- read.csv(blob_urls_with_sas[[1]])

    enter image description here

    5.2。使用 R 包 data.table

    install.packages("data.table")
    library(data.table)
    df <- fread(blob_urls_with_sas[[1]])
    

    enter image description here

    5.3。使用 R 包 readr

    install.packages("readr")
    library(readr)
    df <- read_csv(blob_urls_with_sas[[1]])
    

    enter image description here

注意:对于 reticulate库,请引用RStudio文章 Calling Python from R .

希望有帮助。

<小时/>

更新您的快速问题:

enter image description here

关于使用 R 从 Microsoft Azure 读取 csv 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57355132/

相关文章:

sql-server - 如何从Databricks集群运行SQL语句

r - R中第三维的均值

r - R Markdown 和 Windows 中的多行 latex 方程

c# - 如何关闭 Microsoft Azure Visual Studio C# WebRole WorkerRole 调试输出噪音?

java - 如何查询存储在 azure WADMETRICS 表中的指标?

r - sparkr 数据 block 错误 : too many open devices

azure - Databricks Runtime 10.4 LTS - 升级后出现 AnalysisException : No such struct field id in 0, 1

c++ - R/C++ 中集合覆盖问题的变体

r - 基于不同组合计算群体相对频率的误差

azure - 尝试使用另一个 Azure 租户的 guest Azure AD 帐户通过 SSO 登录 Office 加载项