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 中。

    org.apache.flink flink-sql-jdbc-driver-bundle ${flink.version}
  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的。

相关推荐
阿宁学科技术库7 小时前
关于“库库AI”在股票投研场景实用性的技术观察
大数据·人工智能·职场和发展
whcyhhh8 小时前
头歌实践教学平台:大数据存储2023(十三3)
大数据·开发语言·python
2601_962065258 小时前
2.启动mysql容器
java·数据库·mysql
2601_962065498 小时前
PHP For 循环
android·java·php
2601_962201728 小时前
Java进阶,集合,Colllection,常见数据结构
java·数据结构·windows
彷徨而立8 小时前
【C/C++】多线程读写普通 int 变量的一些问题
java·c语言·c++
卓怡学长8 小时前
w175基于springboot校园二手物品交易平台
java·spring boot·spring·maven·intellij-idea
chen<>8 小时前
malloc 和 free 背后发生了什么?
java·linux·服务器·操作系统
sunzy_泽阳8 小时前
Java + Spring 实现 Hermes Agent:从源码看多模型接入、子代理、人审与沙箱
java
2601_961133288 小时前
同花顺期货通指标公式通达信指标
java