【问题标题】:Azure databricks dataframe write gives job abort errorAzure databricks 数据帧写入会导致作业中止错误
【发布时间】:2020-12-10 08:41:49
【问题描述】:

我正在尝试将数据写入 csv 文件并将文件存储在 Azure Data Lake Gen2 中,并遇到作业中止错误消息。同样的代码以前可以正常工作。

错误信息:

org.apache.spark.SparkException: Job aborted.   

代码:

import requests
response = requests.get('https://myapiurl.com/v1/data', auth=('user', 'password'))
data = response.json()
from pyspark.sql import *
df=spark.createDataFrame([Row(**i) for i in data])  
df.write.format(source).mode("overwrite").save(path) #error line

【问题讨论】:

  • 您能分享一下您尝试执行的代码吗?
  • 嗨@HimanshuSinha-msft,感谢您的回复。请查找已更新代码的 OP。
  • 您能告诉我您如何在数据块中访问 Azure Data Lake Gen2 吗?
  • 您是否将spark.conf.set( "fs.azure.account.key.<storage-account-name>.blob.core.windows.net", "<storage-account-access-key>") 添加到您的代码中
  • @paone 此外,如果您使用 ADLS Gen2,您需要使用abfss 协议来访问文件并将spark.conf.set( "fs.azure.account.key.<storage-account-name>.dfs.core.windows.net", "<storage-account-access-key-name>") 添加到您的代码中以进行身份​​验证。更多详情请参考docs.microsoft.com/en-us/azure/databricks/data/data-sources/…

标签: pyspark azure-databricks pyspark-dataframes azure-data-lake-gen2


【解决方案1】:

下面我总结一下解决方案

如果你想在 Azure databricks 中访问 Azure 数据湖 gen2,你有两种选择。

  1. 将 Azure 数据湖 gen2 挂载为 Azure databricks 的文件系统。完成后,您可以使用路径/mnt/<> 读写文件。我们只需要运行一次代码。

    一个。创建服务主体并将 Storage Blob Data Contributor 分配给 Data Lake Storage Gen2 存储帐户范围内的 sp

     az login
    
     az ad sp create-for-rbac -n "MyApp" --role "Storage Blob Data Contributor" \
    --scopes /subscriptions/<subscription>/resourceGroups/<resource-group>/providers/Microsoft.Storage/storageAccounts/<storage-account>
    

    b.代码

     configs = {"fs.azure.account.auth.type": "OAuth",
      "fs.azure.account.oauth.provider.type": "org.apache.hadoop.fs.azurebfs.oauth2.ClientCredsTokenProvider",
      "fs.azure.account.oauth2.client.id": "<appId>",
      "fs.azure.account.oauth2.client.secret": "<clientSecret>",
      "fs.azure.account.oauth2.client.endpoint": "https://login.microsoftonline.com/<tenant>/oauth2/token",
      "fs.azure.createRemoteFileSystemDuringInitialization": "true"}
    
     dbutils.fs.mount(
        source = "abfss://<container-name>@<storage-account-name>.dfs.core.windows.net/folder1",
        mount_point = "/mnt/flightdata",
        extra_configs = configs)
    
  2. 使用存储帐户访问密钥直接访问。

    我们可以将代码spark.conf.set( "fs.azure.account.key.&lt;storage-account-name&gt;.dfs.core.windows.net", "&lt;storage-account-access-key-name&gt;") 添加到我们的脚本中。然后我们就可以读写路径为abfss://&lt;file-system-name&gt;@&lt;storage-account-name&gt;.dfs.core.windows.net/的文件了。

    例如

     from pyspark.sql.types import StringType
     spark.conf.set(
       "fs.azure.account.key.testadls05.dfs.core.windows.net", "<account access key>")
    
      df = spark.createDataFrame(["10", "11", "13"], StringType()).toDF("age")
      df.show()
      df.coalesce(1).write.format('csv').option('header', True).mode('overwrite').save('abfss://test@testadls05.dfs.core.windows.net/result_csv') 
    

更多详情请参考here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-24
    • 1970-01-01
    • 2021-10-22
    • 2015-10-11
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    相关资源
    最近更新 更多