Exploser dans PySpark

Je voudrais transformer à partir d'un DataFrame qui contient des listes de mots dans un DataFrame avec chaque mot dans sa propre ligne.

Comment dois-je faire exploser sur une colonne dans un DataFrame?

Voici un exemple avec certains de mes tentatives où vous pouvez dé-commenter chaque ligne de code et obtenez le message d'erreur répertoriés dans le commentaire suivant. J'utilise PySpark en Python 2.7 avec Spark 1.6.1.

from pyspark.sql.functions import split, explode
DF = sqlContext.createDataFrame([('cat \n\n elephant rat \n rat cat', )], ['word'])
print 'Dataset:'
DF.show()
print '\n\n Trying to do explode: \n'
DFsplit_explode = (
 DF
 .select(split(DF['word'], ' '))
#  .select(explode(DF['word']))  # AnalysisException: u"cannot resolve 'explode(word)' due to data type mismatch: input to function explode should be array or map type, not StringType;"
#   .map(explode)  # AttributeError: 'PipelinedRDD' object has no attribute 'show'
#   .explode()  # AttributeError: 'DataFrame' object has no attribute 'explode'
).show()

# Trying without split
print '\n\n Only explode: \n'

DFsplit_explode = (
 DF 
 .select(explode(DF['word']))  # AnalysisException: u"cannot resolve 'explode(word)' due to data type mismatch: input to function explode should be array or map type, not StringType;"
).show()

S'il vous plaît conseils

OriginalL'auteur user1982118 | 2016-07-05