Flink从socket读取数据sink到redis

import org.apache.flink.api.common.functions.MapFunction;

import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.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;
import org.apache.flink.streaming.connectors.redis.common.mapper.RedisCommandDescription;
import org.apache.flink.streaming.connectors.redis.common.mapper.RedisMapper;

/**
 * Flink从socket读取数据sink到redis
 *
 */
public class MyRedisSink {
    public static void main(String[] args) throws Exception {
        // 获取一个执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        
        //安装nc命令:yum install -y nc
        //nc -l 33069 ##执行该命令,然后一行一行输入字符
        DataStreamSource<String> dataStream = env.socketTextStream("106.12.241.89", 33069, "\n");

        //lpsuh reids_words => socket发送过来的数据,最终会存储到redis中//对数据进行组装,把string转化为tuple2<String,String>
        DataStream<Tuple2<String, String>> redis_wordsData = dataStream
                .map(new MapFunction<String, Tuple2<String, String>>() {
                    @Override
                    public Tuple2<String, String> map(String s) throws Exception {
                        System.out.println("socket发送过来的数据: " + s);
                        return new Tuple2<>("reids_words", s);
                    }
                });

        //创建redis的配置
        FlinkJedisPoolConfig build = new FlinkJedisPoolConfig.Builder().setHost("127.0.0.1").setPort(6379).build();

        //创建redissink
        RedisSink<Tuple2<String, String>> redisSink = new RedisSink<>(build, new MyRedisMapper());

        redis_wordsData.addSink(redisSink);

        env.execute("MyRedisSink");
    }

    public static class MyRedisMapper implements RedisMapper<Tuple2<String, String>> {
        /**
         * 表示从接收的数据中获取需要操作的redis key
         */
        @Override
        public String getKeyFromData(Tuple2<String, String> data) {
            return data.f0; //第一个元素
        }

        /**
         * 表示从接收的数据中获取需要操作的redis value
         */
        @Override
        public String getValueFromData(Tuple2<String, String> data) {
            return data.f1; //第二个元素
        }

        @Override
        public RedisCommandDescription getCommandDescription() {
            return new RedisCommandDescription(RedisCommand.LPUSH);
        }
    }
}

 

通过nc命令模拟发送数据:

Flink从socket读取数据sink到redis

 

控制台打印的信息:

socket发送过来的数据: aa
socket发送过来的数据: bb
socket发送过来的数据: cc
socket发送过来的数据: dd
socket发送过来的数据: 11
socket发送过来的数据: 22

 

在Redis中查看socket发送过来的数据:

Flink从socket读取数据sink到redis

 

 

 

 

 

上一篇:Flume基础(十三):自定义 Sink


下一篇:60-120-040-使用-Data Transformation简介