Flink写Mysql
Flink 自带了JDBCOutputFormat来写入Mysql
FlinkKafkaConsumer011<String> consumer011 = new FlinkKafkaConsumer011<String>("flink_order", new SimpleStringSchema(), props);
DataStream<String> stream = env.addSource(consumer011);
stream.map(new MapFunction<String, Row>() {
@Override
public Row map(String s) throws Exception {
System.out.println(s);
Row row = new Row(3);
int va=1;
row.setField(0, va);
row.setField(1, va);
row.setField(2, va);
return row;
}
}).writeUsingOutputFormat(JDBCOutputFormat.buildJDBCOutputFormat()
.setDrivername("com.mysql.jdbc.Driver")
.setDBUrl("jdbc:mysql://xx")
.setUsername("xx")
.setPassword("xx")
.setQuery("insert into order_cnt(cnt,user,num) values(?,?,?)")
.setSqlTypes(new int[]{Types.INTEGER, Types.INTEGER, Types.INTEGER})
.finish());
try {
env.execute("flink-test");
} catch (Exception e) {
e.printStackTrace();
}