4

FLINK 基于1.15.2的Java开发-读文件并把内容 sink到redis

 1 year ago
source link: https://blog.csdn.net/lifetragedy/article/details/127139454
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

需求为:我们需要把如下文本,按照“,“分隔。然后把每行第二列作为主键,第三列作为value塞到Redis里。

而flink要用到Redis必须要使用以下pom依赖

在使用flink redis connector时,一定要记得把slf4j-log4j12给排除掉,否则这个包日后会和我们在项目工程内的log4j2产生冲突而导致输不出日志。

完整的项目pom配置

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
newCodeMoreWhite.png

SinkToRedis.java



newCodeMoreWhite.png

用到的LineSplitter和SinkRedisMapper辅助类

LineSplitter.java



newCodeMoreWhite.png

SinkRedisMapper.java



newCodeMoreWhite.png

同样,打包上传到我们的flink的webui(也可以在eclipse里直接运行)

我们在上一篇:FLINK 基于1.15.2的Java开发-入门中详细给出了flink的maven打包。flink官方主张使用maven-shade-plugin打包, 主要是申明了如下build版段



newCodeMoreWhite.png

其中mainClass很重要,必须要申请成你可以运行flink的那个main类。

最后得到Redis里如下值

fa302864589b40d7bf1fe68ab281a426.png

 下面给出data.txt文件内容:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK