Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

168
Views
Flatten a nested json document using spark and load into Elasticsearch

I am relatively new to Spark and java programming. Given a json file with nested objects I need to flatten its structure(denorm the contents) and load into Elastisearch using spark.

For instance,

if the contents of my example.json is:

{
  "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,
    }
}

I would want to reconstruct this in the below format and load it into ES using spark.

{
  "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,
 }

In case one of the nested objects is empty, the contents can be left empty too.

Any help is appreciated. Thanks

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The answer you are looking for is here.

To summarize, you can just select out the fields you need via dot notation.

val df = sqlcontext.read.json(json)    
val flattened = df.select($"title", $"comments.name")
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!