2016-10-31 3 views
3

중첩 된 complex Json을 처리 중이며 그 아래 스키마가 있습니다.Spark for Json Data

root 
|-- businessEntity: array (nullable = true) 
| |-- element: struct (containsNull = true) 
| | |-- payGroup: array (nullable = true) 
| | | |-- element: struct (containsNull = true) 
| | | | |-- reportingPeriod: struct (nullable = true) 
| | | | | |-- worker: array (nullable = true) 
| | | | | | |-- element: struct (containsNull = true) 
| | | | | | | |-- category: string (nullable = true) 
| | | | | | | |-- person: struct (nullable = true) 
| | | | | | | |-- tax: array (nullable = true) 
| | | | | | | | |-- element: struct (containsNull = true) 
| | | | | | | | | |-- code: string (nullable = true) 
| | | | | | | | | |-- qtdAmount: double (nullable = true) 
| | | | | | | | | |-- ytdAmount: double (nullable = 

내 요구 사항은 값으로 qtdAmount의 키와 값으로 qtdAmount와 연결 코드와 해시 맵을 만드는 것입니다. Map.put (code + "qtdAmount", qtdAmount). 내가 스파크로 어떻게 할 수 있니?

아래의 쉘 명령을 시도했습니다.

import org.apache.spark.sql._ 
val sqlcontext = new SQLContext(sc) 
val cdm = sqlcontext.read.json("/user/edureka/CDM/cdm.json") 
val spark = SparkSession.builder().appName("SQL").config("spark.some.config.option","some-vale").getOrCreate() 
cdm.createOrReplaceTempView("CDM") 
val sqlDF = spark.sql("SELECT businessEntity[0].payGroup[0] from CDM").show() 
val address = spark.sql("SELECT businessEntity[0].payGroup[0].reportingPeriod.worker[0].person.address from CDM as address") 
val worker = spark.sql("SELECT businessEntity[0].payGroup[0].reportingPeriod.worker from CDM") 
val tax = spark.sql("SELECT businessEntity[0].payGroup[0].reportingPeriod.worker[0].tax from CDM") 
val tax = sqlcontext.sql("SELECT businessEntity[0].payGroup[0].reportingPeriod.worker[0].tax from CDM") 
tax.select("tax.code") 


val codes = tax.select(expode(tax("code")) 
scala> val codes = tax.withColumn("code",explode(tax("tax.code"))).withColumn("qtdAmount",explode(tax("tax.qtdAmount"))).withColumn("ytdAmount",explode(tax("tax.ytdAmount"))) 

지도에 모든 코드와 qtdAmount를 가져 오려고합니다. 그러나 나는 그것을 얻지 못하고있다. 하나의 DF에 대해 여러 개의 explode 문을 사용하면 요소의 데카르트 곱이 생성됩니다.

스파크에서이 복잡한 것의 json을 구문 분석하는 방법에 대한 도움을받을 수 있습니까?

답변

1

이런 식으로 codeqtyAmount을 얻을 수 있습니다.

자세한 내용은
import sqlcontext.implicits._ 

    cdm.select(
     $"businessEntity.element.payGroup.element.reportingPeriod.worker.element.tax.element.code".as("code"), 
     $"businessEntity.element.payGroup.element.reportingPeriod.worker.element.tax.element.qtdAmount".as("qtdAmount") 
    ).show 

확인 this