How to take two list to make dataframe via Spark Scala
Input:
val fruit = List("apple","mango","lemon")
val price = List(20,30,50)
Output:
Solution:
//Take two list
val fruit = List("apple","mango","lemon")
val price = List(20,30,50)
//make schema
val schema = StructType(Seq(StructField("Fruit",StringType,true),StructField("Price",IntegerType,true)))
//join both of them
val rows = fruit.zip(price).map(x=> Row(x._1,x._2))
//make rdd rows data
val rowdata = sc.parallelize(rows)
//make dataframe
val df = spark.createDataFrame(rowdata,schema)
//display the dataframe
df.show()
Comments
Post a Comment