The words written in the front
This paper is compiled from the documents and videos that the author can search by himself. Since the raw materials are all in English (see the reference document at the end of the article for specific materials), there are inevitable understanding errors. Welcome everyone to discuss and point out my mistakes.
JSON is a lightweight data interchange format, and my company uses THIS data format for information exchange between the front and back ends.
Redis is also essential middleware for software development to improve performance.
Putting JSON data into the Redis database is also an inevitable operation.
This is the first article in the series, mainly through Redis to JSON traditional operations, thus leading to Redis JSON this article.
I’ll start with two traditional ways to manipulate JSON data in Redis.
Redis is the traditional way to manipulate JSON
Raw String A pure String
Put JSON data into Redis as a string.
Method of use
# placed JSON
> set tag '{"1":{"tag":"GOAL_SELECT_TAG","execTs":1640676319},"2":{"tag":"GOAL_SELECT_TAG","execTs":1640676319}}'
OK
# get JSON
> get tag
{"1": {"tag":"GOAL_SELECT_TAG"."execTs":1640676319},"2": {"tag":"GOAL_SELECT_TAG"."execTs":1640676319}}
Copy the code
advantages
- Small memory footprint
disadvantages
JSON
Key-level access and modification of data is not supported. usingstring
Form, can only be obtained in full
Hash data structure
HASH the JSON data into Redis.
Method of use
# placed JSON
> hset h name zm age 16
2
# get JSON
> hgetall h
name
zm
age
16
> hget h name
zm
Copy the code
advantages
- provide
JSON
Key level access and modification, and the time complexity isO(1)
disadvantages
JSON
The value of a key can only be a string or numeric valueHASH
Is stored in the same way,JSON
Data can only have one level and cannot be nested. Such as
{
"1": {
"tag": "GOAL_SELECT_TAG"."execTs": 1640676319
},
"2": {
"tag": "GOAL_SELECT_TAG"."execTs": 1640676319}}Copy the code
- Occupies less memory
string
big
Redis JSON
Redis JSON is one of Redis Modules.
Redis Module
Redis JSON
Just like other Redis data types,
Redis JSON
A new type that stores data in binary form in nodes of the tree (ReJSON stores the data in binary format in the tree’s nodes
). See the actual operation below for details.Redis JSON
Offers a range ofAPI
To operateThe JSON data
.
Redis JSON startup and getting started
Operating environment setup
- Go to theredis cloudSign up, apply for the free database, and add
Redis JSON
As well asRedis Search
twoModules
. - Use terminal or client software to connect to the database based on the generated database configuration items
Redis
.
This way, we can perform Redis JSON operations.
At the same time, two bugs were discovered
Redis Cloud
A spelling error in
Red
Client error
aRedis JSON
Example command
JSON.SET key $ '{"foo":"bar", "ans": 42}'
Copy the code
JSON.SET
Set:JSON
The command.Redis JSON
All commands need to be prefixed with JSONkey
: specifickey
The name- $: indicates the root path. In order to obtain
JSON
Key values at different levels in,JSON
Has its own set of query language, seeJSONPath '{"foo":"bar", "ans": 42}'
: JSON string
The Redis JSON command is different from the normal Redis command in that there is a Path. The Path is used to parse JSON data easily and uniformly.
There are at least two standards for JSON paths … which means there is no standard for JSON paths.
Redis JSON command
Common Commands
You are given a JSON data
{
"1": {
"tag": "GOAL_SELECT_TAG"."execTs": 1640676319
},
"2": {
"tag": "GOAL_SELECT_TAG"."execTs": 1640676319}}Copy the code
- Set up the
JSON
2022- 01- 10 20:22:44 JSON.SET tag $ '{"1":{"tag":"GOAL_SELECT_TAG","execTs":1640676319},"2":{"tag":"GOAL_SELECT_TAG","execTs":1640676319}}'
OK
Copy the code
- The key to
1
The content of the
2022- 01- 11 21:37:36 JSON. GET the tag $1.
[{"tag":"GOAL_SELECT_TAG"."execTs":1640676319}]
Copy the code
- The key to
2
thetag
content
2022- 01- 11 21:38:45 JSON. GET the tag $2..tag
["GOAL_SELECT_TAG"]
Copy the code
- Traversal access
tag
Value, returns an array
2022- 01- 10 20:23:01 JSON. GET the tag $.. tag ["GOAL_SELECT_TAG"."GOAL_SELECT_TAG"]
Copy the code
- Increase or decrease the key to zero
1
theexecTs
The numerical
/ / to get
2022- 01- 12 18:34:49 JSON. GET the tag $1.
[{"tag":"GOAL_SELECT_TAG"."execTs":1640676980}]
/ / by 980
2022- 01- 12 18:35:00 JSON. NUMINCRBY tag $1..execTs - 980.
// Latest results
[1640676000]
Copy the code
Command summary
- The corresponding
JSON
Different components ofRedis JSON
Provide corresponding commands - According to the command name you can guess its function, according to the specific needs of each, then refer to the official documentation.
conclusion
Redis JSON
To be able to operateJSON
Data exists.- The use of commands is common
Redis
The difference in command is thatPath
The concept of. - For better operation
JSON
The data,Path
It is unavoidable. You can learnThe use of JSONPath. - For a
Redis
In addition to functions, the most important thing is the time complexity of commands. But itsThe official documentationThe language is not clear, so this article is to explain, so as not to delay everyone.
This is the first Redis JSON article, and we’ll be looking for more on actual projects, command time complexity, overall performance, and more.
Reference documentation
The official documentation
RedisJSON — pulando JSON como tipo Nativo no Redis
Redis as a JSON store
JSON in Redis – When to use RedisJSON
Redis JSON, from Redis Labs
RedisJSON + RediSearch as a real-time document database, Redis Labs
RedisJSON Explained
RedisJSON: Public Preview & Performance Benchmarking