Flink output to Redis

   1. Code

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
org.apache.flink.api.scala._ Import
Val Datastream = inputStream.map (X => {
Val x.split ARR = ( ",")
SensorReading (ARR (0) .trim, ARR (. 1) .trim. toLong, ARR (2) .trim.toDouble)
})

// sink
Val new new FlinkJedisPoolConfig.Builder the conf = (). The setHost ( "localhost"). setPort (6379) .build ()

dataStream.addSink (new new RedisSink [SensorReading] (the conf, new new MyRedisMapper))

env.execute ( "sink redis Test")
}
}

// Mapper redis a class definition, which defines the storage command to the calling redis
class MyRedisMapper the extends RedisMapper [SensorReading] {
the override DEF getCommandDescription: = {RedisCommandDescription
// save id and temperature sensor to a hash table: 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.结果
 

Guess you like

Origin www.cnblogs.com/wddqy/p/12175808.html