【问题标题】:Write result of api to a data lake with Databricks使用 Databricks 将 api 的结果写入数据湖
【发布时间】:2020-08-11 19:18:13
【问题描述】:

我正在使用以下代码进行 api 调用。

import requests   
response = requests.get('https://myapi.com/api/v1/city', auth=('user', 'password'))
data = response.json()

希望将 json 有效负载保存为 Azure Data Lake Storage Gen2 中的 json 文档并从此处读取文档。

data.write.json("wasbs://<file_system>@<storage-account-name>.dfs.core.windows.net/city.json")

错误: AttributeError: 'list' object has no attribute 'write'

【问题讨论】:

    标签: apache-spark pyspark azure-databricks


    【解决方案1】:

    您需要将 json 列表 data 变量转换为数据框,或者使用

    df=spark.createDataFrame(data,schema), df=spark.read.json(data)

    然后使用写入 Azure DataLake 存储

    df.write.json("wasbs://&lt;file_system&gt;@&lt;storage-account-name&gt;.dfs.core.windows.net/city.json")

    Or 如果您不想转换为 json,请使用

    spark.createDataFrame(data,schema).saveAsTextFile("&lt;path&gt;")


    更新:

    尝试使用 Row 对象创建数据框。

    Example:

    data=[{'id': 1}]
    
    from pyspark.sql import *
    
    df=spark.createDataFrame([Row(**i) for i in data])
    df.show()
    #+---+
    #| id|
    #+---+
    #|  1|
    #+---+
    
    df.write.json("<path>")
    

    【讨论】:

    • 嗨@484,当我使用df=spark.read.json(data) df.write.json("wasbs://fdmtestfileshare@entedatdevdl.dfs.core.windows.net/building.json") 时出现以下错误。 java.lang.ArrayStoreException: java.util.HashMap
    • @paone,请检查我编辑的答案 Update 部分,因为我们正在创建数据帧然后写入存储。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 2019-04-22
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多