Redis JSON介绍和命令大全

Redis JSON

redis在6.x开始支持json
注意:json可以说是在http接口返回值的网红,之所以说是网红,是因为之前不是json,之后当然也就未可知也。
注意我上面只说了在http接口返回值的场景。实际上在内部的rpc调用,也有用json传输的,不过这比较小众。
更多使用在存储数据场景下,也是泛滥的场景,如mysql、es、mongodb、redis中,json似乎包罗万象了。
⚠️注意!这不是好事,灵活性的提升必定伴随着其他如规范性、稳定性等问题的到来,
如果还没有到来,那肯定是时间还不够长、业务还没有增长。

redis官网介绍 https://redis.io/docs/latest/develop/data-types/json/

先说说JSON是什么

JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation)

再说说JSON Path

JSONPath是一种用于从JSON文档中提取信息的查询语言,类似于XML的XPath。

先推荐两个网站

JsonPath语法 https://goessner.net/articles/JsonPath/
JsonPath在线 https://jsonpath.com/
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

JSONPath JAVA clents

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.7.0</version> <!-- 请检查是否有更新的版本 -->
</dependency>

Redis JSON 安装

  1. 使用redis-stack
    docker run -it --name redis-stack -p 6379:6379 redis/redis-stack:latest
  2. 使用redis 6.x及以上,配置loadMoudle
    本文使用redis-stack,如下图,可以看到启动的时候自动加载了json模块,
    除此之外还有timeseries、search等模块

在这里插入图片描述

内存

官网内存 https://redis.io/docs/latest/develop/data-types/json/ram/
在这里插入图片描述
可以看到json的内存还是挺恐怖的,哈哈哈,用不上还是用string吧…
keyvalue都占用内存

json命令语法

json命令大全 https://redis.io/docs/latest/commands/?group=json
在这里插入图片描述

命令url

indexcommandurl
0JSON.ARRAPPENDhttps://redis.io/docs/latest/commands/json.arrappend/
1JSON.ARRINDEXhttps://redis.io/docs/latest/commands/json.arrindex/
2JSON.ARRINSERThttps://redis.io/docs/latest/commands/json.arrinsert/
3JSON.ARRLENhttps://redis.io/docs/latest/commands/json.arrlen/
4JSON.ARRPOPhttps://redis.io/docs/latest/commands/json.arrpop/
5JSON.ARRTRIMhttps://redis.io/docs/latest/commands/json.arrtrim/
6JSON.CLEARhttps://redis.io/docs/latest/commands/json.clear/
7JSON.DEBUGhttps://redis.io/docs/latest/commands/json.debug/
8JSON.DEBUG MEMORYhttps://redis.io/docs/latest/commands/json.debug-memory/
9JSON.DELhttps://redis.io/docs/latest/commands/json.del/
10JSON.FORGEThttps://redis.io/docs/latest/commands/json.forget/
11JSON.GEThttps://redis.io/docs/latest/commands/json.get/
12JSON.MERGEhttps://redis.io/docs/latest/commands/json.merge/
13JSON.MGEThttps://redis.io/docs/latest/commands/json.mget/
14JSON.MSEThttps://redis.io/docs/latest/commands/json.mset/
15JSON.NUMINCRBYhttps://redis.io/docs/latest/commands/json.numincrby/
16JSON.NUMMULTBYhttps://redis.io/docs/latest/commands/json.nummultby/
17JSON.OBJKEYShttps://redis.io/docs/latest/commands/json.objkeys/
18JSON.OBJLENhttps://redis.io/docs/latest/commands/json.objlen/
19JSON.RESPhttps://redis.io/docs/latest/commands/json.resp/
20JSON.SEThttps://redis.io/docs/latest/commands/json.set/
21JSON.STRAPPENDhttps://redis.io/docs/latest/commands/json.strappend/
22JSON.STRLENhttps://redis.io/docs/latest/commands/json.strlen/
23JSON.TOGGLEhttps://redis.io/docs/latest/commands/json.toggle/
24JSON.TYPEhttps://redis.io/docs/latest/commands/json.type/

命令解释

