1. Import dependencies
<!-- https://mvnrepository.com/artifact/org.apache.bahir/flink-connector-redis -->
<dependency>
<groupId>org.apache.bahir</groupId>
<artifactId>flink-connector-redis_2.11</artifactId>
<version>1.0</version>
</dependency>
2. Customize the RedisMapper class
Example: Write the IDs of all User sample objects in Datream into the set whose key is Online_Number
class MyRedisMapper extends RedisMapper[User]{
//定义对于每条数据使用的redis命令与描述
override def getCommandDescription: RedisCommandDescription = {
new RedisCommandDescription(RedisCommand.SADD , "Online_Number")
}
//定义每条数据的key值
override def getKeyFromData(user: User): String = "Online_Number"
//定义每条数据的value值
override def getValueFromData(user: User): String = user.id.toString
}
3. Create redis configuration object
Redis sink provides three types of config classes, namely FlinkJedisPoolConfig, FlinkJedisClusterConfig, and FlinkJedisSentinelConfig.
FlinkJedisPoolConfig : a single-node redis instance.
FlinkJedisPoolConfig : Redis instance in cluster mode.
FlinkJedisSentinelConfig : Redis instance with sentinel mode.
Take FlinkJedisPoolConfig as an example:
val config: FlinkJedisPoolConfig = FlinkJedisPoolConfig.Builder().setHost("localhost").setPort(6379).build()
4. Create RedisSink object
Create a RedisSink object, the parameters are the config created in the third step and the RedisMapper object defined in the second step .
val redisSink = new RedisSink[User](config,new MyRedisMapper)
5. Call addSink of Dstream
dataStream.addSink(redisSink)