首页 > 数据库技术 > 详细

flink 从mysql 读取数据 放入kafka中 用于搜索全量

时间:2019-05-16 21:05:57      阅读:2039      评论:0      收藏:0      [点我收藏+]
接着上一篇,将mysql的数据导入kafka中

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

        TypeInformation[] fieldTypes = new TypeInformation[] { BasicTypeInfo.STRING_TYPE_INFO };

        RowTypeInfo rowTypeInfo = new RowTypeInfo(fieldTypes);
        JDBCInputFormat jdbcInputFormat = JDBCInputFormat.buildJDBCInputFormat().setDrivername("com.mysql.jdbc.Driver")
                .setDBUrl("jdbc:mysql://*:3306/tablename?characterEncoding=utf8")
                .setUsername("*").setPassword("*")
                .setQuery("select LOGIC_CODE from *").setRowTypeInfo(rowTypeInfo).finish();

        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
        DataSource<Row> s = env.createInput(jdbcInputFormat);
        BatchTableEnvironment tableEnv = new BatchTableEnvironment(env, TableConfig.DEFAULT());
        tableEnv.registerDataSet("t2", s);
        Table query = tableEnv.sqlQuery("select * from t2");
        DataSet<String> result = tableEnv.toDataSet(query, Row.class).map(new MapFunction<Row, String>(){   
            @Override
              public String map(Row value) throws Exception {
                return value.toString() ;
              }

        });

        logger.info("read db end"); 

        KafkaOutputFormat kafkaOutput = KafkaOutputFormat.buildKafkaOutputFormat()
                .setBootstrapServers("*:9092").setTopic("search_test_whk").setAcks("all").setBatchSize("1000")
                .setBufferMemory("100000").setLingerMs("1").setRetries("2").finish();

        result.output(kafkaOutput);

        logger.info("write kafka end");

        env.execute("Flink add data source");

    }

flink 从mysql 读取数据 放入kafka中 用于搜索全量

原文:https://blog.51cto.com/12597095/2395983

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!