Flink from entry to real fragrance (9, Sink data output-Redis)

If there is no redis environment, you can install a
sudo yum install epel-release -y on the virtual machine first

sudo yum -y install redis

systemctl start redis

The default is to monitor at 127.0.0.1, if you want to adjust, you can change the /etc/redis.conf configuration file to change #bind 127.0.0.1 to bind 0.0.0.0 and restart the redis service (if you find that you still can’t access it, there is a high probability (It is a linux firewall problem)

在pom.xml中增加redis依赖
<dependency>
<groupId>org.apache.bahir</groupId>
<artifactId>flink-connector-redis_2.11</artifactId>
<version>1.0</version>
</dependency>

Create a new RedisSink1.scala (be careful not to have the same name as RedisSink in the imported source code)
package com.mafei.sinktest

import org.apache.flink.streaming.api.scala.{StreamExecutionEnvironment, createTypeInformation}
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 SensorReadingTest5(id: String,timestamp: Long, temperature: Double)

object RedisSink1 {
def main(args: Array[String]): Unit = {
//Create execution environment
val env = StreamExecutionEnvironment.getExecutionEnvironment

val inputStream= env.readTextFile("/opt/java2020_study/maven/flink1/src/main/resources/sensor.txt")
env.setParallelism(1)
inputStream.print()

//先转换成样例类类型
val dataStream = inputStream
  .map(data =>{
    val arr = data.split(",")   //按照,分割数据,获取结果
    SensorReadingTest5(arr(0), arr(1).toLong,arr(2).toDouble)  //生成一个传感器类的数据,参数中传toLong和toDouble是因为默认分割后是字符串类别
  })

//定义一个FlinkJedisConfigBase
val conf = new FlinkJedisPoolConfig.Builder()
    .setHost("127.0.0.1")
    .setPort(6379)
    .build()
dataStream.addSink(new RedisSink[SensorReadingTest5](conf, new MyRedisMapper))
env.execute("写入redis")

}
}

//Define a redis mapper class, used to define the command called when saving to redis
class MyRedisMapper extends RedisMapper[SensorReadingTest5]{

//Define the command to save data to redis, HSET table name key value
override def getCommandDescription: RedisCommandDescription = {
// additionalKey is used for similar hset, set the table name
new RedisCommandDescription(RedisCommand.HSET, "sensor_temper")
}

//Set the redis key
override def getKeyFromData(t: SensorReadingTest5): String = t.id

//Set the value of redis to temperature
override def getValueFromData(t: SensorReadingTest5): String = t.temperature.toString
}

Operation effect and code structure diagram
Flink from entry to real fragrance (9, Sink data output-Redis)
Log in to redis on the server to see the effect:
[root@localhost ~]# redis-cli
127.0.0.1:6379> keys *
1) "sensor_temper"
127.0.0.1:6379> hget sensor_temper sensor1
"41.0"

Guess you like

Origin blog.51cto.com/mapengfei/2547246