2016-10-31 5 views
3

私はネストされた複合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をマップに取得しようとしています。しかし、私はそれを取得していません。 1つのDFに複数の展開文を使用すると、要素のデカルト積が生成されます。

誰かがスパークのこの非常に複雑な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

をチェック