org.apache.flink.table.api.TableException: Sink does not exists

FlinkSQL_1.12_用DDL实现Kafka到MySQL的数据传输_实现按照条件进行过滤写入MySQL_flink从kafka拉取数据并过滤数据写入mysql_旧城里的阳光的博客-CSDN博客

参考这篇文章,写了kafka到mysql的代码例子,因为自己改了表结构,运行下面代码:

java 复制代码
package org.test.flink;
 
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
 
//TODO 用DDL实现Kafka到MySQL的数据传输
public class FlinkSQL15_SQL_DDL_Kafka_MySQL {
    public static void main(String[] args) throws Exception {
        //1.获取执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
 
        //2.使用DDL的方式加载数据--注册SourceTable
        tableEnv.executeSql("create table source_sensor(account_id  BIGINT)" +
                "with (" +
                "'connector.type' = 'kafka'," +
                "'connector.version' = 'universal'," +
                "'connector.topic' = 'testtopic'," +
                "'connector.properties.bootstrap.servers' = '11.0.24.216:9092'," +
                "'connector.properties.group.id' = 'bigdata1109'," +
                "'format.type' = 'json'"
                + ")");
 
        Table table = tableEnv.sqlQuery("select * from source_sensor");
 
        //3.注册SinkTable:Mysql
        tableEnv.executeSql("CREATE TABLE spend_report (\n" +
                "    account_id BIGINT,\n" +
                "    PRIMARY KEY (account_id) NOT ENFORCED)" +
                "with (" +
                "'connector' = 'jdbc'," +
                "'url' = 'jdbc:mysql://11.0.24.216:4306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false',"+
                "'table-name' = 'spend_report',"+
                "'username' = 'root',"+
                "'password' = '123456'"
                + ")");
 
       //4.执行查询kafka数据
//        Table source_sensor = tableEnv.from("source_sensor");
//        //5.将数据写入Mysql
//        source_sensor.executeInsert("sink_sensor");
//
        table.executeInsert("sink_sensor");
 
        //6.执行任务
        env.execute();
    }
}

发现报错如下:

java 复制代码
Exception in thread "main" org.apache.flink.table.api.TableException: Sink `default_catalog`.`default_database`.`sink_sensor` does not exists
	at org.apache.flink.table.planner.delegation.PlannerBase.translateToRel(PlannerBase.scala:247)
	at org.apache.flink.table.planner.delegation.PlannerBase.$anonfun$translate$1(PlannerBase.scala:159)
	at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:286)
	at scala.collection.Iterator.foreach(Iterator.scala:943)
	at scala.collection.Iterator.foreach$(Iterator.scala:943)
	at scala.collection.AbstractIterator.foreach(Iterator.scala:1431)
	at scala.collection.IterableLike.foreach(IterableLike.scala:74)
	at scala.collection.IterableLike.foreach$(IterableLike.scala:73)
	at scala.collection.AbstractIterable.foreach(Iterable.scala:56)
	at scala.collection.TraversableLike.map(TraversableLike.scala:286)
	at scala.collection.TraversableLike.map$(TraversableLike.scala:279)
	at scala.collection.AbstractTraversable.map(Traversable.scala:108)
	at org.apache.flink.table.planner.delegation.PlannerBase.translate(PlannerBase.scala:159)
	at org.apache.flink.table.api.internal.TableEnvironmentImpl.translate(TableEnvironmentImpl.java:1329)
	at org.apache.flink.table.api.internal.TableEnvironmentImpl.executeInternal(TableEnvironmentImpl.java:676)
	at org.apache.flink.table.api.internal.TableImpl.executeInsert(TableImpl.java:572)
	at org.apache.flink.table.api.internal.TableImpl.executeInsert(TableImpl.java:554)
	at org.test.flink.FlinkSQL15_SQL_DDL_Kafka_MySQL.main(FlinkSQL15_SQL_DDL_Kafka_MySQL.java:50)

点击table.executeInsert看了下源码:

java 复制代码
    /**
     * Writes the {@link Table} to a {@link TableSink} that was registered under the specified path,
     * and then execute the insert operation.
     *
     * <p>See the documentation of {@link TableEnvironment#useDatabase(String)} or {@link
     * TableEnvironment#useCatalog(String)} for the rules on the path resolution.
     *
     * <p>A batch {@link Table} can only be written to a {@code
     * org.apache.flink.table.sinks.BatchTableSink}, a streaming {@link Table} requires a {@code
     * org.apache.flink.table.sinks.AppendStreamTableSink}, a {@code
     * org.apache.flink.table.sinks.RetractStreamTableSink}, or an {@code
     * org.apache.flink.table.sinks.UpsertStreamTableSink}.
     *
     * <p>Example:
     *
     * <pre>{@code
     * Table table = tableEnv.fromQuery("select * from MyTable");
     * TableResult tableResult = table.executeInsert("MySink");
     * tableResult...
     * }</pre>
     *
     * @param tablePath The path of the registered TableSink to which the Table is written.
     * @return The insert operation execution result.
     */
    TableResult executeInsert(String tablePath);

发现executeInsert方法的参数tablePath需要传入表名,这里的表名应该和

java 复制代码
tableEnv.executeSql("create table source_sensor(account_id  BIGINT)"

的表名source_sensor一致。

将:

java 复制代码
table.executeInsert("sink_sensor");

改成:

java 复制代码
table.executeInsert("source_sensor");

后执行成功。

flink1.2的demo完整代码:flink-java-1.12.7: flink1.12.7的java demo,包括flink wordcount示例,如何连接kafka

相关推荐
计算机毕设定制辅导-无忧学长11 小时前
TDengine 权限管理与安全配置实战(二)
大数据·安全·tdengine
2401_8979300611 小时前
Kibana 连接 Elasticsearch(8.11.3)教程
大数据·elasticsearch·jenkins
计算机毕设定制辅导-无忧学长11 小时前
TDengine 快速上手:安装部署与基础 SQL 实践(一)
大数据·sql·tdengine
塔能物联运维11 小时前
塔能科技:精准节能,擎动工厂可持续发展巨轮
大数据·运维
今天我又学废了12 小时前
Spark,HDFS概述
大数据·hdfs·spark
ps酷教程13 小时前
Apache httpclient & okhttp(2)
okhttp·apache
青云交13 小时前
Java 大视界 -- 基于 Java 的大数据机器学习模型在图像识别中的迁移学习与模型优化(173)
大数据·迁移学习·图像识别·模型优化·deeplearning4j·机器学习模型·java 大数据
Yan-英杰14 小时前
DeepSeek-R1模型现已登录亚马逊云科技
java·大数据·人工智能·科技·机器学习·云计算·deepseek
黄雪超14 小时前
Flink介绍——实时计算核心论文之Storm论文总结
大数据·论文阅读·storm
TDengine (老段)14 小时前
TDengine 中的日志系统
java·大数据·数据库·物联网·时序数据库·tdengine·iotdb