JSON.ARRAPPEND
commandJSON.ARRAPPEND
syntaxJSON.ARRAPPEND key [path] value [value ...]
description

Append the json values into the array at path after the last element in it

time complexityO(1) when path is evaluated to a single value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.arrappend/
JSON.ARRINDEX
commandJSON.ARRINDEX
syntaxJSON.ARRINDEX key path value [start [stop]]
description

Search for the first occurrence of a JSON value in an array

time complexityO(N) when path is evaluated to a single value where N is the size of the array, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.arrindex/
JSON.ARRINSERT
commandJSON.ARRINSERT
syntaxJSON.ARRINSERT key path index value [value ...]
description

Insert the json values into the array at path before the index (shifts to the right)

time complexityO(N) when path is evaluated to a single value where N is the size of the array, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.arrinsert/
JSON.ARRLEN
commandJSON.ARRLEN
syntaxJSON.ARRLEN key [path]
description

Report the length of the JSON array at path in key

time complexityO(1) where path is evaluated to a single value, O(N) where path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.arrlen/
JSON.ARRPOP
commandJSON.ARRPOP
syntaxJSON.ARRPOP key [path [index]]
description

Remove and return an element from the index in the array

time complexityO(N) when path is evaluated to a single value where N is the size of the array and the specified index is not the last element, O(1) when path is evaluated to a single value and the specified index is the last element, or O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.arrpop/
JSON.ARRTRIM
commandJSON.ARRTRIM
syntaxJSON.ARRTRIM key path start stop
description

Trim an array so that it contains only the specified inclusive range of elements

time complexityO(N) when path is evaluated to a single value where N is the size of the array, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.arrtrim/
JSON.CLEAR
commandJSON.CLEAR
syntaxJSON.CLEAR key [path]
description

Clear container values (arrays/objects) and set numeric values to 0

time complexityO(N) when path is evaluated to a single value where N is the size of the values, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 2.0.0
urlhttps://redis.io/docs/latest/commands/json.clear/
JSON.DEBUG MEMORY
commandJSON.DEBUG MEMORY
syntaxJSON.DEBUG MEMORY key [path]
description

Report a value’s memory usage in bytes

time complexityO(N) when path is evaluated to a single value, where N is the size of the value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.debug-memory/
JSON.DEBUG
commandJSON.DEBUG
syntaxJSON.DEBUG
description

This is a container command for debugging related tasks.

time complexityN/A
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.debug/
JSON.DEL
commandJSON.DEL
syntaxJSON.DEL key [path]
description

Delete a value

time complexityO(N) when path is evaluated to a single value where N is the size of the deleted value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.del/
JSON.FORGET
commandJSON.FORGET
syntaxJSON.FORGET key [path]
description

See JSON.DEL.

time complexityO(N) when path is evaluated to a single value where N is the size of the deleted value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.forget/
JSON.GET
commandJSON.GET
syntaxJSON.GET key [INDENTÂ indent] [NEWLINEÂ newline] [SPACEÂ space] [path [path ...]]
description

Return the value at path in JSON serialized form

time complexityO(N) when path is evaluated to a single value where N is the size of the value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.get/
JSON.MERGE
commandJSON.MERGE
syntaxJSON.MERGE key path value
description

Merge a given JSON value into matching paths. Consequently, JSON values at matching paths are updated, deleted, or expanded with new children.

time complexityO(M+N) when path is evaluated to a single value where M is the size of the original value (if it exists) and N is the size of the new value, O(M+N) when path is evaluated to multiple values where M is the size of the key and N is the size of the new value * the number of original values in the key
available inRedis Stack / JSON 2.6.0
urlhttps://redis.io/docs/latest/commands/json.merge/
JSON.MGET
commandJSON.MGET
syntaxJSON.MGET key [key ...] path
description

Return the values at path from multiple key arguments

time complexityO(M*N) when path is evaluated to a single value where M is the number of keys and N is the size of the value, O(N1+N2+…+Nm) when path is evaluated to multiple values where m is the number of keys and Ni is the size of the i-th key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.mget/
JSON.MSET
commandJSON.MSET
syntaxJSON.MSET key path value [key path value ...]
description

