繁体   English   中英

使用 spark 和 scala 将具有值的 dataframe 列转换为列表

[英]Converting a dataframe column with values to a list using spark and scala

+-----------------------------------------------------------------------------------------------------------------------------------------------+
|Texts                                                                                                                                          |
+----------------------------------------------------------------------------------------------------------------------------------------------+
|RT @xxxxxx: post aqwe qwqq ssdd qaAQ WQWQW CSDWDW!!! 

must RT !                                                                                                                                      |
|RT @xxxxx: aaa in ssss ssss ss sqqq this qqq in "sss" should xxxx xx at xx xaaaa aqw   |
|RT @xxxx: QWW sadad jkhj to hjyhy a eryr rrryryry? ersfsfdsgdgdgg t rtrt ytyyryr.
 
sadwf wwewe ewewe jyiopo;l dwewre etet of the ddgdg-we dfdfdf, @b…                                                                              |
+-----------------------------------------------------------------------------------------------------------------------------------------------+

我想使用 scala 和 spark 在列表中的 Text 列中包含这些值行。

1. val newList =   myDataframe.select("Texts").rdd.map(_(0)).collect.toList
2. val newList =   myDataframe.select("Texts").collect().map(_(0)).toList
   newList .foreach(println)

两种方式都没有给出任何 output 并且程序也不会终止。 没有错误被抛出。

预期 output

["RT @xxxxxx: post aqwe qwqq ssdd qaAQ WQWQW CSDWDW!!! must RT !", "RT @xxxxx: aaa in ssss ssss ss sqqq this qqq in "sss" should xxxx xx at xx xaaaa aqw", "RT @xxxx: QWW sadad jkhj to hjyhy a eryr rrryryry? ersfsfdsgdgdgg t rtrt ytyyryr.

sadwf wwewe ewewe jyiopo;l dwewre etet of the ddgdg-we dfdfdf, @b…"]

请注意 dataframe 中每一行中的句子可能包含新行

例如I am going to the the shop.\n Its very expensive

这句话将显示为

 I am going to the shop
 its very expensive

但两者都属于同一行。

以下方法正确地将 Z6A8064B5DF4794555500553C47C55057DZ 的列转换为列表

1. val newList =   myDataframe.select("Texts").rdd.map(_(0)).collect.toList
2. val newList =   myDataframe.select("Texts").collect().map(_(0)).toList

但是问题中的 Dataframe 说每一行都可能包含新行。 因此上述方法不能直接工作。 应删除第一行新行。

val singleLineDataframe =  myDataframe.withColumn("Texts", regexp_replace(col("Texts"), "[\\r\\n\\n]", "."))
val sentenceList =   singleLineDataframe.select("Texts").rdd.map(r => r(0)).collect.toList
for ( element <- sentenceList)
      println(element)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM