真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

Flink批處理之讀寫(xiě)Mysql

1、添加Maven坐標(biāo)


       MySQL
       mysql-connector-java
       5.1.48


 
        org.apache.flink
        flink-jdbc_2.12
         1.8.0
 

2、建表

CREATE TABLE `temp` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `time` varchar(255) DEFAULT NULL,
  `type` bigint(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

3、 Show Code

package com.fwmagic.flink.batch;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;
import org.apache.flink.api.java.io.jdbc.JDBCInputFormat;
import org.apache.flink.api.java.io.jdbc.JDBCOutputFormat;
import org.apache.flink.api.java.operators.DataSource;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.api.java.typeutils.RowTypeInfo;
import org.apache.flink.types.Row;

import java.util.concurrent.TimeUnit;

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

        String driverClass = "com.mysql.jdbc.Driver";
        String dbUrl = "jdbc:mysql://localhost:3306/test";
        String userNmae = "root";
        String passWord = "123456";
        String sql = "insert into test.temp (name,time,type) values (?,?,?)";

        ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        /**
         * 文件內(nèi)容:
         * 關(guān)羽,2019-10-14 00:00:01,1
         * 張飛,2019-10-14 00:00:02,2
         * 趙云,2019-10-14 00:00:03,3
         */

        String filePath = "/Users/temp/data.csv";

        //讀csv文件內(nèi)容,轉(zhuǎn)成Row對(duì)象
        DataSet outputData = env.readCsvFile(filePath).fieldDelimiter(",").types(String.class, String.class, Long.class).map(new MapFunction, Row>() {
            @Override
            public Row map(Tuple3 t) throws Exception {
                Row row = new Row(3);
                row.setField(0, t.f0.getBytes("UTF-8"));
                row.setField(1, t.f1.getBytes("UTF-8"));
                row.setField(2, t.f2.longValue());
                return row;
            }
        });

        //將Row對(duì)象寫(xiě)到mysql
        outputData.output(JDBCOutputFormat.buildJDBCOutputFormat()
                .setDrivername(driverClass)
                .setDBUrl(dbUrl)
                .setUsername(userNmae)
                .setPassword(passWord)
                .setQuery(sql)
                .finish());

        //觸發(fā)執(zhí)行
        env.execute("insert data to mysql");

        System.out.println("mysql寫(xiě)入成功!");

        TimeUnit.SECONDS.sleep(6);

        //讀mysql
        DataSource dataSource = env.createInput(JDBCInputFormat.buildJDBCInputFormat()
                .setDrivername(driverClass)
                .setDBUrl(dbUrl)
                .setUsername(userNmae)
                .setPassword(passWord)
                .setQuery("select * from temp")
                .setRowTypeInfo(new RowTypeInfo(BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.LONG_TYPE_INFO))
                .finish());

        //獲取數(shù)據(jù)并打印
        dataSource.map(new MapFunction() {
            @Override
            public String map(Row value) throws Exception {
                System.out.println(value);
                return value.toString();
            }
        }).print();

    }
}

4、注意事項(xiàng)

  • 數(shù)據(jù)寫(xiě)入mysql的DataSet泛型要求是row,需要轉(zhuǎn)換;
  • 數(shù)據(jù)讀取的結(jié)果也是row類型,不能直接print,需要轉(zhuǎn)換;
  • 數(shù)據(jù)寫(xiě)入后一定要加上env.execute(),觸發(fā)任務(wù)執(zhí)行;
  • 涉及到中文的,需要轉(zhuǎn)換成UTF-8,不然數(shù)據(jù)庫(kù)中會(huì)出現(xiàn)亂碼。

文章名稱:Flink批處理之讀寫(xiě)Mysql
URL網(wǎng)址:http://weahome.cn/article/jsopjp.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部