Redis 扩展模块 -- RedisJSON

摘要

RedisJSON 简介

  • RedisJSON 是 Redis 官方维护的一个扩展模块,隶属于 Redis Stack,专门用于对JSON数据进行操作。

  • 该模块以 Redis Module 方式加载,可无缝集成到现有 Redis 实例中。

  • Redis8+,RedisJSON 已经内置在 Redis 中,可以在安装redis同时安装全部 Stack 模块。

安装 RedisJSON

安装时需要科学上网,主要是安装依赖时需要从海外网下载,如果要部署在国内服务器,可能会连接失败。
可以在海外的相同配置的服务器上进行编译,之后将编译好的rejson.so上传到国内服务器即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
mkdir -p /usr/local/soft/modules/
cd /usr/local/soft/modules
# clone 代码,这里 --recursive 是为了拉取子模块
git clone --recursive https://github.com/RedisJSON/RedisJSON.git
cd RedisJSON
# 推荐切换到稳定的release版本
git checkout v2.8.16
# 更新子模块,非必须,如果上面 clone 时没有加上 --recursive ,这个步骤就不能省略
git submodule update --init --recursive

# 检查并安装需要的依赖
./sbin/setup
## 输出
# readies version: 7fc8e62
dnf install -q -y ca-certificates
dnf install -q -y wget unzip
/usr/local/soft/modules/RedisJSON/deps/readies/bin/enable-utf8
dnf install -q -y git unzip rsync
/usr/local/soft/modules/RedisJSON/deps/readies/bin/getclang --modern
/usr/local/soft/modules/RedisJSON/deps/readies/bin/getrust
/usr/local/soft/modules/RedisJSON/deps/readies/bin/getcmake --usr
dnf install -q -y which
/usr/local/soft/modules/RedisJSON/deps/readies/bin/getgcc --modern
dir=$(mktemp -d /tmp/tar.XXXXXX); (cd $dir; wget --no-verbose -O tar.tgz http://redismodules.s3.amazonaws.com/readies/gnu/gnu-tar-1.32-x64-centos7.tgz; tar -xzf tar.tgz -C /; ); rm -rf $dir
dnf install -q -y lcov
/usr/bin/python3 /usr/local/soft/modules/RedisJSON/deps/readies/bin/getrmpytools --reinstall --modern
/usr/bin/python3 -m pip install --disable-pip-version-check --user -r /usr/local/soft/modules/RedisJSON/tests/pytest/requirements.txt
/usr/local/soft/modules/RedisJSON/deps/readies/bin/getaws
NO_PY2=1 /usr/local/soft/modules/RedisJSON/deps/readies/bin/getpudb

# RedisJSON 的编译依赖 Rust 工具链,所以编译前需要安装rust,若已经安装则忽略
# 安装 rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# 按提示选择默认安装(通常选择 1)

# 让 Rust 生效
source $HOME/.cargo/env
# 检查 rust 版本
rustc --version
cargo --version


# 编译 RedisJSON
make
# 编译过程未保存说明编译成功,编译后的文件位于 `bin/linux-x64-release/rejson.so`

./sbin/setup 报错

  • 本人使用的是 Amazon Linux 2023(内核 6.1),即 EL9,相当于CentOS 9,所以第一次运行会报错,大致报错信息如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
./sbin/setup
## 错误信息1
……
[FAILED] raven-release.el9.noarch.rpm: Status code: 403 for https://dyn.su/el9/base/x86_64/raven-release.el9.noarch.rpm (IP: 104.21.57.14)
Status code: 403 for https://dyn.su/el9/base/x86_64/raven-release.el9.noarch.rpm (IP: 104.21.57.14)

In /usr/local/soft/modules/RedisBloom/deps/readies/bin/getepel:
346 # xinstall --allowerasing https://dl.fedoraproject.org/pub/epel/epel-release-latest-${EPEL}.noarch.rpm
347 fi
348
349 >>> install_raven
350 install_remi
351 # install_centos_stream_repos
352

command failed: /usr/local/soft/modules/RedisBloom/deps/readies/bin/getepel

In /usr/local/soft/modules/RedisBloom/sbin/setup:
16 python3 -m pip list
17 fi
18
19 >>> $ROOT/sbin/system-setup.py
20 if [[ $VERBOSE == 1 ]]; then
21 python3 -m pip list
22 fi 编译安装时报错

## 错误分析与解决方法
问题原因:
你这个错误不是 RedisBloom 本身的编译问题,而是 依赖环境初始化(readies / getepel)阶段失败,失败点非常明确。
readies/bin/getepel 脚本在 RHEL 9 / Rocky 9 / AlmaLinux 9 系统上尝试安装 Raven Repo,但该仓库地址 https://dyn.su/el9/... 已被 403 Forbidden 拒绝,导致脚本直接失败并中断 setup。
这不是你机器的问题,而是 RedisBloom 依赖工具链对 EL9 的兼容性滞后。
RedisBloom 的 ./sbin/setup 会调用 deps/readies/bin/getepel
这个脚本用于:
安装 EPEL
安装 Raven Repo(EL9 特有)
安装 Remi Repo
但 Raven Repo 目前已不稳定 / 不再公开提供 rpm 下载,而 readies 代码仍然在强制安装。
你的系统是 EL9 系列,日志中明确:raven-release.el9.noarch.rpm
说明你使用的可能是如下系统中的一个:
RHEL 9
Rocky Linux 9
AlmaLinux 9
CentOS Stream 9

推荐解决方案:
Raven Repo 并非是 ReidsBloom 的必要依赖,所以直接修改 getepel 脚本,禁用 install_raven
vi deps/readies/bin/getepel,找到所有 install_raven,并将其注释掉即可

## 错误信息2
……
dnf install -q -y llvm-toolset-7.0
Error: Unable to find a match: llvm-toolset-7.0
command failed: dnf install -q -y llvm-toolset-7.0
command failed: /usr/local/soft/modules/RedisJSON/deps/readies/bin/getclang --modern

In /usr/local/soft/modules/RedisJSON/sbin/setup:
18 python3 -m pip list
19 fi
20
21 >>> $ROOT/sbin/system-setup.py
22 if [[ $VERBOSE == 1 ]]; then
23 python3 -m pip list
24 fi

# 错误分析与解决方法:
问题原因:
同上,还是操作系统的事,在 `deps/readies/bin/getclang`中有一个方法,其是一看就明白了
def redhat_compat(self):
if not self.modern:
return
self.run("%s/bin/getepel" % READIES, sudo=True)
if self.dist in ['centos', 'ol'] and self.os_version[0] >= 8:
self.install("clang")
self.install("llvm-toolset")
else:
self.install("llvm-toolset-7.0")
self.cp_to_profile_d("/opt/rh/llvm-toolset-7.0/enable", "llvm-toolset-7.0.sh")
我使用的机器不是 centos,所以就走else的逻辑了,另外这里即便走了 centos逻辑,也有问题,就是el9中已经没有 llvm-toolset 了,取而代之的是 llvm,所以需要修改该方法

修改方法如下:
def redhat_compat(self):
if not self.modern:
return
self.run("%s/bin/getepel" % READIES, sudo=True)
# 去掉判断,直接安装,前提是 el9
self.install("clang")
self.install("llvm")

Redis 启用模块

  • 将生成的 rejson.so 拷贝到 redis 的 modules 目录下(非必须)

1
2
# 注意 .so 文件需要包含可执行权限
cp /usr/local/soft/modules/RedisBloom/bin/linux-x64-release/rejson.so /usr/local/soft/redis-7.4.7/modules/rejson.so
  • 本文采用 loadmodule 加载模块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 将 rejson.so 添加到 redis.conf 中,需要重启 redis
loadmodule /usr/local/soft/redis-7.4.7/modules/rejson.so

# 启动redis
redis-server redis.conf

# 登录测试
redis-cli --user admin --pass 123456
# 查看模块
127.0.0.1:6379> info Modules
## 输出
# Modules
# Modules
module:name=ReJSON,ver=20816,api=1,filters=0,usedby=[],using=[],options=[handle-io-errors]
module:name=bf,ver=20817,api=1,filters=0,usedby=[],using=[],options=[handle-io-errors]

127.0.0.1:6379> MODULE LIST
# 输出
1) 1) "name"
2) "ReJSON"
3) "ver"
4) (integer) 20816
5) "path"
6) "/usr/local/soft/redis-7.4.7/modules/rejson.so"
7) "args"
8) (empty array)
2) 1) "name"
2) "bf"
3) "ver"
4) (integer) 20817
5) "path"
6) "/usr/local/soft/redis-7.4.7/modules/redisbloom.so"
7) "args"
8) (empty array)