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

index command url
0 JSON.ARRAPPEND https://redis.io/docs/latest/commands/json.arrappend/
1 JSON.ARRINDEX https://redis.io/docs/latest/commands/json.arrindex/
2 JSON.ARRINSERT https://redis.io/docs/latest/commands/json.arrinsert/
3 JSON.ARRLEN https://redis.io/docs/latest/commands/json.arrlen/
4 JSON.ARRPOP https://redis.io/docs/latest/commands/json.arrpop/
5 JSON.ARRTRIM https://redis.io/docs/latest/commands/json.arrtrim/
6 JSON.CLEAR https://redis.io/docs/latest/commands/json.clear/
7 JSON.DEBUG https://redis.io/docs/latest/commands/json.debug/
8 JSON.DEBUG MEMORY https://redis.io/docs/latest/commands/json.debug-memory/
9 JSON.DEL https://redis.io/docs/latest/commands/json.del/
10 JSON.FORGET https://redis.io/docs/latest/commands/json.forget/
11 JSON.GET https://redis.io/docs/latest/commands/json.get/
12 JSON.MERGE https://redis.io/docs/latest/commands/json.merge/
13 JSON.MGET https://redis.io/docs/latest/commands/json.mget/
14 JSON.MSET https://redis.io/docs/latest/commands/json.mset/
15 JSON.NUMINCRBY https://redis.io/docs/latest/commands/json.numincrby/
16 JSON.NUMMULTBY https://redis.io/docs/latest/commands/json.nummultby/
17 JSON.OBJKEYS https://redis.io/docs/latest/commands/json.objkeys/
18 JSON.OBJLEN https://redis.io/docs/latest/commands/json.objlen/
19 JSON.RESP https://redis.io/docs/latest/commands/json.resp/
20 JSON.SET https://redis.io/docs/latest/commands/json.set/
21 JSON.STRAPPEND https://redis.io/docs/latest/commands/json.strappend/
22 JSON.STRLEN https://redis.io/docs/latest/commands/json.strlen/
23 JSON.TOGGLE https://redis.io/docs/latest/commands/json.toggle/
24 JSON.TYPE https://redis.io/docs/latest/commands/json.type/

命令解释

JSON.ARRAPPEND
command JSON.ARRAPPEND
syntax JSON.ARRAPPEND key [path] value [value ...]
description

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

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.arrappend/
JSON.ARRINDEX
command JSON.ARRINDEX
syntax JSON.ARRINDEX key path value [start [stop]]
description

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

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.arrindex/
JSON.ARRINSERT
command JSON.ARRINSERT
syntax JSON.ARRINSERT key path index value [value ...]
description

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

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.arrinsert/
JSON.ARRLEN
command JSON.ARRLEN
syntax JSON.ARRLEN key [path]
description

Report the length of the JSON array at path in key

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.arrlen/
JSON.ARRPOP
command JSON.ARRPOP
syntax JSON.ARRPOP key [path [index]]
description

Remove and return an element from the index in the array

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.arrpop/
JSON.ARRTRIM
command JSON.ARRTRIM
syntax JSON.ARRTRIM key path start stop
description

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

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.arrtrim/
JSON.CLEAR
command JSON.CLEAR
syntax JSON.CLEAR key [path]
description

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

time complexity O(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 in Redis Stack / JSON 2.0.0
url https://redis.io/docs/latest/commands/json.clear/
JSON.DEBUG MEMORY
command JSON.DEBUG MEMORY
syntax JSON.DEBUG MEMORY key [path]
description

Report a value’s memory usage in bytes

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.debug-memory/
JSON.DEBUG
command JSON.DEBUG
syntax JSON.DEBUG
description

This is a container command for debugging related tasks.

time complexity N/A
available in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.debug/
JSON.DEL
command JSON.DEL
syntax JSON.DEL key [path]
description

Delete a value

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.del/
JSON.FORGET
command JSON.FORGET
syntax JSON.FORGET key [path]
description

See JSON.DEL.

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.forget/
JSON.GET
command JSON.GET
syntax JSON.GET key [INDENTÂ indent] [NEWLINEÂ newline] [SPACEÂ space] [path [path ...]]
description

Return the value at path in JSON serialized form

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.get/
JSON.MERGE
command JSON.MERGE
syntax JSON.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 complexity O(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 in Redis Stack / JSON 2.6.0
url https://redis.io/docs/latest/commands/json.merge/
JSON.MGET
command JSON.MGET
syntax JSON.MGET key [key ...] path
description

Return the values at path from multiple key arguments

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.mget/
JSON.MSET
command JSON.MSET
syntax JSON.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 complexity O(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 in Redis Stack / JSON 2.6.0
url https://redis.io/docs/latest/commands/json.mset/
JSON.NUMINCRBY
command JSON.NUMINCRBY
syntax JSON.NUMINCRBY key path value
description

Increment the number value stored at path by number

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.numincrby/
JSON.OBJKEYS
command JSON.OBJKEYS
syntax JSON.OBJKEYS key [path]
description

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

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.objkeys/
JSON.OBJLEN
command JSON.OBJLEN
syntax JSON.OBJLEN key [path]
description

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

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.objlen/
JSON.SET
command JSON.SET
syntax JSON.SET key path value [NX | XX]
description

Set the JSON value at path in key

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.set/
JSON.STRAPPEND
command JSON.STRAPPEND
syntax JSON.STRAPPEND key [path] value
description

Append the json-string values to the string at path

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.strappend/
JSON.STRLEN
command JSON.STRLEN
syntax JSON.STRLEN key [path]
description

Report the length of the JSON String at path in key

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://redis.io/docs/latest/commands/json.strlen/
JSON.TOGGLE
command JSON.TOGGLE
syntax JSON.TOGGLE key path
description

Toggle a Boolean value stored at path

time complexity O(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 in Redis Stack / JSON 2.0.0
url https://redis.io/docs/latest/commands/json.toggle/
JSON.TYPE
command JSON.TYPE
syntax JSON.TYPE key [path]
description

Report the type of JSON value at path

time complexity O(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 in Redis Stack / JSON 1.0.0
url https://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中查询或搜索
  • 原子部分更新

猜你喜欢

转载自blog.csdn.net/xiaolixi199311/article/details/143076260