如何在流水线RDD上使用flatMap()?

时间:2016-03-30 03:14:15

标签: python apache-spark pyspark rdd

我有一个名为'all_tweets'的sql数据帧,它只有一个列文本。

%= form_for @product, :html => {:multipart => true}  do |p| %>
  <div class="block margin_top">

 <%= p.fields_for(:images,  html: { multipart: true, id: "images",class: "dropzone"}) do |i| %>
     <div class="fallback">

            <%= i.file_field :photo %><br>
            <%= i.submit "Save" %>
          </div>
    <% end %>
</div>

现在,我正在将此数据帧转换为RDD,以对其执行一些转换和操作。

all_tweets.show(5)

+--------------------+
|                text|
+--------------------+
|@always_nidhi @Yo...|
|@OnlyDancers Bell...|
|Taste of Iceland!...|
|Serasi ade haha @...|
|@BeezyDH_ it's li...|
+--------------------+
only showing top 5 rows

现在,我正在尝试在其上运行flatMap,将句子分成单词。

[I] all_twt_rdd = all_tweets.rdd

[I] type(all_twt_rdd)

[O] pyspark.rdd.RDD

[I] all_twt_rdd.first()

[O] Row(text=u'@always_nidhi @YouTube no i dnt understand bt i loved the music nd their dance awesome all the song of this mve is rocking')

当我运行上述内容时,我收到以下错误。 RDD有数据。但我不明白为什么它会出错。流水线RDD是否继承了RDD的功能?

[I] user_cnt = all_twt_rdd.flatMap(lambda line: line.split(" ")).take(5)

1 个答案:

答案 0 :(得分:2)

问题是您在.split()上调用Row,而不是字符串。行对象没有.split()方法 - 只有字符串。您想要拆分其text属性,因此请明确调用它:

user_cnt = all_twt_rdd.flatMap(lambda line: line.text.split(" ")).take(5)
相关问题