Guest User

Untitled

a guest
Jun 25th, 2018
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. +---+--------+----+
  2. | Id| Size| Amt|
  3. +---+--------+----+
  4. | a1| 1|55.0|
  5. | a2| 2|48.0|
  6. | a3| 3|28.0|
  7. +---+--------+----+
  8.  
  9. StructType([
  10. StructField("Id", StringType(), True),
  11. StructField("Size", IntegerType(), True),
  12. StructField("Amt", FloatType(), True)
  13. ])
  14.  
  15. {"Id":"a1", "Size":1, "Amt":55.0}
  16. {"Id":"a2", "Size":2, "Amt":48.0}
  17. {"Id":"a3", "Size":3, "Amt":28.0}
  18.  
  19. {"Id":"a1", "Size":1, "Amt":55.0, "Arr":[{"Id":"a1","Size":1,"Amt":55.0 }] }
  20. {"Id":"a2", "Size":2, "Amt":48.0, "Arr":[{"Id":"a2","Size":2,"Amt":48.0 }] }
  21. {"Id":"a3", "Size":3, "Amt":28.0, "Arr":[{"Id":"a3","Size":3,"Amt":28.0 }] }
  22.  
  23. df1 = df.select('Id', 'Size', 'Amt', array('Id','Size','Amt').alias("Arr"))
  24. df1.write.json("my_new_output_path")
  25.  
  26. {"Id":"a1", "Size":1, "Amt":55.0, "Arr":["a1", 1 ,55.0] }
  27. {"Id":"a2", "Size":2, "Amt":48.0, "Arr":["a2", 2 ,48.0] }
  28. {"Id":"a3", "Size":3, "Amt":28.0, "Arr":["a3", 3 ,28.0] }
  29.  
  30. df1 = df.select('Id', 'Size', 'Amt', create_map(lit('Id'), 'Id', lit('Size'), 'Size', lit('Amt'), 'Amt').alias("Arr"))
  31.  
  32. {"Id":"a1","Size":1,"Amt":55.0,"Arr":{"Id":"a1","Size":"1","Amt":"55.0"}}
  33. {"Id":"a2","Size":2,"Amt":48.0,"Arr":{"Id":"a2","Size":"2","Amt":"48.0"}}
  34. {"Id":"a3","Size":3,"Amt":28.0,"Arr":{"Id":"a3","Size":"3","Amt":"28.0"}}
Add Comment
Please, Sign In to add comment