2017-04-16 46 views
0

我有這個特殊的JSON文件,它是由PostgreSQL生成的日誌。我將這個JSON文件從多行格式轉換爲單行格式。在我分析的Dataframe中的一個字段中有一個字符串列。此字符串列本身就是JSON格式的這個例子:查詢使用Spark DataFrames的JSON數據列,但不知道它的Schema

"query_block": { 
    "select_id": 1, 
    "cost_info": { 
     "query_cost": "671.64" 
    }, 
    "ordering_operation": { 
     "using_filesort": true, 
     "cost_info": { 
     "sort_cost": "100.00" 
     }, 
     "table": { 
     "table_name": "test1", 
     "access_type": "range", 
     "possible_keys": [ 
      "PRIMARY" 
     ], 
     "key": "PRIMARY", 
     "used_key_parts": [ 
      "id" 
     ], 
     "key_length": "4", 
     "rows_examined_per_scan": 100, 
     "rows_produced_per_join": 100, 
     "filtered": "100.00", 
     "cost_info": { 
      "read_cost": "21.64", 
      "eval_cost": "20.00", 
      "prefix_cost": "41.64", 
      "data_read_per_join": "18K" 
     }, 
     "used_columns": [ 
      "id", 
      "c" 
     ], 
     "attached_condition": "(`proxydemo`.`test1`.`id` between 501747 and <cache>((504767 + 99)))" 
     } 
    } 
    } 
} 

我知道,在星火2.0+我可以使用

from_json(E:柱,模式:StructType):列

函數來自SparkSQL函數。但我不確定該字符串的模式應該是什麼。我已經完成了很多模式和StructType定義,但是這個有點層次!我不明白這個模式應該如何定義! `

+0

我只是想補充一點,我不要求某人爲我完成模式,只知道解釋這個分層數據模式的技術是什麼? –

回答

0

我發現了嵌套模式是如何工作的。

在該特定示例的架構去,因爲這:

對於對象的根:

val query_block_schema = (new StructType) 
     .add("select_id", LongType) 
     .add("cost_info", StringType) 
     .add("ordering_operation", StringType) 

對於第二層:

val query_plan_schema = (new StructType) 
    .add("query_block", StringType) 

等等...

所以我認爲這個問題已經解決。之後,我將所有這些合併在一起,以防它們不爲null,並基本上平整整個嵌套對象。

相關問題