如何将数据帧转换为RDD [String,String]?
我有一个数据框
> python3 test.py
Tom
Tom & Dick
Tom, Dick & Harry
Groucho, Chico, Harpo & Zeppo
George, Alfred & Abe
>
如何将其转换为RDD [String,String],其中第一列是键,而剩余列组成的json字符串是值?
df : [id : String, coutry :String, title: String]
答案 0 :(得分:1)
你不能拥有RDD[String, String]
。 RDD只需1 type parameter
所以你想要的是RDD[(String, String)]
。
df.rdd
.map(row => {
val id = row.getString(0)
val country = row.getString(1)
val title = row.getString(2)
val jsonString = s"{country: $country, title: $title}"
(id, jsonString)
})
答案 1 :(得分:0)
有DataFrame.toJSON返回RDD [String],基于此方法,您可以自己进行转换