简体   繁体   中英

spark.createDataFrame () not working with Seq RDD

CreateDataFrame takes 2 arguments , an rdd and schema.

my schema is like this

val schemas= StructType( Seq( StructField("number",IntegerType,false), StructField("notation", StringType,false) ) )

in one case i am able to create dataframe from RDD like below:

`val data1=Seq(Row(1,"one"),Row(2,"two"))

val rdd=spark.sparkContext.parallelize(data1)

val final_df= spark.createDataFrame(rdd,schemas)`

In other case like below .. i am not able to

`val data2=Seq((1,"one"),(2,"two"))

val rdd=spark.sparkContext.parallelize(data2)

val final_df= spark.createDataFrame(rdd,schemas)`

Whats wrong with data2 for not able to become a valid rdd for Dataframe?

but we can able to create dataframe using toDF() with data2 but not CreateDataFrame.

val data2_DF=Seq((1,"one"),(2,"two")).toDF("number", "notation")

Please help me understand this behaviour.

Is Row mandatory while creating dataframe?

In the second case, just do :

val final_df = spark.createDataFrame(rdd)

Because your RDD is an RDD of Tuple2 (which is a Product ), the schema is known at compile time, so you don't need to specify a schema

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM