apache-sparkpysparksequencefile

Convert SequenceFile as DataFrame


I have a SequenceFile that I want to convert as DataFrame using Pyspark.

For that I am using the following code:

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()

However, it gives me an output with some values with 'u:

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

Am I doing the correct approach?


Solution

  • Try to use toDF directly?

    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)]
    )