Set or update one or more JSON values according to the specified key-path-value triplets

time complexityO(K*(M+N)) where k is the number of keys in the command, when path is evaluated to a single value where M is the size of the original value (if it exists) and N is the size of the new value, or O(K*(M+N)) when path is evaluated to multiple values where M is the size of the key and N is the size of the new value * the number of original values in the key
available inRedis Stack / JSON 2.6.0
urlhttps://redis.io/docs/latest/commands/json.mset/
JSON.NUMINCRBY
commandJSON.NUMINCRBY
syntaxJSON.NUMINCRBY key path value
description

Increment the number value stored at path by number

time complexityO(1) when path is evaluated to a single value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.numincrby/
JSON.OBJKEYS
commandJSON.OBJKEYS
syntaxJSON.OBJKEYS key [path]
description

Return the keys in the object that’s referenced by path

time complexityO(N) when path is evaluated to a single value, where N is the number of keys in the object, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.objkeys/
JSON.OBJLEN
commandJSON.OBJLEN
syntaxJSON.OBJLEN key [path]
description

Report the number of keys in the JSON object at path in key

time complexityO(1) when path is evaluated to a single value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.objlen/
JSON.SET
commandJSON.SET
syntaxJSON.SET key path value [NX | XX]
description

Set the JSON value at path in key

time complexityO(M+N) when path is evaluated to a single value where M is the size of the original value (if it exists) and N is the size of the new value, O(M+N) when path is evaluated to multiple values where M is the size of the key and N is the size of the new value * the number of original values in the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.set/
JSON.STRAPPEND
commandJSON.STRAPPEND
syntaxJSON.STRAPPEND key [path] value
description

Append the json-string values to the string at path

time complexityO(1) when path is evaluated to a single value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.strappend/
JSON.STRLEN
commandJSON.STRLEN
syntaxJSON.STRLEN key [path]
description

Report the length of the JSON String at path in key

time complexityO(1) when path is evaluated to a single value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.strlen/
JSON.TOGGLE
commandJSON.TOGGLE
syntaxJSON.TOGGLE key path
description

Toggle a Boolean value stored at path

time complexityO(1) when path is evaluated to a single value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 2.0.0
urlhttps://redis.io/docs/latest/commands/json.toggle/
JSON.TYPE
commandJSON.TYPE
syntaxJSON.TYPE key [path]
description

Report the type of JSON value at path

time complexityO(1) when path is evaluated to a single value, O(N) when path is evaluated to multiple values, where N is the size of the key
available inRedis Stack / JSON 1.0.0
urlhttps://redis.io/docs/latest/commands/json.type/

  • JSON.ARRAPPEND
127.0.0.1:6379> json.set blog $ '{"user":{"id":222078,"nick":"TOM"},"id":1000,"content":"一起学习redis json吧","delete":false,"like":300,"tags":["redis","json"]}'
127.0.0.1:6379> json.arrappend blog $.tags '"backend"' '"midware"'
4
127.0.0.1:6379> json.get blog INDENT "\t" NEWLINE "\n" SPACE " " $
[
	{
		"user": {
			"id": 222078,
			"nick": "TOM"
		},
		"id": 1000,
		"content": "一起学习redis json吧",
		"delete": false,
		"like": 300,
		"tags": [
			"redis",
			"json",
			"backend",
			"midware"
		]
	}
]
127.0.0.1:6379> 
  • JSON.ARRINSERT
  • JSON.STRAPPEND

  • JSON.ARRPOP
  • JSON.ARRTRIM
  • JSON.CLEAR
  • JSON.DEL
  • JSON.FORGET

  • JSON.SET
  • JSON.MSET
  • JSON.NUMINCRBY
  • JSON.NUMMULTBY
  • JSON.MERGE
  • JSON.TOGGLE

  • JSON.ARRLEN
  • JSON.ARRINDEX
    127.0.0.1:6379> JSON.SET tom $ '["black","silver"]'
    OK
    127.0.0.1:6379>
    127.0.0.1:6379>
    127.0.0.1:6379>
    127.0.0.1:6379> JSON.ARRINDEX tom $ '"black"'
    1) (integer) 0
       127.0.0.1:6379> JSON.ARRINDEX tom $ '"silver"'
    1) (integer) 1
    
  • JSON.DEBUG
  • JSON.DEBUG MEMORY
  • JSON.GET
  • JSON.MGET
  • JSON.OBJKEYS
  • JSON.OBJLEN
  • JSON.RESP (deprecated)
  • JSON.STRLEN
  • JSON.TYPE

