Flink 输出至 Redis
【1】引入第三方 Bahir 提供的 Flink-redis 相关依赖包
1 <!-- https://mvnrepository.com/artifact/org.apache.bahir/flink-connector-redis --> 2 <dependency> 3 <groupId>org.apache.bahir</groupId> 4 <artifactId>flink-connector-redis_2.11</artifactId> 5 <version>1.0</version> 6 </dependency>
【2】Flink 连接 Redis并输出 Sink 处理结果
1 package com.zzx.flink 2 3 import org.apache.flink.streaming.api.scala._ 4 import org.apache.flink.streaming.connectors.redis.RedisSink 5 import org.apache.flink.streaming.connectors.redis.common.config.FlinkJedisPoolConfig 6 import org.apache.flink.streaming.connectors.redis.common.mapper.{RedisCommand, RedisCommandDescription, RedisMapper} 7 8 object RedisSinkTest { 9 def main(args: Array[String]): Unit = { 10 // 创建一个流处理执行环境 11 val env = StreamExecutionEnvironment.getExecutionEnvironment 12 //从文件中读取数据并转换为 类 13 val inputStreamFromFile: DataStream[String] = env.readTextFile("E:\\Project\\flink\\src\\main\\resources\\wordcount.txt") 14 //转换 SensorReading为用户自定义的类,是从文件转换而来的 15 val dataStream: DataStream[SensorReading] = inputStreamFromFile 16 .map( data => { 17 var dataArray = data.split(",") 18 SensorReading(dataArray(0),dataArray(1).toLong,dataArray(2).toDouble) 19 }) 20 //定义一个 redis 的配置类 继承了FlinkJedisConfigBase 正是 SensorReading需要传入的参数,底层将有些数据保存成了状态数据。 21 val conf = new FlinkJedisPoolConfig.Builder().setHost("192.168.52.131").setPort(6379).setPassword("zzx").build() 22 //定义 RedisMapper 数据保存的类型 23 val myMapper = new RedisMapper[SensorReading] { 24 //定义保存数据到 redis的命令,hset table key value 25 override def getCommandDescription: RedisCommandDescription = { 26 // hset tablesname 27 new RedisCommandDescription(RedisCommand.HSET , "sensor_temp") 28 } 29 //设置key 30 override def getKeyFromData(data: SensorReading): String = data.id 31 //设置value 32 override def getValueFromData(data: SensorReading): String = data.temperature.toString 33 } 34 dataStream.addSink(new RedisSink[SensorReading](conf,myMapper)) 35 36 env.execute("Redis Sink test") 37 } 38 }
查看源码可知 RedisSink是继承自 RichSinkFunction<IN> 类
public class RedisSink<IN> extends RichSinkFunction<IN> {
【3】查看 Redis 输出信息