首页 > 解决方案 > 将 SequenceFile 转换为 DataFrame

问题描述

我有一个 SequenceFile,我想使用 Pyspark 将其转换为 DataFrame。

为此,我使用以下代码:

seq_file = sc.sequenceFile("products_sequencefile")
df = prod_seq.map(lambda a: str(a).split(",")).map(lambda a: (a[0],a[1],a[2],a[3],a[4],a[5],a[6])).toDF()

但是,它给了我一个带有 'u:

+--------+-------+---+--------------------+---+------+--------------------+
|      _1|     _2| _3|                  _4| _5|    _6|                  _7|
+--------+-------+---+--------------------+---+------+--------------------+
|(u'1009'| u'1009| 45|Diamond Fear No E...|   |599.99|http://images.acm...|

我在做正确的方法吗?

标签: apache-sparkpysparksequencefile

解决方案


直接用toDF试试?

df = sc.sequenceFile("products_sequencefile").toDF('key', 'value')
ncols = 6    # set the ncols here as appropriate
df = df.select(
    'key',
    *[F.split(F.col('value'), ',')[i] for i in range(ncols)]
)

推荐阅读