flink基于JDBC的Source案例

object JDBCSourceTest {
def main(args: Array[String]): Unit = {
val env = ExecutionEnvironment.getExecutionEnvironment
val inputMysql: DataSet[Row] = MyJDBCRead(env)
inputMysql.map(r=>(
r.getField(0),r.getField(1))).print()

}

//通过jdbc读取Mysql
def MyJDBCRead(env:ExecutionEnvironment) ={
val inputMysql = env.createInput(JDBCInputFormat.buildJDBCInputFormat()
//链接驱动名称
.setDrivername(“com.mysql.jdbc.Driver”)
//url
.setDBUrl(“jdbc:mysql://spark1:3306/company”)
.setUsername(“root”)
.setPassword(“root”)
.setQuery(“select id,name from staff”)
.setRowTypeInfo(new RowTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.DOUBLE_TYPE_INFO))
.finish()
)
inputMysql
}

}

你可能感兴趣的:(原创)