2017-10-05 39 views
0

我有以下模式更新架构的数据帧中的Apache星火

root 
|-- col_a: string (nullable = false) 
|-- col_b: string (nullable = false) 
|-- col_c_a: string (nullable = false) 
|-- col_c_b: string (nullable = false) 
|-- col_d: string (nullable = false) 
|-- col_e: string (nullable = false) 
|-- col_f: string (nullable = false) 

现在我想的模式转换为这个数据帧是这样一个数据帧。

root 
|-- col_a: string (nullable = false) 
|-- col_b: string (nullable = false) 
|-- col_c: struct (nullable = false) 
    |-- col_c_a: string (nullable = false) 
    |-- col_c_b: string (nullable = false) 
|-- col_d: string (nullable = false) 
|-- col_e: string (nullable = false) 
|-- col_f: string (nullable = false) 

我可以能够通过明确地取出由row类型的每个列的值与map转型的帮助下做到这一点,但是这是非常复杂的过程,看起来并不那么好,

有什么我可以做到这一点吗?

感谢

回答

1

有与定义的内置结构功能:

def struct(cols: Column*): Column 

你可以用它喜欢:

df.show 
+---+---+ 
| a| b| 
+---+---+ 
| 1| 2| 
| 2| 3| 
+---+---+ 

df.withColumn("struct_col", struct($"a", $"b")).show 
+---+---+----------+ 
| a| b|struct_col| 
+---+---+----------+ 
| 1| 2|  [1,2]| 
| 2| 3|  [2,3]| 
+---+---+----------+ 

新的数据框之中的模式:

|-- a: integer (nullable = false) 
|-- b: integer (nullable = false) 
|-- struct_col: struct (nullable = false) 
| |-- a: integer (nullable = false) 
| |-- b: integer (nullable = false) 

在你的情况下,你可以这样做:

df.withColumn("col_c" , struct($"col_c_a", $"col_c_b")).drop($"col_c_a").drop($"col_c_b")