当前位置:网站首页>Flink SQL 实现读写redis,并动态生成Hset key
Flink SQL 实现读写redis,并动态生成Hset key
2022-07-06 23:35:00 【HD0do】
在官方提供的flink-connect-redis中并没有实现Flink SQL的方式读写redis。要想实现Flink sql的方式读写redis需要自己实现对应的代码,好在GitHub已经有了对应的开源实现,我们只需对应的借鉴使用即可。
pom 依赖:
<dependency>
<groupId>io.github.jeff-zou</groupId>
<artifactId>flink-connector-redis</artifactId>
<version>1.0.11</version>
</dependency>
通过导入jar,可以实现Flink sql 读写redis :
先创建Flink CDC 的一张表:
CREATE TABLE memberCDC(
`member_id` bigint,
mobile string,
`tenant_id` string ,
gender int ,
proctime as procTime(),
PRIMARY KEY(member_id) NOT ENFORCED
) WITH (
'connector' = 'mysql-cdc',
'hostname' = '10.50.20.181',
'port' = '330806',
'username' = 'reGaYTkly',
'password' = '40IGOJHFDlloBE6',
'database-name' = 'member_db',
'table-name' = 'm_member_tenant',
'scan.startup.mode'='latest-offset',
'debezium.skipped.operations'='d'
)
Flink SQL 写入redis(以Hset数据类型为例):
CREATE TABLE memberRedis (
`member_id` bigint,
mobile string,
`tenant_id` string,
gender int
) WITH (
'connector' = 'redis',
'host' = '10.180.80.89',
'port' = '300079',
'password' = 'big88dajjggta34',
'redis-mode' = 'single',
'command'='hset'
)
inserter into memberRedis
select
member_id,
mobile,
tenant_id,
gender
from memberCDC
这里 member_id 相当于Hset的 additionalKey mobile相当于key,而这可以通过SQL 语句动态的拼接生成需要的key格式。相当于可以支持动态生成你需要的key.
Flink SQL读取redis(以Hset数据类型为例):
读取redis中的数据,一般来用作为维度关联使用:
CREATE TABLE memberSourceRedis (
`member_id` bigint,
mobile string,
`tenant_id` string,
gender int
) WITH (
'connector' = 'redis',
'host' = '10.110.60.5',
'port' = '30479',
'password' = 'daffdag1234',
'redis-mode' = 'single',
'command'='hget',
'maxIdle'='2',
'minIdle'='1',
'lookup.cache.max-rows'='10',
'lookup.cache.ttl'='10',
'lookup.max-retries'='3'
)
insert into memberRedis
select
t2.member_id,
t2.mobile,
t2.tenant_id,
t1.gender
from memberCDC t1
left join memberSourceRedis for system_time as of t1.proctime as t2 on
t1.member_id = t2.member_id and t1.mobile = t2.mobile
类似读取,这里 member_id 相当于Hset的 additionalKey mobile相当于key,这里可以通过SQL 语句动态的拼接生成key,传进来的key实际上就是memberCDC中关联字段的数据值。
连接redis参数列表:
总结:
上述Flink SQL 读写redis 对于 hset 数据类型来说都支持一个字段的值写入和读取,多个字段写入会被默认过滤。
如果要支持多个字段的写入和读取,需要修改依赖代码,在写入时候将多个字段封装为JSONString 写入Redis,在读取的时候将JSON数据通过 catalog将映射为对应多个字段的值。
后续文章中也会介绍如何修改源码,编译jar包来实现支持多个字段的Flink SQL的读写redis。如果你有需要也可以通过下面的链接来实现对应的flink sql读写Redis的特殊业务需求。感觉有用也欢迎关注 迪答 公众号~~
本文参考GitHub的开源项目:https://github.com/jeff-zou/flink-connector-redis
边栏推荐
- Initial experience of annotation
- pmp真的有用吗?
- Use, configuration and points for attention of network layer protocol (taking QoS as an example) when using OPNET for network simulation
- MySQL数据库学习(8) -- mysql 内容补充
- Annotation初体验
- Educational Codeforces Round 22 B. The Golden Age
- K6el-100 leakage relay
- [JS component] date display.
- 全链路压测:影子库与影子表之争
- Sorry, I've learned a lesson
猜你喜欢
[opencv] image morphological operation opencv marks the positions of different connected domains
高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏
Design, configuration and points for attention of network specified source multicast (SSM) simulation using OPNET
Is the human body sensor easy to use? How to use it? Which do you buy between aqara green rice and Xiaomi
带你遨游银河系的 10 种分布式数据库
Autowired注解用于List时的现象解析
什么是依赖注入(DI)
Harmonyos fourth training
LabVIEW is opening a new reference, indicating that the memory is full
Longest palindrome substring (dynamic programming)
随机推荐
App clear data source code tracking
Life experience of an update statement
Knapsack problem unrelated to profit (depth first search)
说一说MVCC多版本并发控制器?
Linkedblockingqueue source code analysis - initialization
Mysql database learning (8) -- MySQL content supplement
TabLayout修改自定义的Tab标题不生效问题
[question] Compilation Principle
How Alibaba cloud's DPCA architecture works | popular science diagram
Mybaits之多表查询(联合查询、嵌套查询)
Auto.js 获取手机所有app名字
Timer创建定时器
AIDL 与Service
背包问题(01背包,完全背包,动态规划)
Lombok插件
【opencv】图像形态学操作-opencv标记不同连通域的位置
[optimal web page width and its implementation] [recommended collection "
基于 hugging face 预训练模型的实体识别智能标注方案:生成doccano要求json格式
Dbsync adds support for mongodb and ES
As we media, what websites are there to download video clips for free?