I have the following json loaded as a Dataframe:
root
|-- transactions_analytics_data: struct (nullable = true)
| |-- cumulative_fraud_transactions: long (nullable = true)
| |-- cumulative_fraud_transactions_percent: double (nullable = true)
| |-- cumulative_transactions: long (nullable = true)
| |-- cumulative_transactions_percent: double (nullable = true)
| |-- false_predictions: struct (nullable = true)
| | |-- no_of_false_negatives: long (nullable = true)
| | |-- no_of_false_positives: long (nullable = true)
| |-- false_predictions_amount: struct (nullable = true)
| | |-- predicted_false_negative: double (nullable = true)
| | |-- predicted_false_positive: double (nullable = true)
| |-- fraud_transactions_barline: struct (nullable = true)
| | |-- data: array (nullable = true)
| | | |-- element: struct (containsNull = true)
| | | | |-- falseNegatives: long (nullable = true)
| | | | |-- time: string (nullable = true)
| | | | |-- totalFrauds: long (nullable = true)
| |-- fraud_transactions_map: struct (nullable = true)
| | |-- California: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Connecticut: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Delaware: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Florida: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Georgia: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Hawaii: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Idaho: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Illinois: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Indiana: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Iowa: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Kansas: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Kentucky: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Louisiana: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Maine: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Maryland: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Massachusetts: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Michigan: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Minnesota: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Mississippi: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Missouri: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Montana: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Nebraska: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Nevada: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- New Hampshire: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- New Jersey: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- New Mexico: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- New York: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- North Carolina: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- North Dakota: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Ohio: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Oklahoma: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Oregon: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Pennsylvania: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Rhode Island: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- South Carolina: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- South Dakota: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Tennessee: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Texas: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Utah: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Vermont: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Virginia: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Washington: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| | |-- Wisconsin: struct (nullable = true)
| | | |-- falseNegatives: long (nullable = true)
| | | |-- falsePositives: long (nullable = true)
| | | |-- totalFrauds: long (nullable = true)
| |-- top10_affected_merchants: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- count: long (nullable = true)
| | | |-- date_affected: string (nullable = true)
| | | |-- merchange_category: string (nullable = true)
I wanted a DataFrame with each attribute as column, I tried the below for top10_affected_merchants which is working fine because it is array but it did not work on struct, please help me
val sqlContext = new org.apache.spark.sql.hive.HiveContext(sc)
import sqlContext.implicits._
val jsonDF = sqlContext.jsonFile("file:///root/sample.json")
jsonDF.printSchema()
///TOP 10 MARCHAENTS DATA SET
val top10_affected_merchantsDF = jsonDF.select(explode(jsonDF("transactions_analytics_data.top10_affected_merchants")))
top10_affected_merchantsDF.printSchema()
top10_affected_merchantsDF.registerTempTable("top10affectedmerchants")
val topaffectedmerchantsDF = sqlContext.sql("SELECT col.count, col.date_affected, col.merchange_category FROM top10affectedmerchants")
topaffectedmerchantsDF.printSchema()
topaffectedmerchantsDF.show()