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); }