If you want to create a hive table(which does not exist) from a dataframe (some times it fails to create with
DataFrameWriter.saveAsTable
).StructType.toDDL
will helps in listing the columns as a string.
val df = ...
val schemaStr = df.schema.toDDL # This gives the columns
spark.sql(s"""create table hive_table ( ${schemaStr})""")
//Now write the dataframe to the table
df.write.saveAsTable("hive_table")
hive_table
will be created in default space since we did not provide any database at spark.sql()
. stg.hive_table
can be used to create hive_table
in stg
database.