用Kettle将csv文件导入至数据库表中,简单流程如下:
建立好【CSV文件输入】->【插入/更新】转换后,进行测试,运行报如下错误:
2019/07/16 15:01:45 - 插入 / 更新.0 - ERROR (version 8.3.0.0-371, build 8.3.0.0-371 from 2019-06-11 11.09.08 by buildguy) : Error in step, asking everyone to stop because of:
2019/07/16 15:01:45 - 插入 / 更新.0 - ERROR (version 8.3.0.0-371, build 8.3.0.0-371 from 2019-06-11 1.09.08 by buildguy) : org.pentaho.di.core.exception.KettleDatabaseException:
2019/07/16 15:01:45 - 插入 / 更新.0 - Error inserting/updating row
2019/07/16 15:01:45 - 插入 / 更新.0 - Data truncation: Incorrect datetime value: '16/7/2019 14:28:32' for column 'create_time' at row 1
2019/07/16 15:01:45 - 插入 / 更新.0 -
2019/07/16 15:01:45 - 插入 / 更新.0 - at org.pentaho.di.core.database.Database.insertRow(Database.java:1324)
2019/07/16 15:01:45 - 插入 / 更新.0 - at org.pentaho.di.core.database.Database.insertRow(Database.java:1248)
2019/07/16 15:01:45 - 插入 / 更新.0 - at org.pentaho.di.core.database.Database.insertRow(Database.java:1236)
2019/07/16 15:01:45 - 插入 / 更新.0 - at org.pentaho.di.core.database.Database.insertRow(Database.java:1224)
2019/07/16 15:01:45 - 插入 / 更新.0 - at org.pentaho.di.trans.steps.insertupdate.InsertUpdate.lookupValues(InsertUpdate.java:114)
2019/07/16 15:01:45 - 插入 / 更新.0 - at org.pentaho.di.trans.steps.insertupdate.InsertUpdate.processRow(InsertUpdate.java:299)
2019/07/16 15:01:45 - 插入 / 更新.0 - at org.pentaho.di.trans.step.RunThread.run(RunThread.java:62)
2019/07/16 15:01:45 - 插入 / 更新.0 - at java.lang.Thread.run(Thread.java:748)
2019/07/16 15:01:45 - 插入 / 更新.0 - Caused by: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '16/7/2019 14:28:32' for column 'create_time' at row 1
2019/07/16 15:01:45 - 插入 / 更新.0 - at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3968)
2019/07/16 15:01:45 - 插入 / 更新.0 - at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
2019/07/16 15:01:45 - 插入 / 更新.0 - at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
2019/07/16 15:01:45 - 插入 / 更新.0 - at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
2019/07/16 15:01:45 - 插入 / 更新.0 - at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
2019/07/16 15:01:45 - 插入 / 更新.0 - at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
2019/07/16 15:01:45 - 插入 / 更新.0 - at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
2019/07/16 15:01:45 - 插入 / 更新.0 - at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
2019/07/16 15:01:45 - 插入 / 更新.0 - at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
2019/07/16 15:01:45 - 插入 / 更新.0 - at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
2019/07/16 15:01:45 - 插入 / 更新.0 - at org.pentaho.di.core.database.Database.insertRow(Database.java:1291)
2019/07/16 15:01:45 - 插入 / 更新.0 - ... 7 more
遂查看字段数据类型,发现create_time字段为String类型,改为Date类型后继续测试,再次报错:
2019/07/16 15:10:14 - 插入 / 更新.0 - create_time String : couldn't convert string [16/7/2019 14:28:32] to a date using format [yyyy/MM/dd HH:mm:ss.SSS] on offset location 18
2019/07/16 15:10:14 - 插入 / 更新.0 - 16/7/2019 14:28:32
2019/07/16 15:10:14 - 插入 / 更新.0 - at org.pentaho.di.core.database.Database.setValues(Database.java:1081)
2019/07/16 15:10:14 - 插入 / 更新.0 - at org.pentaho.di.trans.steps.insertupdate.InsertUpdate.lookupValues(InsertUpdate.java:83)
2019/07/16 15:10:14 - 插入 / 更新.0 - at org.pentaho.di.trans.steps.insertupdate.InsertUpdate.processRow(InsertUpdate.java:299)
2019/07/16 15:10:14 - 插入 / 更新.0 - at org.pentaho.di.trans.step.RunThread.run(RunThread.java:62)
2019/07/16 15:10:14 - 插入 / 更新.0 - at java.lang.Thread.run(Thread.java:748)
可以看出String转成Date后时间格式不正确,这里通过【转换】中的【字段选择】功能来修改时间格式。
添加【字段选择】步骤后双击编辑,点击元数据,在行中添加需要改变元数据的字段,选择字段类型为Date(输入端字段类型不要修改),设置时间格式,点击确定。
再次运行,csv文件数据成功导入数据库。