一、读MySQL
1、通过JDBC方式定义MySQLDataSource类
1.1首先加入JDBC依赖
1.2定义JDBCInputFormat
1.3获取Row类型的DataStreamSource
1.4转化DataStream为DataStream
public class MysqlDataSource {
private static final Logger log = LoggerFactory.getLogger(MySQLDataSource.class);
public static DataStream readFromDb(StreamExecutionEnvironment streamExecutionEnvironment) throws Exception {
//final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//1.定义field 类型
TypeInformation[] fieldTypes = new TypeInformation[]{BasicTypeInfo.INT_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO};
//2.定义field name
String[] fieldNames = new String[]{"id", "name", "password", "age"};
//3.定义Row类型
RowTypeInfo rowTypeInfo = new RowTypeInfo(fieldTypes, fieldNames);
String jdbcUrl = parameterTool.get(PropertiesConstants.MYSQL_JDBC_URL);
log.info(jdbcUrl);
//4.定义JDBCInputFormat
JDBCInputFormat jdbcInputFormat = JDBCInputFormat
.buildJDBCInputFormat()
.setDrivername("com.mysql.jdbc.Driver")
.setDBUrl(jdbcUrl)
.setUsername(parameterTool.get(PropertiesConstants.MYSQL_USERNAME))
.setPassword(parameterTool.get(PropertiesConstants.MYSQL_PASSWORD))
.setQuery("select id, name, password, age from student")
.setRowTypeInfo(rowTypeInfo)
.finish();
//5.以JDBCInputFormat形式读取MySQL DB数据
DataStreamSource dataStreamSourceRow = streamExecutionEnvironment.createInput(jdbcInputFormat);
//阶段性验证可以正确读取
dataStreamSourceRow.print();
//6.将Row类型Stream转化为Entity类型
DataStream dataStream = dataStreamSourceRow.map(new RichMapFunction() {
@Override
public Student map(Row value) throws Exception {
Student s = new Student();
s.setId((Integer) value.getField(0));
s.setName((String) value.getField(1));
<

本文详细介绍了Flink如何读写MySQL数据库,包括通过JDBC方式定义MySQLDataSource类进行读操作,自定义DataSource方式实现数据读取,以及通过JDBC和自定义Sink方式实现数据写入。对比了JDBC与自定义方式的优缺点。
最低0.47元/天 解锁文章
9333

被折叠的 条评论
为什么被折叠?



