2017-10-17 77 views
0

我有下面的JSON結構,我試圖將每個元素轉換爲列,如下所示使用Spark SQL進行結構化。爆炸(控制)不起作用。有人可以建議一種方法來做到這一點?Spark SQL爆炸結構數組

輸入:

{ 
"control" : [ 
    { 
     "controlIndex": 0, 
     "customerValue": 100.0, 
     "guid": "abcd", 
     "defaultValue": 50.0, 
     "type": "discrete" 
    }, 
    { 
     "controlIndex": 1, 
     "customerValue": 50.0, 
     "guid": "pqrs", 
     "defaultValue": 50.0, 
     "type": "discrete" 
    } 
    ] 
} 

所需的輸出:

controlIndex customerValue guid defaultValult type 

0   100.0   abcd 50.0   discrete 

1   50.0   pqrs 50.0   discrete 
+1

你能添加代碼你試過了嗎? – mrsrinivas

+0

我試過 - 從myview中選擇爆炸(控制)它使一列,並把每個結構內, – abiswas

回答

0

爆炸將會給定數組中創建的每個元素的新行或映射列

import org.apache.spark.sql.functions.explode 

df.select(
    explode($"control") 
)  
+0

我會嘗試...... thx – abiswas