码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2019-05-16 21:37:44      阅读:1833      评论:0      收藏:0      [点我收藏+]

标签:arc   form   nal   for   return   stat   sel   tostring   user   

接着上一篇,将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中 用于搜索全量

标签:arc   form   nal   for   return   stat   sel   tostring   user   

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

(1)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!