2017-08-31 172 views
-1

我有模式的數據幀像下面火花斯卡拉 - JSON字符串轉換成JSON結構

|-- aaa: string (nullable = true) 
|-- bbb: long (nullable = false) 
|-- ccc: string (nullable = true) 
|-- ddd: string (nullable = true) 
|-- eee: long (nullable = false) 

我已經有結果VAL針對以上。

我想要的模式轉換如下用火花斯卡拉

|ddd:struct (nullable = true) 
     |-- aaa: string (nullable = true) 
     |-- bbb: string (nullable = true) 
     |-- ccc: string (nullable = true) 
     |-- eee: string (nullable = true) 

請幫

+1

請出示你的努力和發佈您的代碼。 –

+0

歡迎來到計算器。請給出你的問題的更詳細的描述。儘可能包含代碼。 –

回答

0

您應該使用struct function

第一種方法是使用withColumn API,如果你不想要原始列可以drop他們作爲

import org.apache.spark.sql.functions._ 
df.withColumn("ddd", struct(col("aaa"), col("bbb").cast(StringType).as("bbb"), col("ccc"), col("eee").cast(StringType).as("eee"))).drop("aaa", "bbb", "ccc", "eee") 

第二個方法是使用select API作爲

df.select(struct(col("aaa"), col("bbb").cast(StringType).as("bbb"), col("ccc"), col("eee").cast(StringType).as("eee")).as("ddd")) 
+0

喜惡劣感謝您的答覆 – gayathri

+0

但它附加 – gayathri

+0

我要創建一個新的模式作爲 – gayathri