FlinkSql 读取kafka sink到mysql 案例

 public static void main(String[] args) {


        StreamExecutionEnvironment bsEnv = StreamExecutionEnvironment.getExecutionEnvironment();
        bsEnv.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        EnvironmentSettings bsSettings = EnvironmentSettings.newInstance().useBlinkPlanner().inStreamingMode().build();
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(bsEnv, bsSettings);


        Kafka kafka = new Kafka()
                .version("universal")
                .topic("flink_test")
                .property("zookeeper.connect", "localhost:2181")
                .property("bootstrap.servers", "localhost:9092")
                .startFromLatest();

        tableEnv.connect(kafka)
                .withFormat(
                        new Json().failOnMissingField(true).deriveSchema()
                )

                .withSchema(
                        new Schema()
                                .field("name", Types.STRING)
                                .field("deptno", Types.STRING)
                                .field("amount", Types.INT)
                               // .field("ptime",Types.SQL_TIMESTAMP)

                )
                .inAppendMode()
                .registerTableSource("Orders");





        Table testTable = tableEnv.sqlQuery("select  ,amount as num from Orders  ");
        tableEnv.toAppendStream(testTable,Row.class).print();

        JDBCAppendTableSink tableSink = JDBCAppendTableSink.builder()
                .setDBUrl("jdbc:mysql://101.191.233.18:3306/dataservice?useUnicode=true&characterEncoding=UTF-8")
                .setDrivername("com.mysql.jdbc.Driver")
                .setUsername("")
                .setPassword("")
                .setQuery("insert into flink_test(name,num) values(?,?)")
                .setBatchSize(1)
                .setParameterTypes(STRING_TYPE_INFO,INT_TYPE_INFO).build();
        TypeInformation[] fieldTypes = {Types.STRING, Types.INT};
        tableEnv.registerTableSink("jdbcSink",new String[]{"name","num"},fieldTypes,tableSink);
        testTable.insertInto("jdbcSink");
        try {
//            table.insertInto("csvOutputTable");
            tableEnv.execute("aa");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

你可能感兴趣的:(flink)