I have a schema which is of type StructType:
val schema = getSchema(); // getSchema returns StructType
I created another field of type StructField:
StructField("NAME", StringType, false)
I know that I can call schema.add() method to add the NAME field to the end of the existing schema, but how do I add NAME to the beginning of the schema to make it the first column? Thanks.
You can create an auxiliary StructType
and then add your existing one to it:
val auxSchema=StructType(Array(StructField("NAME", StringType, false)))
StructType(auxSchema++schema)
This will work.
val schema = StructType(
StructField("string_column", StringType, true) ::
StructField("int_column", IntegerType, true) ::
StructField("float_column", FloatType, true) ::
StructField("_corrupt_record", StringType, true) :: Nil
)
val newschema = schema.add(StructField("new_column", StringType, true))
println(newschema)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With