flink自定义jdbcsource

4 篇文章 0 订阅

import com.yan.product.utils.productInfo
import org.apache.flink.configuration.Configuration
import org.apache.flink.streaming.api.functions.source.{RichSourceFunction, SourceFunction}

import java.sql.{Connection, DriverManager, PreparedStatement}


class trafficSpeedLimitSourceFunc extends RichSourceFunction[productInfo] {
  private var running = true
  private var conn: Connection = _
  private var sql: PreparedStatement = _

  //初始化jdbc,只在未启动的时候执行一次
  override def open(parameters: Configuration): Unit = {
    conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "root")
    sql = conn.prepareStatement("select companyCode,storeCode,productCode,price from productinf")

  }

  override def run(ctx: SourceFunction.SourceContext[productInfo]): Unit = {
    while (running) {
      val set = sql.executeQuery()
      while (set.next()) {
        var companyCode: String = set.getString("companyCode")
        var storeCode: String = set.getString("storeCode")
        var productCode: String = set.getString("productCode")
        var price: Double = set.getDouble("price ")
        ctx.collect(productInfo(companyCode, storeCode,productCode, price))
      }
      //半小时执行一次
      Thread.sleep(30 * 60 * 1000)
    }
  }

  override def cancel(): Unit = {
    running = false

  }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好,如果您需要自定义 Flink Source Function 从 PostgreSQL 中读取数据,可以按照以下步骤操作: 1. 在项目中添加 PostgreSQL JDBC 驱动,例如: ```xml <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.2.12</version> </dependency> ``` 2. 实现 SourceFunction 接口,并在 run 方法中编写从 PostgreSQL 中读取数据的逻辑,例如: ```java public class PostgresSourceFunction implements SourceFunction<Row> { private final String url; private final String username; private final String password; private final String query; private volatile boolean isRunning = true; public PostgresSourceFunction(String url, String username, String password, String query) { this.url = url; this.username = username; this.password = password; this.query = query; } @Override public void run(SourceContext<Row> ctx) throws Exception { Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { conn = DriverManager.getConnection(url, username, password); stmt = conn.prepareStatement(query); rs = stmt.executeQuery(); while (isRunning && rs.next()) { // 将查询结果转化为 Flink 的 Row 类型 Row row = Row.of(rs.getInt(1), rs.getString(2), rs.getDouble(3)); // 发射数据到下游算子 ctx.collect(row); } } finally { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } } @Override public void cancel() { isRunning = false; } } ``` 在这个例子中,我们使用 JDBC 连接 PostgreSQL 数据库,并执行指定的查询语句,将查询结果转为 Flink 的 Row 类型,然后通过 SourceContext 发射数据到下游算子。 3. 在 Flink 程序中使用自定义Source Function,例如: ```java String url = "jdbc:postgresql://localhost:5432/mydb"; String username = "myuser"; String password = "mypassword"; String query = "SELECT id, name, price FROM products"; DataStream<Row> stream = env.addSource(new PostgresSourceFunction(url, username, password, query)); ``` 在这个例子中,我们使用 addSource 方法将自定义的 PostgresSourceFunction 添加到 Flink 程序中,从而能够读取 PostgreSQL 数据库中的数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值