flink维表关联系列之Redis维表关联:实时查询

本文介绍了在Flink中如何进行低延时的维表关联,特别是在实时查询Redis场景下。通过使用io.lettuce包实现Redis的异步查询,支持单点、sentinel和集群模式。以广告业务为例,展示了如何根据广告位ID从Redis获取广告主ID的流程,并提供了测试验证。
摘要由CSDN通过智能技术生成

点击上方蓝

字关注~

       

在做维表关联如果要求低延时,即维表数据的变更能够被立刻感知到,所以就要求在查询时没有缓存策略,直接查询数据库维表信息。
本篇以实时查询redis为例,要求redis 客户端支持异步查询,可以使用io.lettuce包,支持redis不同模式:单点模式、sentinel模式、集群模式,需要在pom中引入:

<dependency>
            <groupId>io.lettuce</groupId>
            <artifactId>lettuce-core</artifactId>
            <version>5.0.5.RELEASE</version>
</dependency>
<dependency>
            <groupId>io.netty</groupId>
            <artifactId>netty-all</artifactId>
            <version>4.1.24.Final</version>
 </dependency>

关于其不同模式的用法可以参考:https://juejin.im/post/5d8eb73ff265da5ba5329c66
里面做了比较详细的说明,为方便测试使用单点模式,仍以广告业务为例,根据广告位ID从redis里面查询对位的广告主ID。

Redis中数据准备:

Flink 中,我们可以使用 Flink 的 Table API 或 DataStream API 与 Redis 进行维表关联查询。 下面是一个使用 Flink Table API 和 Redis 进行维表关联查询的示例代码: ```java // 创建 Redis 连接配置 RedisOptions redisOptions = new RedisOptions() .setHost("localhost") .setPort(6379); // 创建 Redis 维表 RedisTableSource redisTableSource = RedisTableSource.builder() .forHostPort("localhost", 6379) .withTableName("myTable") .withColumnNames("key", "value") .build(); // 注册 Redis 维表 tableEnv.registerTableSource("myTable", redisTableSource); // 执行关联查询 Table result = tableEnv.sqlQuery("SELECT t1.*, t2.value FROM myDataStream AS t1 JOIN myTable AS t2 ON t1.key = t2.key"); ``` 在上面的示例中,我们首先创建了一个 Redis 连接配置,然后使用 `RedisTableSource` 创建了一个 Redis 维表,并将其注册到 Flink 的 Table API 中。最后,我们执行了一条 SQL 查询,将一个数据流和 Redis 维表进行关联查询,并将结果保存在 `result` 变量中。 如果你想使用 DataStream API 进行维表关联查询,可以使用 `RedisLookupTableFunction`。下面是一个示例代码: ```java // 创建 Redis 连接配置 RedisOptions redisOptions = new RedisOptions() .setHost("localhost") .setPort(6379); // 创建 Redis 维表 RedisLookupTableFunction redisLookupTableFunction = new RedisLookupTableFunction("myTable", redisOptions, "key", "value"); // 执行关联查询 DataStream result = dataStream .keyBy("key") .connect(redisLookupTableFunction) .process(new MyProcessFunction()); ``` 在上面的示例中,我们首先创建了一个 Redis 连接配置,然后使用 `RedisLookupTableFunction` 创建了一个 Redis 维表查询函数。最后,我们将数据流按照 `key` 字段进行分区,并将其与 Redis 维表查询函数连接起来,最后使用 `process` 方法执行关联查询
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值