【问题标题】:how to denormalize JSON in spark如何在火花中非规范化 JSON
【发布时间】:2018-10-24 13:16:44
【问题描述】:

例如,这是如下的 JSON。我实际上是从亚马逊网站上拿的,但我认为这适用于这个问题。

{
    "player": {
        "username": "user1",
        "characteristics": {
            "race": "Human",
            "class": "Warlock",
            "subclass": "Dawnblade",
            "power": 300,
            "playercountry": "USA"
        },
        "arsenal": {
            "kinetic": {
                "name": "Sweet Business",
                "type": "Auto Rifle",
                "power": 300,
                "element": "Kinetic"
            },
            "energy": {
                "name": "MIDA Mini-Tool",
                "type": "Submachine Gun",
                "power": 300,
                "element": "Solar"
            },
            "power": {
                "name": "Play of the Game",
                "type": "Grenade Launcher",
                "power": 300,
                "element": "Arc"
            }
        },
        "armor": {
            "head": "Eye of Another World",
            "arms": "Philomath Gloves",
            "chest": "Philomath Robes",
            "leg": "Philomath Boots",
            "classitem": "Philomath Bond"
        },
        "location": {
            "map": "Titan",
            "waypoint": "The Rig"
        }
    }
} 

我想将其转换为以下内容并另存为 avro。我是 spark 编程的新手,所以围绕函数式风格来思考 Java 背景有点困难。请至少指导我,以便我可以自己编写代码。

{
    "player.username": "user1",
    "player.characteristics.race": "Human",
    "player.characteristics.class": "Warlock",
    "player.characteristics.subclass": "Dawnblade",
    "player.characteristics.power": 300,
    "player.characteristics.playercountry": "USA",
    "player.arsenal.kinetic.name": "Sweet Business",
    "player.arsenal.kinetic.type": "Auto Rifle",
    "player.arsenal.kinetic.power": 300,
    "player.arsenal.kinetic.element": "Kinetic",
    "player.arsenal.energy.name": "MIDA Mini-Tool",
    "player.arsenal.energy.type": "Submachine Gun",
    "player.arsenal.energy.power": 300,
    "player.arsenal.energy.element": "Solar",
    "player.arsenal.power.name": "Play of the Game",
    "player.arsenal.power.type": "Grenade Launcher",
    "player.arsenal.power.power": 300,
    "player.arsenal.power.element": "Arc",
    "player.armor.head": "Eye of Another World",
    "player.armor.arms": "Philomath Gloves",
    "player.armor.chest": "Philomath Robes",
    "player.armor.leg": "Philomath Boots",
    "player.armor.classitem": "Philomath Bond",
    "player.location.map": "Titan",
    "player.location.waypoint": "The Rig"
}

【问题讨论】:

标签: json scala apache-spark denormalization


【解决方案1】:

其实和spark没什么太大关系,你只需要一个函数将嵌套的json字符串展平为一个新的json字符串。如果您首先拥有 RDD[String],则代码示例将是一些东西。

rdd.mapPartitions(ps => ps.map(jsonFlatten)) 

然后将rdd 转换为dataframe

可以从这里找到示例jsonFlatten

Play [Scala]: How to flatten a JSON object

【讨论】:

  • 在读取多个文件中的多个 JSON 字符串时如何使用这些扁平化方法?
  • 尝试使用您提到的方法时出现以下错误。不知道我做错了什么。类型不匹配,预期:Iterator[Row] => Iterator[NotInferedU],实际:Iterator[Row] => Any 类型不匹配,预期:Row => NotInferedB,实际:(JsValue, String) => JsObject 类型不匹配,预期:行 => NotInferedB,实际:(JsValue, String) => JsObject
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-06-21
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
  • 2017-10-27
  • 2013-08-21
  • 2016-05-13
相关资源
最近更新 更多