Flink输出到Redis
Posted 大牛和小白
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flink输出到Redis相关的知识,希望对你有一定的参考价值。
1.代码
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.streaming.connectors.redis.RedisSink
import org.apache.flink.streaming.connectors.redis.common.config.FlinkJedisPoolConfig
import org.apache.flink.streaming.connectors.redis.common.mapper.{RedisCommand, RedisCommandDescription, RedisMapper}
//温度传感器读取样例类
case class SensorReading(id: String, timestamp: Long, temperature: Double)
object RedisSinkTest {
def main(args: Array[String]): Unit = {
val env = StreamExecutionEnvironment.getExecutionEnvironment
env.setParallelism(1)
//source
val inputStream = env.readTextFile("sensor1.txt")
//transform
import org.apache.flink.api.scala._
val dataStream = inputStream.map(x => {
val arr = x.split(",")
SensorReading(arr(0).trim, arr(1).trim.toLong, arr(2).trim.toDouble)
})
//sink
val conf = new FlinkJedisPoolConfig.Builder().setHost("localhost").setPort(6379).build()
dataStream.addSink(new RedisSink[SensorReading](conf, new MyRedisMapper))
env.execute("redis sink test")
}
}
//定义一个redis的mapper类,用于定义保存到redis时调用的命令
class MyRedisMapper extends RedisMapper[SensorReading] {
override def getCommandDescription: RedisCommandDescription = {
//把传感器id和温度值保存成哈希表: HSET key field value
new RedisCommandDescription(RedisCommand.HSET, "sensor_temperature")
}
//相当于是field
override def getKeyFromData(data: SensorReading): String = {
data.id
}
override def getValueFromData(data: SensorReading): String = {
data.temperature.toString
}
}
2.结果
有帮助的欢迎评论打赏哈,谢谢!
以上是关于Flink输出到Redis的主要内容,如果未能解决你的问题,请参考以下文章