跳到主要内容
版本:Next

Redis

Redis 接收器连接器

描述

Redis 接收器连接器可以在批处理或流处理作业中把上游数据写入 Redis。它支持单节点 Redis 和 Redis Cluster, 可以写入 key/stringhashlistsetzset 这几类 Redis 数据。

key 可以是固定的 Redis key,也可以是上游字段名。开启 support_custom_key = true 后,还可以用上游字段 拼出 Redis key,例如 user:${id}

支持引擎

Spark
Flink
SeaTunnel Zeta

主要特性

支持的数据源信息

使用 Redis 连接器时,需要安装以下依赖。可以通过 install-plugin.sh 安装,也可以从 Maven 中央仓库下载。

数据源依赖
Redis下载

选项

名称类型是否必填默认值描述
hoststringmode = SINGLE 时必填-单节点模式下的 Redis 主机地址。
portint6379单节点模式下的 Redis 端口。
nodeslistmode = CLUSTER 时必填-Redis Cluster 节点,例如 ["redis-0:6379", "redis-1:6379"]
modestringSINGLERedis 部署模式,支持 SINGLECLUSTER
userstring-Redis ACL 用户名。
authstring-Redis 认证密码。
db_numint0Redis 数据库编号,主要用于单节点模式。
keystring-Redis key、上游字段名,或开启 support_custom_key 后的 key 模板。
data_typestring-Redis 数据类型,支持 KEYSTRINGHASHLISTSETZSET
formatstringJSON未配置 value 字段时的序列化格式,支持 JSONTEXT
field_delimiterstring,format = TEXT 时使用的字段分隔符。
batch_sizeint10每次批量写入前最多缓存的行数。
expirelong-1Redis key 过期时间,单位秒。小于等于 0 表示不设置过期时间。
support_custom_keybooleanfalse是否用上游字段值替换 key 里的占位符。
value_fieldstring-写入 KEY/STRINGLISTSETZSET 时,作为 Redis value 的上游字段。
hash_key_fieldstring-data_type = HASH 时,作为 Redis hash field 的上游字段。
hash_value_fieldstring-data_type = HASH 时,作为 Redis hash value 的上游字段。
multi_table_sink_replicaint1多表写入时的写入器副本数。
common-optionsconfig-接收器插件通用参数,详情请参考接收器通用选项

写入规则

key

support_custom_key = false 时,连接器会先判断 key 是否是上游字段名:

  • 如果字段存在,就使用该字段的值作为 Redis key。
  • 如果字段不存在,就把 key 本身当作固定 Redis key。

support_custom_key = true 时,连接器会用上游字段值替换 key 里的占位符。${field} 和旧格式 {field} 都支持。

data_type

  • KEYSTRING:每行写入一个 Redis 字符串。同一个 key 下,后写入的数据会覆盖先写入的数据。
  • HASH:写入 Redis hash。配置 hash_key_field 可以指定 hash field。
  • LIST:把每行数据追加到 Redis list。
  • SET:把每行数据加入 Redis set。
  • ZSET:把每行数据加入 Redis zset,score 固定为 1

value_field

写入 KEY/STRINGLISTSETZSET 时,如果只想写入某一个上游字段,可以配置 value_field。 如果不配置,连接器会按 format 把整行数据序列化后写入 Redis。

hash_key_field 和 hash_value_field

写入 HASH 时,hash_key_field 用来指定 Redis hash field。如果配置了 hash_value_field,则把这个字段的值 作为 Redis hash value;如果不配置,连接器会把整行数据序列化后作为 hash value。

multi_table_sink_replica

多表写入时的写入器副本数。当上游数据带有表标识,并且一个作业需要写入多张 Redis 表时使用。

多表作业中,key 可以包含 ${table_name},这样不同上游表的数据会写入不同 Redis key,例如 key = "redis-result-${table_name}"

示例

写入 Redis List

env {
parallelism = 1
job.mode = "BATCH"
}

source {
FakeSource {
schema = {
fields {
id = int
name = string
}
}
rows = [
{ kind = INSERT, fields = [1, "Alice"] },
{ kind = INSERT, fields = [2, "Bob"] }
]
}
}

sink {
Redis {
host = "localhost"
port = 6379
key = "person_list"
data_type = LIST
value_field = "name"
}
}

使用自定义 Key 模板

sink {
Redis {
host = "localhost"
port = 6379
key = "person:${id}"
support_custom_key = true
data_type = KEY
format = JSON
}
}

写入 Redis Hash

sink {
Redis {
host = "localhost"
port = 6379
key = "person_hash"
data_type = HASH
hash_key_field = "id"
hash_value_field = "name"
}
}

写入 Redis Cluster 并设置过期时间

sink {
Redis {
mode = CLUSTER
nodes = ["redis-cluster-0:6379", "redis-cluster-1:6379", "redis-cluster-2:6379"]
key = "event:${id}"
support_custom_key = true
data_type = KEY
value_field = "name"
batch_size = 20
expire = 30
}
}

变更日志

Change Log
ChangeCommitVersion
[Improve][Connector-V2] Use key_field_name option when reading Redis hash data (#9642)https://github.com/apache/seatunnel/commit/5d214a73052.3.12
[Feature][Redis] Add redis key into the result record (#9574)https://github.com/apache/seatunnel/commit/6e8b7c5da52.3.12
[Fix][Connector-Redis] Redis did not write successfully, but the task did not fail (#9055)https://github.com/apache/seatunnel/commit/07510ed9372.3.11
[hotfix][redis] fix npe cause by null host parameter (#8881)https://github.com/apache/seatunnel/commit/7bd58651652.3.10
[Improve][Redis] Optimized Redis connection params (#8841)https://github.com/apache/seatunnel/commit/e56f06cdf02.3.10
[Improve] restruct connector common options (#8634)https://github.com/apache/seatunnel/commit/f3499a6eeb2.3.10
[improve] update Redis connector config option (#8631)https://github.com/apache/seatunnel/commit/f1c313eea62.3.10
[Feature][Redis] Flush data when the time reaches checkpoint.interval and update test case (#8308)https://github.com/apache/seatunnel/commit/e15757bcd72.3.9
Revert "[Feature][Redis] Flush data when the time reaches checkpoint interval" and "[Feature][CDC] Add 'schema-changes.enabled' options" (#8278)https://github.com/apache/seatunnel/commit/fcb29382862.3.9
[Feature][Redis] Flush data when the time reaches checkpoint.interval (#8198)https://github.com/apache/seatunnel/commit/2e24941e6a2.3.9
[Hotfix] Fix redis sink NPE (#8171)https://github.com/apache/seatunnel/commit/6b9074e7692.3.9
[Improve][dist]add shade check rule (#8136)https://github.com/apache/seatunnel/commit/51ef8000162.3.9
[Feature][Connector-Redis] Redis connector support delete data (#7994)https://github.com/apache/seatunnel/commit/02a35c39792.3.9
[Improve][Connector-V2] Redis support custom key and value (#7888)https://github.com/apache/seatunnel/commit/ef2c3c72832.3.9
[Feature][Restapi] Allow metrics information to be associated to logical plan nodes (#7786)https://github.com/apache/seatunnel/commit/6b7c53d03c2.3.9
[improve][Redis]Redis scan command supports versions 5, 6, 7 (#7666)https://github.com/apache/seatunnel/commit/6e70cbe3342.3.8
[Improve][Connector] Add multi-table sink option check (#7360)https://github.com/apache/seatunnel/commit/2489f6446b2.3.7
[Feature][Core] Support using upstream table placeholders in sink options and auto replacement (#7131)https://github.com/apache/seatunnel/commit/c4ca74122c2.3.6
[Improve][Redis] Redis reader use scan cammnd instead of keys, single mode reader/writer support batch (#7087)https://github.com/apache/seatunnel/commit/be37f05c072.3.6
[Feature][Kafka] Support multi-table source read (#5992)https://github.com/apache/seatunnel/commit/60104602d12.3.6
[Improve][Connector-V2]Support multi-table sink feature for redis (#6314)https://github.com/apache/seatunnel/commit/fed89ae3fc2.3.5
[Feature][Core] Upgrade flink source translation (#5100)https://github.com/apache/seatunnel/commit/5aabb14a942.3.4
[Feature][Connector-V2] Support TableSourceFactory/TableSinkFactory on redis (#5901)https://github.com/apache/seatunnel/commit/e84dcb8c102.3.4
[Improve][Common] Introduce new error define rule (#5793)https://github.com/apache/seatunnel/commit/9d1b2582b22.3.4
[Improve] Remove use SeaTunnelSink::getConsumedType method and mark it as deprecated (#5755)https://github.com/apache/seatunnel/commit/8de74081002.3.4
[Improve][Connector-v2][Redis] Redis support select db (#5570)https://github.com/apache/seatunnel/commit/77fbbbd0ee2.3.4
Support config column/primaryKey/constraintKey in schema (#5564)https://github.com/apache/seatunnel/commit/eac76b4e502.3.4
[Feature][Connector-v2][RedisSink]Support redis to set expiration time. (#4975)https://github.com/apache/seatunnel/commit/b5321ff1d22.3.3
Merge branch 'dev' into merge/cdchttps://github.com/apache/seatunnel/commit/4324ee19122.3.1
[Improve][Project] Code format with spotless plugin.https://github.com/apache/seatunnel/commit/423b5830382.3.1
[improve][api] Refactoring schema parse (#4157)https://github.com/apache/seatunnel/commit/b2f573a13e2.3.1
[Improve][build] Give the maven module a human readable name (#4114)https://github.com/apache/seatunnel/commit/d7cd6010512.3.1
[Improve][Project] Code format with spotless plugin. (#4101)https://github.com/apache/seatunnel/commit/a2ab1665612.3.1
[Feature][Connector] add get source method to all source connector (#3846)https://github.com/apache/seatunnel/commit/417178fb842.3.1
[Hotfix][OptionRule] Fix option rule about all connectors (#3592)https://github.com/apache/seatunnel/commit/226dc6a1192.3.0
[Improve][Connector-V2][Redis] Unified exception for redis source & sink exception (#3517)https://github.com/apache/seatunnel/commit/205f7825852.3.0
options in conditional need add to required or optional options (#3501)https://github.com/apache/seatunnel/commit/51d5bcba102.3.0
[feature][api] add option validation for the ReadonlyConfig (#3417)https://github.com/apache/seatunnel/commit/4f824fea362.3.0
[Feature][Redis Connector V2] Add Redis Connector Option Rules & Improve Redis Connector doc (#3320)https://github.com/apache/seatunnel/commit/1c10aacb302.3.0
[Connector-V2][ElasticSearch] Add ElasticSearch Source/Sink Factory (#3325)https://github.com/apache/seatunnel/commit/38254e3f262.3.0
[Improve][Connector-V2][Redis] Support redis cluster connection & user authentication (#3188)https://github.com/apache/seatunnel/commit/c7275a49cc2.3.0
[DEV][Api] Replace SeaTunnelContext with JobContext and remove singleton pattern (#2706)https://github.com/apache/seatunnel/commit/cbf82f755c2.2.0-beta
[Feature][Connector-V2] Add redis sink connector (#2647)https://github.com/apache/seatunnel/commit/71a9e4b0192.2.0-beta
[#2606]Dependency management split (#2630)https://github.com/apache/seatunnel/commit/fc047be69b2.2.0-beta
[Feature][Connector-V2] Add redis source connector (#2569)https://github.com/apache/seatunnel/commit/405f7d6f992.2.0-beta