flink写入redis(续)

本文深入剖析了Flink连接Redis的实现原理,包括RedisSink类的详细结构、RedisMapper接口的功能、RedisCommand枚举类的作用、FlinkJedisConfigBase的配置方式以及RedisCommandsContainer接口的使用。同时,文章提供了修改指南,帮助读者根据需求定制自己的RedisSink。

参照源码:https://github.com/apache/bahir-flink/tree/master/flink-connector-redis

先分析,后修改。

flink-connector-redis代码目录:

着重看RedisSink类:

1. 包含如下几个私有对象: 

private String additionalKey;
private Integer additionalTTL;
private RedisMapper<IN> redisSinkMapper;
private RedisCommand redisCommand;
private FlinkJedisConfigBase flinkJedisConfigBase;
private RedisCommandsContainer redisCommandsContainer;
additionalKey:hset中的hashField 或 zadd中的score
additionalTTL:ttl
redisSinkMapper:redis输出映射接口,提供抽象方法,由用户实现
public interface RedisMapper<T> extends Function, Serializable {

    /**
     * Returns descriptor which defines data type.
     *
     * @return data type descriptor
     */
    RedisCommandDescription getCommandDescription();

    /**
     * Extracts key from data.
     *
     * @param data source data
     * @return key
     */
    String getKeyFromData(T data);

    /**
     * Extracts value from data.
     *
     * @param data source data
     * @return value
     */
    String getValueFromData(T data);

    /**
     * Extracts the additional key from data as an {@link Optional<String>}.
     * The default implementation returns an empty Optional.
     *
     * @param data
     * @return Optional
     */
    default Optional<String> getAdditionalKey(T data) {
        return Optional.empty();
    }

    /**
     * Extracts the additional time to live (TTL) for data as an {@link Optional<Integer>}.
     * The default implementation returns an empty Optional.
     *
     * @param data
     * @return Optional
     */
    default Optional<Integer> getAdditionalTTL(T data) {
        return Optional.empty();
    }
}
redisCommand:redis命令枚举类
public enum RedisCommand {

    /**
     * Insert the specified value at the head of the list stored at key.
     * If key does not exist, it is created as empty list before performing the push operations.
     */
    LPUSH(RedisDataType.LIST),

    /**
     * Insert the specified value at the tail of the list stored at key.
     * If key does not exist, it is created as empty list before performing the push operation.
     */
    RPUSH(RedisDataType.LIST),

    /**
     * Add the specified member to the set stored at key.
     * Specified member that is already a member of this set is ignored.
     */
    SADD(RedisDataType.SET),

    ...
}
flinkJedisConfigBase:redis配置信息抽象类,提供三种实现类
  • FlinkJedisPoolConfig:Single 
  • FlinkJedisClusterConfig:Cluster
  • FlinkJedisSentinelConfig:Sentinel

重点!​​​

RedisCommandsContainer:redis命令容器接口,提供open()、close()及hset()等抽象方法

/**
 * The container for all available Redis commands.
 */
public interface RedisCommandsContainer extends Serializable {

    /**
     * Open the Jedis container.
     *
     * @throws Exception if the instance can not be opened properly
     */
    void open() throws Exception;

    void hset(String key, String hashField, String value, Integer ttl);
    void rpush(String listName, String value);
    void lpush(String listName, String value);
    void sadd(String setName, String value);
    void publish(String channelName, String message);
    void set(String key, String value);
    void setex(String key, String value, Integer ttl);
    void pfadd(String key, String element);
    void zadd(String key, String score, String element);
    void zrem(String key, String element);

    /**
     * Close the Jedis container.
     *
     * @throws IOException if the instance can not be closed properly
     */
    void close() throws IOException;
}

提供两个实现类:

public class RedisContainer implements RedisCommandsContainer, Closeable {}
public class RedisClusterContainer implements RedisCommandsContainer, Closeable {}

和一个构造类:

public class RedisCommandsContainerBuilder {}

重中之重!

RedisContainer 中包含 private transient (私有、临时)的JedisPool、JedisSentinel Pool 

public class RedisContainer implements RedisCommandsContainer, Closeable {

    private transient JedisPool jedisPool;
    private transient JedisSentinelPool jedisSentinelPool;

    ...
}

RedisClusterContainer  中包含 private transient (私有、临时)的JedisCluster

public class RedisClusterContainer implements RedisCommandsContainer, Closeable {

    private transient JedisCluster jedisCluster;

    ...
}

关于 transient: 

  • 一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问。
  • transient关键字只能修饰变量,而不能修饰方法和类。注意,本地变量是不能被transient关键字修饰的。变量如果是用户自定义类变量,则该类需要实现Serializable接口。
  • 被transient关键字修饰的变量不再能被序列化,一个静态变量不管是否被transient修饰,均不能被序列化。

