Flink socket to read data from the 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;
importorg.apache.flink.streaming.connectors.redis.common.mapper.RedisCommandDescription;
 Import org.apache.flink.streaming.connectors.redis.common.mapper.RedisMapper; 

/ ** 
 * Flink socket to read data from the sink redis 
 * 
 * / 
public  class MyRedisSink {
     public  static  void main (String [] args) throws Exception {
         // Get an execution environment 
        StreamExecutionEnvironment the env = StreamExecutionEnvironment.getExecutionEnvironment (); 
        
        // installation command nc: nc yum the install -Y
         // nc - l 33069 ## execute the command, then the input characters line by line
        DataStreamSource <String> = env.socketTextStream Datastream ( "106.12.241.89", 33069, "\ n-" ); 

        // lpsuh reids_words => Socket transmitted over the data eventually stored in redis // assembled data, the string into Tuple2 <String, String> 
        DataStream <Tuple2 <String, String >> redis_wordsData = Datastream 
                .map ( new new MapFunction <String, Tuple2 <String, String >> () { 
                    @Override 
                    public Tuple2 <String, String> Map ( S String) throws Exception { 
                        System.out.println ( "data sent from the 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>> {
        /**
         * Retrieves redis key to be operated from the received data 
         * / 
        @Override 
        public String getKeyFromData (Tuple2 <String, String> Data) {
             return data.f0; // first element 
        } 

        / ** 
         * indicates received from data acquisition operations required value Redis 
         * / 
        @Override 
        public String getValueFromData (Tuple2 <String, String> data) {
             return data.f1; // second element 
        } 

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

 

Nc analog transmission data commands:

 

Print Console information:

Data sent over socket: AA 
BB: data sent over socket 
data sent over socket: cc 
data sent over socket: dd 
: transmitting data over the socket 11 
data sent over socket: 22

 

View socket data sent over in the Redis:

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/linjiqin/p/12570816.html