准备工作
Flink Redis Connector
提供了一个Sink可将数据写入Redis。若要使用该连接器需要将以下内容引入工程:
<!-- redis connector -->
<dependency>
<groupId>org.apache.bahir</groupId>
<artifactId>flink-connector-redis_2.11</artifactId>
<version>1.0</version>
</dependency>
代码实现
核心代码在于:
streaming.addSink(new RedisSink[Person](redisConf, new MyRedisMapper))
这里你需要两个参数:
redisConf
:redis配置信息
// 配置redis conf
val redisConf: FlinkJedisPoolConfig = new FlinkJedisPoolConfig.Builder()
.setHost("host")
.setPort(6379)
.setTimeout(30000)
.build()
MyRedisMapper
:自定义的RedisMapper
//自定义MyRedisMapper
class MyRedisMapper extends RedisMapper[Person] {
override def getCommandDescription: RedisCommandDescription = {
new