Insert DataFrame into SQL table with AUTO_INCREMENT column

I have a MySQL table that contains a column AUTO_INCREMENT

:

CREATE TABLE features (
  id INT NOT NULL AUTO_INCREMENT,
  name CHAR(30),
  value DOUBLE PRECISION
);

      

I created DataFrame

and wanted to insert it into this table.

case class Feature(name: String, value: Double)
val rdd: RDD[Feature]
val df = rdd.toDF()
df.write.mode(SaveMode.Append).jdbc("jdbc:mysql://...", "features", new Properties)

      

I get an error Column count doesn’t match value count at row 1

. If I remove the column id

it works. How can I insert this data into a table without changing the schema?

+3


source to share


1 answer


You must include the field id

in the DataFrame, but its value will be ignored and replaced with an auto-incrementing ID. I.e:

case class Feature(id: Int, name: String, value: Double)

      



Then just set id

to 0 or any number on creation Feature

.

+2


source







All Articles