I am trying to automate and load random data into a empty dataframe using spark scala
import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.Row
import org.apache.spark.rdd.RDD
val df = spark.sql("select * from test.test")
val emptyDF= spark.createDataFrame(spark.sparkContext.emptyRDD[Row], df.schema)
Here I am trying to create a empty dataframe with test table schema . In this case it is (id int, name string). I am trying to add a empty row to this dataframe.
val df2=Seq((1,2)).toDF("col1","col2")
emptyDF.union(df2)
But if I change the table name I have to do this operation manually in Seq(data) and toDF(columns),I want to change the code so that the data can be added by random and schema should infer from table, like example as below
val columninfo = "\""+emptyDF.columns.mkString("\",\"")+"\""
val columncount = emptyDF.columns.size
val x = (1 to columncount).toList.mkString(",")
var df1=Seq(x).toDF(columninfo)
But Its not working , Please let me know if there is any otherway to append the random data to the empty dataframe or how to automate the above operation, or any other approach with is suitable. Thanks in advance