Hardik Gupta
Hardik Gupta

Reputation: 4790

read content of Column<COLUMN-NAME> in pyspark

I am using spark 1.5.0

I have a data frame created like below, and am trying to read a column from here

>>> words = tokenizer.transform(sentenceData)
>>> words
DataFrame[label: bigint, sentence: string, words: array<string>]
>>> words['words']
Column<words>

I want to read all the words (vocab) from the sentences. How can I read this

Edit 1: Error Still Prevails

I now ran this in spark 2.0.0 and getting this error

>>> wordsData.show()
+--------------------+--------------------+
|                desc|               words|
+--------------------+--------------------+
|Virat is good bat...|[virat, is, good,...|
|     sachin was good| [sachin, was, good]|
|but modi sucks bi...|[but, modi, sucks...|
| I love the formulas|[i, love, the, fo...|
+--------------------+--------------------+

>>> wordsData
DataFrame[desc: string, words: array<string>]


>>> vocab = wordsData.select(explode('words')).rdd.flatMap(lambda x: x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/BIG-DATA/spark-2.0.0-bin-hadoop2.7/python/pyspark/rdd.py", line 305, in flatMap
    return self.mapPartitionsWithIndex(func, preservesPartitioning)
  File "/opt/BIG-DATA/spark-2.0.0-bin-hadoop2.7/python/pyspark/rdd.py", line 330, in mapPartitionsWithIndex
    return PipelinedRDD(self, f, preservesPartitioning)
  File "/opt/BIG-DATA/spark-2.0.0-bin-hadoop2.7/python/pyspark/rdd.py", line 2383, in __init__
    self._jrdd_deserializer = self.ctx.serializer
AttributeError: 'SparkSession' object has no attribute 'serializer'

Resolution for Edit - 1 - Link

Upvotes: 0

Views: 278

Answers (1)

user7329627
user7329627

Reputation: 26

You can:

from pyspark.sql.functions import explode

words.select(explode('words')).rdd.flatMap(lambda x: x)

Upvotes: 1

Related Questions