解决了Jedis序列化报错的问题:

org.apache.flink.api.common.InvalidProgramException: The implementation of the RichFilterFunction is not serializable. The object probably contains or references non serializable fields.

...

Caused by: java.io.NotSerializableException: redis.clients.jedis.JedisSentinelPool

2.构造方法:

    public RedisSink(FlinkJedisConfigBase flinkJedisConfigBase, RedisMapper<IN> redisSinkMapper) {
        Objects.requireNonNull(flinkJedisConfigBase, "Redis connection pool config should not be null");
        Objects.requireNonNull(redisSinkMapper, "Redis Mapper can not be null");
        Objects.requireNonNull(redisSinkMapper.getCommandDescription(), "Redis Mapper data type description can not be null");

        this.flinkJedisConfigBase = flinkJedisConfigBase;

        this.redisSinkMapper = redisSinkMapper;
        RedisCommandDescription redisCommandDescription = redisSinkMapper.getCommandDescription();

        this.redisCommand = redisCommandDescription.getCommand();
        this.additionalTTL = redisCommandDescription.getAdditionalTTL();
        this.additionalKey = redisCommandDescription.getAdditionalKey();
    }

3.invoker()方法:

    public void invoke(IN input, Context context) throws Exception {
        String key = redisSinkMapper.getKeyFromData(input);
        String value = redisSinkMapper.getValueFromData(input);

        Optional<String> optAdditionalKey = redisSinkMapper.getAdditionalKey(input);
        Optional<Integer> optAdditionalTTL = redisSinkMapper.getAdditionalTTL(input);

        switch (redisCommand) {
            case RPUSH:
                this.redisCommandsContainer.rpush(key, value);
                break;
            case LPUSH:
                this.redisCommandsContainer.lpush(key, value);
                break;

            ...

            default:
                throw new IllegalArgumentException("Cannot process such data type: " + redisCommand);
        }
    }

4.open()和close()方法:

    @Override
    public void open(Configuration parameters) throws Exception {
        try {
            this.redisCommandsContainer = RedisCommandsContainerBuilder.build(this.flinkJedisConfigBase);
            this.redisCommandsContainer.open();
        } catch (Exception e) {
            LOG.error("Redis has not been properly initialized: ", e);
            throw e;
        }
    }

    @Override
    public void close() throws IOException {
        if (redisCommandsContainer != null) {
            redisCommandsContainer.close();
        }
    }

以上,

  • 修改RedisMapper提供对应的Redis映射
  • 修改RedisCommandsContainer提供需要的Redis命令
  • 修改FlinkJedisConfigBase提供需要的Redis配置

根据自身需求,写一版自己的RedisSink,问题不大。

### 如何使用 Flink 将数据写入 Redis 为了实现 Apache FlinkRedis 数据库写入数据的功能,可以采用多种方法。其中一种常见的方式是通过自定义 `RedisMapper` 接口来完成此操作。 下面是一个具体的例子,展示了如何创建一个实现了 `RedisMapper<Tuple2<String, String>>` 的类: ```java public static class RedisExampleMapper implements RedisMapper<Tuple2<String, String>> { @Override public RedisCommandDescription getCommandDescription() { return new RedisCommandDescription(RedisCommand.HSET, "HASH_NAME"); } @Override public String getKeyFromData(Tuple2<String, String> data) { return data.f0; } @Override public String getValueFromData(Tuple2<String, String> data) { return data.f1; } } ``` 这段代码定义了一个名为 `RedisExampleMapper` 的静态内部类,该类指定了要使用的 Redis 命令(在这里是指定哈希表名称并设置键值对),以及从输入元组中提取用于构建命令参数的关键字和值的方法[^2]。 接着,在主程序里可以通过调用 `addSink()` 方法并将上述映射器作为参数传递给它,从而建立到目标存储系统的连接路径;之后再调用 `execute()` 来启动整个流处理流程[^1]。 对于更复杂的场景或者希望简化配置过程的情况,则建议查阅专门针对 Flink Connector Redis 编写的文档资料,这有助于理解更多高级特性和最佳实践指导[^3]。 另外值得注意的是,在实际应用过程中可能还会涉及到与其他组件如 Kafka 集成的需求,此时就需要掌握好各个部分之间的协作机制,并能够灵活运用 Flink 提供的各种算子来进行必要的转换处理工作[^4]。 如果想要进一步提升易用性,还可以考虑基于 Bahir-Flink 这样的第三方扩展包进行二次开发,使得用户可以直接通过 SQL 语句的形式定义待保存的数据字段及其对应的 Redis 表结构[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值