flinksql(1) kafka-join-pg

pom
<properties>
    <!-- 声明版本号,以后统一在这里进行管理-->
    <flink.version>1.14.4</flink.version>
    <scala.version>2.11</scala.version>
    <postgresql.version>42.3.0</postgresql.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-java</artifactId>
        <version>${flink.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-streaming-java_${scala.version}</artifactId>
        <version>${flink.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-clients_${scala.version}</artifactId>
        <version>${flink.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-table-api-java-bridge_${scala.version}</artifactId>
        <version>${flink.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-table-planner_${scala.version}</artifactId>
        <version>${flink.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-connector-kafka_${scala.version}</artifactId>
        <version>${flink.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-json</artifactId>
        <version>${flink.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.flink</groupId>
        <artifactId>flink-connector-jdbc_${scala.version}</artifactId>
        <version>${flink.version}</version>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>${postgresql.version}</version>
    </dependency>
</dependencies>



public static void main(String[] args) throws Exception {
    StreamExecutionEnvironment execEnv = StreamExecutionEnvironment.getExecutionEnvironment();
   TableEnvironment tableEnv = StreamTableEnvironment.create(execEnv);
    String KafkaTable = "CREATE TABLE KafkaTable (\n" +
            "  `id` BIGINT,\n" +
            "  `name` STRING,\n" +
            "  `age` INT \n" +
            ") WITH (\n" +
            "  'connector' = 'kafka',\n" +
            "  'topic' = 'flz01',\n" +
            "  'properties.bootstrap.servers' = '192.168.191.131:9092',\n" +
            "  'properties.group.id' = 'flz_group_1',\n" +
            "  'scan.startup.mode' = 'latest-offset',\n" +
            "  'json.ignore-parse-errors' = 'true',\n" +
            "  'format' = 'json'\n" +
            ")";

    String test1 = "CREATE TABLE test1 (\n" +
            "  id BIGINT,\n" +
            "  name STRING,\n" +
            "  age INT \n" +
            ") WITH (\n" +
            "   'connector' = 'jdbc',\n" +
            "   'username' = 'postgres',\n" +
            "   'password' = 'postgres',\n" +
            "   'driver' = 'org.postgresql.Driver',\n" +
            "   'url' = 'jdbc:postgresql://localhost:5432/postgres',\n" +
            "   'table-name' = 'test1'\n" +
            ")";

    String test2 = "CREATE TABLE test2 (\n" +
            "  id BIGINT,\n" +
            "  name STRING,\n" +
            "  age INT, \n" +
            "  PRIMARY KEY (id) NOT ENFORCED \n" +
            ") WITH (\n" +
            "   'connector' = 'jdbc',\n" +
            "   'username' = 'postgres',\n" +
            "   'password' = 'postgres',\n" +
            "   'driver' = 'org.postgresql.Driver',\n" +
            "   'url' = 'jdbc:postgresql://localhost:5432/postgres',\n" +
            "   'table-name' = 'test2'\n" +
            ")";
    String InsertTable = "insert into test2 \n" +
            " select a.id,a.name,b.age from KafkaTable a left join test1 b on a.name= b.name \n" ;
    tableEnv.executeSql(KafkaTable);
    tableEnv.executeSql(test1);
    tableEnv.executeSql(test2);
    // MyUserTable表要有PRIMARY KEY 不然报错
    tableEnv.executeSql(InsertTable);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值