将Json展平为多行

时间:2018-05-16 18:20:42

标签: json scala apache-spark flatten denormalization

假设我的JSON中有一个数组,我试图将其展平并将其保存到数据库中。如何复制数据,即将数据转换为数组中每个元素的多行?像下面给出的例子。如果我不知道我在JSON中得到什么结构。我怎样才能做到这一点。我在stackoverflow上找到了一个例子,但如果你知道结构,这会有所帮助。如何在不了解结构的情况下实现它?

Flattening JSON into Tabular Structure using Spark-Scala RDD only fucntion

{ "level":{"productReference":{  

     "prodID":"1234",

     "unitOfMeasure":"EA"

  },

  "states":[  
     {  
        "state":"SELL",
        "effectiveDateTime":"2015-10-09T00:55:23.6345Z",
        "stockQuantity":{  
           "quantity":1400.0,
           "stockKeepingLevel":"A"
        }
     },
     {  
        "state":"HELD",
        "effectiveDateTime":"2015-10-09T00:55:23.6345Z",
        "stockQuantity":{  
           "quantity":800.0,
           "stockKeepingLevel":"B"
        }
     }
  ] }}

+------+-------------+-----+-------------------------+--------+-----------------+
|prodID|unitOfMeasure|state|effectiveDateTime        |quantity|stockKeepingLevel|
+------+-------------+-----+-------------------------+--------+-----------------+
|1234  |EA           |SELL |2015-10-09T00:55:23.6345Z|1400.0  |A                |
|1234  |EA           |HELD |2015-10-09T00:55:23.6345Z|800.0   |B                |
+------+-------------+-----+-------------------------+--------+-----------------+

0 个答案:

没有答案