How to save nested or JSON object in spark Dataset with converting to RDD?

北战南征 提交于 2019-12-06 22:34:37

You can pass as in the column struct , so that that will be saved as the name you passed.

 Dataset<Row> tstDS = spark.read().format("csv").option("header", "true").load("/home/exa9/Documents/SparkLogs/y.csv");

              tstDS.show();

/****
+---+-----+------+----------+
|  A|A_SRC|Past_A|Past_A_SRC|
+---+-----+------+----------+
| a1|   s1|    a2|        s2|
+---+-----+------+----------+

****/
              tstDS.withColumn("A", 


                      functions.to_json( 
                              functions.struct(

                                      functions.col("A").as("source"),
                                      functions.col("A_SRC").as("value"),
                                      functions.col("Past_A").as("p_source"),
                                      functions.col("Past_A_SRC").as("p_value")

                                      ))
                      )
              .select("A")
              .show(false);

/****

+-----------------------------------------------------------+
|A                                                          |
+-----------------------------------------------------------+
|{"source":"a1","value":"s1","p_source":"a2","p_value":"s2"}|
+-----------------------------------------------------------+

****/


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!