繁体   English   中英

使用spark展平嵌套的json文档并加载到Elasticsearch中

[英]Flatten a nested json document using spark and load into Elasticsearch

我是Spark和Java编程的新手。 给定一个带有嵌套对象的json文件,我需要展平其结构(表示内容)并使用spark加载到Elastisearch中。

例如,

如果我的example.json的内容是:

{
  "title": "Nest eggs",
  "body":  "Making your money work...",
  "tags":  [ "cash", "shares" ],
  "comments": 
    {
      "name":    "John Smith",
      "comment": "Great article",
      "age":     28,
      "stars":   4,
      "date":    "2014-09-01"
    }
  "owner": 
    {
      "name":    "John Smith",
      "age":     28,
    }
}

我想以以下格式重建它,并使用spark将其加载到ES中。

{
  "title": "Nest eggs",
  "body":  "Making your money work...",
  "tags":  [ "cash", "shares" ],
  "comments_name": "John Smith",
  "comments_comment": "Great article",
  "comments_age":     28
  "comments_stars":   4,
  "comments_date":    "2014-09-01"
  "owner_name": "John Smith",
  "owner_age":     28,
 }

如果其中一个嵌套对象为空,则内容也可以保留为空。

任何帮助表示赞赏。 谢谢

您正在寻找的答案在这里

总而言之,您可以通过点标记选择所需的字段。

val df = sqlcontext.read.json(json)    
val flattened = df.select($"title", $"comments.name")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM