Using JSON on ElastiCache Redis

ElastiCache for Redis 支持原生 JavaScript 對象表示法 (JSON) 格式,不過使用上有些要注意的地方。

Jerry’s Notes
What’s next?
9 min readAug 16, 2022

--

  1. ElastiCache Redis engine version 6.2.6 or later. 若您目前使用的版本低於這個版本,是不支援的。
  2. 使用 > info Modules 命令是找不到 RedisJSON 模組的。因為 ElastiCache Redis 目前不支援 Modules 的功能,而目前 ElastiCache Redis 所提供的 JSON 功能,也不是透過 Modules 功能來達到這個目的的。
  3. 多個兩個新的 CloudWatch 指標: JsonBasedCmds and JsonBasedCmdsLatency,可以用於觀察執行命令次數及延遲狀況。
  4. 有支援的命令清單,請參考下面連結。

https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/json-list-commands.html

測試記錄:

  1. 新創建的 ElastiCache Redis 若選擇 6.2 版本時,預設會使用 6.2.6 以後的版本來建立,也沒有更小版號可以去選。

!! 若您的 ElastiCache Redis 版本於小 6.2.6 的話,可以透過備份、後還原的方式,來達到這個目的,或是透過更新 Engine 版本到最新的方式。

### 查詢當前 Redis Engine version ###
$ redis-cli -c -h xxx.use1.cache.amazonaws.com info server | grep redis_version
----
redis_version:6.2.6
----
### 使用 AWS CLI 來執行更新 Engine 版本 ###
$ aws elasticache modify-replication-group --replication-group-id single --engine-version 6.2.6 --region us-west-2
An error occurred (InvalidParameterValue) when calling the ModifyReplicationGroup operation: Specific version selection is not allowed for '6.2.6', please use '6.2'!!! 您不能指定特定的小版本,所以要改用 6.2 執行,過當您在 ElastiCache Console 上執行時,Redis 6 就只有兩個版本可以選(6.0 or 6.2),無法指定小版本。$ aws elasticache modify-replication-group --replication-group-id single --engine-version 6.2 --region us-west-2

2. 寫入 JSON 數據https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/json-set.html

x.cache.amazonaws.com:6379> JSON.SET k1 . '{"a":{"a":1, "b":2, "c":3}}'
OK
x.cache.amazonaws.com:6379> JSON.SET k1 $.a.* '0' <- 更新特定資料。
OK
x.cache.amazonaws.com:6379> JSON.GET k1
"{\"a\":{\"a\":0,\"b\":0,\"c\":0}}"

3. 讀取 JSON 內特定資料
https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/json-get.html

### 寫入資料 ###
x.cache.amazonaws.com:6379> JSON.SET k1 . '{"firstName":"John","lastName":"Smith","age":27,"weight":135.25,"isAlive":true,"address":{"street":"21 2nd Street","city":"New York","state":"NY","zipcode":"10021-3100"},"phoneNumbers":[{"type":"home","number":"212 555-1234"},{"type":"office","number":"646 555-4567"}],"children":[],"spouse":null}'
OK

### 讀取特定資料 ###
x.cache.amazonaws.com:6379> JSON.GET k1 $.address.*
"[\"21 2nd Street\",\"New York\",\"NY\",\"10021-3100\"]"

### 讀取特定資料 ###

x.cache.amazonaws.com:6379> JSON.GET k1 $.firstName $.lastName $.age
"{\"$.firstName\":[\"John\"],\"$.lastName\":[\"Smith\"],\"$.age\":[27]}"

同場加映:

在自建的 Redis 上,載入 RedisJSON 模組,並做簡單的讀寫操作。

### 取得 RedisJSON 檔 ###
$ git clone https://github.com/RedisJSON/RedisJSON.git
$ git checkout v1.0.4
$ cd RedisJSON
$ make
....
make[1]: Leaving directory `/home/ec2-user/RedisJSON/src'
$ pwd
/home/ec2-user/RedisJSON
$ ls /home/ec2-user/RedisJSON/src/rejson.so
/home/ec2-user/RedisJSON/src/rejson.so
# Configuration file: redis.conf ###
loadmodule /home/ec2-user/RedisJSON/src/rejson.so
### Command-line option ###
$ /home/ec2-user/redis-stable/src/redis-server --loadmodule /home/ec2-user/RedisJSON/src/rejson.so
---
24334:C 16 Aug 2022 08:38:23.362 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
24334:C 16 Aug 2022 08:38:23.365 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=24334, just started
24334:C 16 Aug 2022 08:38:23.365 # Configuration loaded
....
24334:M 16 Aug 2022 08:38:23.368 # <ReJSON> JSON data type for Redis v1.0.4 [encver 0]
24334:M 16 Aug 2022 08:38:23.368 * Module 'ReJSON' loaded from /home/ec2-user/RedisJSON/src/rejson.so
24334:M 16 Aug 2022 08:38:23.369 * Loading RDB produced by version 6.2.5
....
----
### 確認有載入那一些模組 ###
127.0.0.1:6379> info Modules
# Modules
module:name=ReJSON,ver=10004,api=1,filters=0,usedby=[],using=[],options=[]
127.0.0.1:6379> MODULE LIST
1) 1) "name"
2) "ReJSON"
3) "ver"
4) (integer) 10004
### 寫入一筆 JSON 的數據 ###
127.0.0.1:6379> JSON.SET json_data . '{"name":"Jerry","age":30,"msg":"hello Json"}'
OK
### 讀取整筆資料 ###
127.0.0.1:6379> JSON.GET json_data
"{\"name\":\"Jerry\",\"age\":30,\"msg\":\"hello Json\"}"
### 讀取特定部份的資料 ###
127.0.0.1:6379> JSON.GET json_data .name
"\"Jerry\""
127.0.0.1:6379> JSON.GET json_data .age
"30"
127.0.0.1:6379> JSON.GET json_data .msg
"\"hello Json\""

--

--

Jerry’s Notes
What’s next?

An cloud support engineer focus on troubleshooting with customer reported issue ,and cloud solution architecture.