redis jsonclients

https://github.com/RedisJSON/RedisJSON?tab=readme-ov-file

redis json的使用场景 https://redis.io/docs/latest/develop/data-types/json/use_cases/

  • 从json中查询或搜索
  • 原子部分更新
### Redis 中处理 JSON 数据的方式 Redis 是一种高性能的键值存储系统,广泛用于缓存、消息队列其他场景。随着对结构化数据需求的增长,Redis 提供了多种方法来处理 JSON 数据。 #### 1. 原始字符串存储 最简单的方法是将 JSON 数据作为字符串存储在 Redis 的 `STRING` 类型中[^2]。这种方式适用于简单的应用场景,但存在一些局限性: - 需要手动序列化反序列化 JSON 数据。 - 不支持直接访问嵌套字段或执行复杂查询。 示例代码如下: ```python import redis import json # 连接到 Redis r = redis.Redis(host='localhost', port=6379, db=0) # 存储 JSON 数据 data = {"name": "Alice", "age": 25} r.set("user:1", json.dumps(data)) # 获取并解析 JSON 数据 json_data = r.get("user:1") parsed_data = json.loads(json_data.decode('utf-8')) print(parsed_data["name"]) # 输出 Alice ``` --- #### 2. 使用 Lua 脚本增强功能 通过 Redis 支持的 Lua 脚本,可以在服务器端对 JSON 数据进行更复杂的操作[^3]。例如,更新特定字段而无需完全加载整个对象: 示例脚本: ```lua eval "local json_str = redis.call('get', KEYS[1]); \ if json_str then \ local json_obj = cjson.decode(json_str); \ json_obj['id'] = tonumber(ARGV[1]); \ redis.call('set', KEYS[1], cjson.encode(json_obj)); \ return 'ok'; \ else \ return 'fail'; \ end;" 1 test_json 333 ``` 此脚本实现了对指定键中的 JSON 数据的局部更新,减少了客户端与 Redis 之间的数据传输开销。 --- #### 3. 利用 RedisJSON 扩展模块 为了更好地支持 JSON 数据,Redis 社区推出了 **RedisJSON** 模块[^1]。该模块允许用户以原生形式存储操作 JSON 数据,并提供了一系列专用命令,如 `JSON.SET`, `JSON.GET`, `JSON.MGET` 等。 ##### 主要特性: - **高效查询**: 可以直接读取 JSON 文档中的任意路径节点。 - **原子操作**: 支持针对单个字段的安全并发修改。 - **兼容性强**: 符合标准 JSON 格式,易于与其他工具集成。 安装完成后,可以通过以下方式使用 RedisJSON: ```bash # 设置 JSON 数据 JSON.SET user:1 $ '{"name":"Bob","address":{"city":"New York"}}' # 查询部分字段 JSON.GET user:1 .name # 返回 Bob JSON.GET user:1 .address.city # 返回 New York # 更新某个字段 JSON.SET user:1 .age 30 # 添加/覆盖 age 属性为 30 ``` --- #### 4. 结合 Modules 功能扩展能力 自 Redis 4.0 开始引入的 modules 系统使得开发者能够创建定制化的插件[^4]。除了官方维护的 RedisJSON 外,还有其他第三方模块可供选择,进一步增强了 Redis 对不同类型数据的支持范围。 --- ### 总结 对于需要频繁操作 JSON 数据的应用程序来说,采用 RedisJSON 或者基于 Lua 编写的解决方案都可以显著提高效率。具体选用哪种方案取决于实际业务需求以及团队的技术栈偏好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值