Create a dataframe in pyspark that contains a sing

2019-03-05 05:38发布

问题:

I have an RDD that contains the following [('column 1',value), ('column 2',value), ('column 3',value), ... , ('column 100',value)]. I want to create a dataframe that contains a single column with tuples.

The closest I have gotten is:

schema = StructType((StructField("char", StringType(), False), (StructField("count", IntegerType(), False))))
    my_udf = udf(lambda w, c: (w,c), schema)

and then

df.select(my_udf('char', 'int').alias('char_int'))

but this produces a dataframe with a column of lists, not tuples.

回答1:

struct is a s correct way to represent product types, like tuple, in Spark SQL and this is exactly what you get using your code:

df = (sc.parallelize([("a", 1)]).toDF(["char", "int"])
    .select(my_udf("char", "int").alias("pair")))
df.printSchema()

## root
##  |-- pair: struct (nullable = true)
##  |    |-- char: string (nullable = false)
##  |    |-- count: integer (nullable = false)

There is no other way to represent a tuple unless you want to create an UDT (no longer supported in 2.0.0) or store pickled objects as BinaryType.

Moreover struct fields are locally represented as tuple:

isinstance(df.first().pair, tuple)
## True

I guess you may be confused by square brackets when you call show:

df.show()

## +-----+
## | pair|
## +-----+
## |[a,1]|
## +-----+

which are simply a representation of choice render by JVM counterpart and don't indicate Python types.