flink写入redis

使用 Flink Redis Connector

支持三种Redis连接方式

  1. Single Redis Server
  2. Redis Cluster
  3. Redis Sentinel

源码:https://github.com/apache/bahir-flink

添加依赖:


  org.apache.bahir
  flink-connector-redis_2.11
  1.1-SNAPSHOT

如果找不到对应的依赖,可以使用如下依赖:



    org.apache.bahir
    flink-connector-redis_2.11
    1.0

使用 Single Redis Server:

FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig.Builder().setHost("127.0.0.1").build();

DataStream stream = ...;
stream.addSink(new RedisSink>(conf, new RedisExampleMapper());

其中 RedisExampleMapper:

public static class RedisExampleMapper implements RedisMapper>{

    @Override
    public RedisCommandDescription getCommandDescription() {
        return new RedisCommandDescription(RedisCommand.HSET, "HASH_NAME");
    }

    @Override
    public String getKeyFromData(Tuple2 data) {
        return data.f0;
    }

    @Override
    public String getValueFromData(Tuple2 data) {
        return data.f1;
    }
}

重写以下三个方法即可:

getCommandDescription() 返回对应Redis命令

getKeyFromData() 从数据中获取对应的key

getValueFromData()从数据中获取对应的value

 

使用 Redis Cluster:

FlinkJedisPoolConfig conf = new FlinkJedisPoolConfig.Builder()
    .setNodes(new HashSet(Arrays.asList(new InetSocketAddress(5601)))).build();

DataStream stream = ...;
stream.addSink(new RedisSink>(conf, new RedisExampleMapper());

 使用 Redis Sentinel:

FlinkJedisSentinelConfig conf = new FlinkJedisSentinelConfig.Builder()
    .setMasterName("master").setSentinels(...).build();

DataStream stream = ...;
stream.addSink(new RedisSink>(conf, new RedisExampleMapper());

以上,操作简单,不过限制比较多,很多redis的操作不支持。

如果需要的话,根据源码写一版RedisSink继承RichSinkFunction,详见:

https://blog.csdn.net/www_changer/article/details/102654195

 

你可能感兴趣的:(flink,java,redis,flink,redis,java)