flink-jdbc-driver

Flink JDBC 驱动程序是一个 Java 库,使客户端能够通过 SQL 网关将 Flink SQL 发送到 Flink 集群。

首先启动:1.flink集群,随意任何集群。

2.启动flink-sql-gateway:

XML 复制代码
sql-gateway.sh start -Dsql-gateway.endpoint.rest.address=localhost

验证sql- gateway

复制代码
curl http://localhost:8083/v1/info
  1. Maven 依赖项

    Flink JDBC 驱动是一个通过 JDBC API 访问 Flink 集群的库。有关 Java 中 JDBC 的一般用法,请参见 JDBC 教程

  2. 在项目的pom.xml中添加以下依赖项,或者下载 flink-jdbc-driver-bundle-{VERSION}.jar 并将其添加到你的 Classpath 中。

    <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-sql-jdbc-driver-bundle</artifactId> <version>${flink.version}</version> </dependency>
  3. 使用特定 URL 连接到 Java 代码中的 Flink SQL 网关。

  4. 执行您想要的任何语句。

java 复制代码
public class Main {
    public static void main(String[] args) throws SQLException {
        Properties properties = new Properties();
        properties.setProperty("table.exec.sink.not-null-enforcer","DROP");
        DataSource dataSource = new FlinkDataSource("jdbc:flink://ip:8083", properties);
        try (Connection connection = dataSource.getConnection()) {
            try (Statement statement = connection.createStatement()) {

                /*statement.execute("CREATE TABLE student (\n" +
                        "  id VARCHAR(255),\n" +
                        "  sid VARCHAR(255),\n" +
                        "  name VARCHAR(255),\n" +
                        "  PRIMARY KEY (id) NOT ENFORCED" +
                        ") WITH (\n" +
                        "   'connector' = 'jdbc',\n" +
                        "   'url' = 'jdbc:mysql://ip:3306/test?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true',\n" +
                        "   'username' = 'test',\n" +
                        "   'password' = 'test@123',\n " +
                        "   'table-name' = 'student' " +
                        ")");
                statement.execute("CREATE TABLE students (\n" +
                        "  id VARCHAR(255),\n" +
                        "  sid VARCHAR(255),\n" +
                        "  name VARCHAR(255),\n" +
                        "  PRIMARY KEY (id) NOT ENFORCED" +
                        ") WITH (\n" +
                        "   'connector' = 'jdbc',\n" +
                        "   'url' = 'jdbc:mysql://ip:3306/test?characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true',\n" +
                        "   'username' = 'test',\n" +
                        "   'password' = 'test@123',\n " +
                        "   'table-name' = 'students' " +
                        ")");*/

                statement.execute("CREATE TABLE students (\n" +
                        "  name STRING,\n" +
                        "  sid BIGINT,\n" +
                        "  PRIMARY KEY (sid) NOT ENFORCED" +
                        ") WITH (\n" +
                        "   'connector' = 'paimon',\n" +
                        "   'path' = 'hdfs://ip:8072/paimon/hive/olap_mz.db/students'\n" +
                        ")");
                //statement.execute("INSERT INTO students VALUES ('1', 's1', 'Hi'), ('2', 's2','Hello')");
                try (ResultSet rs = statement.executeQuery("SELECT st.sid,count(st.sid) as coun FROM students st group by st.sid")) {
                    while (rs.next()) {
                        System.out.println(rs.getLong(1) +","+rs.getLong(2) );
                    }
                    statement.close();
                }
                connection.close();

            }

        }


    }
}

注释的是mysql的,运行的是paimon的。

相关推荐
28岁青春痘老男孩15 小时前
JDK8+SpringBoot2.x 升级 JDK 17 + Spring Boot 3.x
java·spring boot
方璧15 小时前
限流的算法
java·开发语言
九河云15 小时前
海上风电“AI偏航对风”:把发电量提升2.1%,单台年增30万度
大数据·人工智能·数字化转型
元Y亨H16 小时前
Nacos - 服务注册
java·微服务
曲莫终16 小时前
Java VarHandle全面详解:从入门到精通
java·开发语言
ruleslol16 小时前
MySQL的段、区、页、行 详解
数据库·mysql
天若有情67316 小时前
校园二手交易系统实战开发全记录(vue+SpringBoot+MySQL)
vue.js·spring boot·mysql
一心赚狗粮的宇叔16 小时前
中级软件开发工程师2025年度总结
java·大数据·oracle·c#
盛世宏博北京16 小时前
云边协同・跨系统联动:智慧档案馆建设与功能落地
大数据·人工智能
奋进的芋圆16 小时前
DataSyncManager 详解与 Spring Boot 迁移指南
java·spring boot·后端