Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

169
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda