簡體   English   中英

在 spark 和 scala 中 Rdd 到 json

[英]Rdd to json in spark and scala

我使用帶有 spark/scala 的 Json 文件並將其保存在 rdd 中。

  val dataFile = "resources/tweet-json/hello.json"
  lazy val rdd = SparkCommons.sqlContext.read.format("json").load(dataFile)

查詢 rdd 后,我想再次生成一個 Json 輸出文件(我將通過 get Http 請求發送該文件)。 我如何在 json 中轉換這個 rdd?

[
{
    "label": [
        "fattacq_an_eser_facq",
        "eu_tot_doc",
        "fattacq_prot_facq",
        "id_sogg",
        "eu_tot_man"
    ],
    "values": [
        {
            "label": "Prima Fattura 2016",
            "values": [
                2016,
                956.48,
                691,
                44633,
                956.48
            ]
        },
        {
            "label": "Seconda Fattura 2016",
            "values": [
                2016,
                190,
                982,
                38127,
                190
            ]
        },
        {
            "label": "Terza Fattura 2016",
            "values": [
                2016,
                140.3,
                1088,
                59381,
                140.3
            ]
        },
        {
            "label": "Quarta Fattura 2016",
            "values": [
                2016,
                488,
                1091,
                59382,
                488
            ]
        },
        {
            "label": "Quinta Fattura 2016",
            "values": [
                2016,
                11365.95,
                1154,
                57526,
                11365.95
            ]
        },
        {
            "label": "Sesta Fattura 2016",
            "values": [
                2016,
                44440.01,
                1276,
                5555,
                44440.01
            ]
        }
    ]
  }
]

您可以簡單地使用 write 函數寫出 Json 示例:

dfTobeSaved.write.format("json").save("/root/data.json")

我認為這應該可以正常工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM