Flink 1.2.0 jdbc从Mysql读取流数据

问题描述 投票:2回答:1

我试图使用Flink 2.1.0从mysql日志表中读取流数据,但是,它只读取一次然后它将停止进程。如果有传入的数据并打印它我想它继续读取。以下是我的代码

public class Database {

    public static void main(String[] args) throws Exception {

        // get the execution environment
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        TypeInformation[] fieldTypes = new TypeInformation[] { LONG_TYPE_INFO, STRING_TYPE_INFO };
        RowTypeInfo rowTypeInfo = new RowTypeInfo(fieldTypes);

        DataStreamSource source = env.createInput(
            JDBCInputFormat.buildJDBCInputFormat()
                    .setDrivername("com.mysql.jdbc.Driver")
                    .setDBUrl("jdbc:mysql://localhost/log_db")
                    .setUsername("root")
                    .setPassword("pass")
                    .setQuery("select id, SERVER_NAME from ERRORLOG")
                    .setRowTypeInfo(rowTypeInfo)
                    .finish()
        );
        source.print().setParallelism(1);
        env.execute("Error Log Data");
    }
}

我正在使用本地内部运行与maven:

mvn exec:java -Dexec.mainClass=com.test.Database

结果:

09:15:56,394 INFO  org.apache.flink.runtime.taskmanager.Task                     - Freeing task resources for Source: Custom Source (1$
4) (41c66a6dfb97e1d024485f473617a342).
09:15:56,394 INFO  org.apache.flink.core.fs.FileSystem                           - Ensuring all FileSystem streams are closed for Sour$
e: Custom Source (1/4)
09:15:56,394 INFO  org.apache.flink.runtime.taskmanager.Task                     - Sink: Unnamed (1/1) (5212fc2a570152c58ffe3d39d3d805$
0) switched from RUNNING to FINISHED.
09:15:56,394 INFO  org.apache.flink.runtime.taskmanager.Task                     - Freeing task resources for Sink: Unnamed (1/1) (521$
fc2a570152c58ffe3d39d3d805b0).
09:15:56,394 INFO  org.apache.flink.runtime.taskmanager.TaskManager              - Un-registering task and sending final execution sta$
e FINISHED to JobManager for task Source: Custom Source (41c66a6dfb97e1d024485f473617a342)
09:15:56,396 INFO  org.apache.flink.runtime.executiongraph.ExecutionGraph        - Source: Custom Source (1/4) (41c66a6dfb97e1d024485f$
73617a342) switched from RUNNING to FINISHED.
09:15:56,396 INFO  org.apache.flink.runtime.client.JobSubmissionClientActor      - 02/22/2017 09:15:56  Source: Custom Source(1/4) swi$
ched to FINISHED 
02/22/2017 09:15:56     Source: Custom Source(1/4) switched to FINISHED 
09:15:56,396 INFO  org.apache.flink.core.fs.FileSystem                           - Ensuring all FileSystem streams are closed for Sink$
 Unnamed (1/1)
09:15:56,397 INFO  org.apache.flink.runtime.taskmanager.TaskManager              - Un-registering task and sending final execution sta$
e FINISHED to JobManager for task Sink: Unnamed (5212fc2a570152c58ffe3d39d3d805b0)
09:15:56,398 INFO  org.apache.flink.runtime.executiongraph.ExecutionGraph        - Sink: Unnamed (1/1) (5212fc2a570152c58ffe3d39d3d805$
0) switched from RUNNING to FINISHED.
09:15:56,398 INFO  org.apache.flink.runtime.executiongraph.ExecutionGraph        - Job Socket Window Data (0eb15d61031ede785e7ed21ead2$
ceea) switched from state RUNNING to FINISHED.
09:15:56,398 INFO  org.apache.flink.runtime.client.JobSubmissionClientActor      - 02/22/2017 09:15:56  Sink: Unnamed(1/1) switched to 
FINISHED 
02/22/2017 09:15:56     Sink: Unnamed(1/1) switched to FINISHED 
09:15:56,405 INFO  org.apache.flink.runtime.checkpoint.CheckpointCoordinator     - Stopping checkpoint coordinator for job 0eb15d61031$
de785e7ed21ead21ceea
09:15:56,406 INFO  org.apache.flink.runtime.client.JobSubmissionClientActor      - Terminate JobClientActor.
09:15:56,406 INFO  org.apache.flink.runtime.client.JobClient                     - Job execution complete
09:15:56,408 INFO  org.apache.flink.runtime.minicluster.FlinkMiniCluster         - Stopping FlinkMiniCluster.
09:15:56,405 INFO  org.apache.flink.runtime.checkpoint.StandaloneCompletedCheckpointStore  - Shutting down
java mysql jdbc apache-flink flink-streaming
1个回答
1
投票

启动查询时,mysql的表数据是固定的,因此作业应该是一个flink批处理作业。

如果要在传入数据时读取传入数据,则flink无法处理此类情况,因为flink不知道传入数据,除非您监视binlog。

你必须使用canal来将binlog从mysql同步到kafka,然后运行flink流工作从kafka读取数据。这是最好的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.