RedisJSON 与 RedisGraph 基础
探索在 Redis 中存储和查询 JSON 文档,以及利用图数据库功能
RedisJSON 与 RedisGraph 基础 是 CoddyKit 上的免费 Redis Caching & Messaging (Pub/Sub, Streams) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Redis Caching & Messaging (Pub/Sub, Streams) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Unlock New Data Types
Redis is incredibly versatile, but sometimes you need more specialized data handling. That's where Redis Modules come in!
These modules extend Redis's core functionality, allowing it to act as a document database, a graph database, and much more.
Introducing RedisJSON
RedisJSON is a module that lets you store, update, and retrieve JSON documents directly within Redis. This is great for managing semi-structured data like user profiles, product catalogs, or configuration settings.
- Native JSON support: Store complex JSON objects.
- Atomic operations: Update parts of a JSON document efficiently.
- Powerful querying: Retrieve data using JSONPath expressions.
Storing JSON with JSON.SET
To store a JSON document, we use the JSON.SET command. You provide a key, a path (usually $ for the root), and the JSON string.
Try adding a simple user profile:
JSON.SET user:profile:1 $ '{"name": "Emma", "age": 28, "city": "London"}'Retrieving JSON with JSON.GET
The JSON.GET command retrieves your JSON. You can get the entire document or specify a JSONPath to fetch only specific fields.
Let's get the full profile and then just Emma's name:
JSON.GET user:profile:1
JSON.GET user:profile:1 $.nameUpdating JSON Fields
One of RedisJSON's strengths is updating specific parts of a document without fetching and rewriting the whole thing. Use JSON.SET with a precise path.
Update Emma's age and add a new hobby:
JSON.SET user:profile:1 $.age 29
JSON.SET user:profile:1 $.hobbies '["reading", "hiking"]'
JSON.GET user:profile:1Introducing RedisGraph
RedisGraph is another powerful module that turns Redis into a graph database. Graph databases are excellent for modeling relationships between data, like social networks, recommendation engines, or fraud detection.
Instead of tables, you work with nodes (entities) and edges (relationships).
Graph Concepts
In RedisGraph, you'll use Cypher, a declarative graph query language, to interact with your data.
- Nodes: Represent entities (e.g., Person, Product).
- Labels: Categorize nodes (e.g.,
:Person). - Properties: Attributes of nodes/edges (e.g.,
name: 'Bob'). - Edges: Represent relationships (e.g.,
-[:FRIENDS_WITH]->).
Creating Graph Data
The GRAPH.QUERY command lets you execute Cypher queries. Let's create a simple social graph with two people and a 'FRIENDS_WITH' relationship.
Notice the graph name 'social' is used to identify our graph.
GRAPH.QUERY social "CREATE (:Person {name: 'Liam'})-[:FRIENDS_WITH]->(:Person {name: 'Olivia'})"Querying Graph Relationships
You can query the graph to find patterns and relationships. The MATCH clause finds the pattern, and RETURN specifies what data to retrieve.
Find who Liam is friends with:
GRAPH.QUERY social "MATCH (p:Person {name: 'Liam'})-[:FRIENDS_WITH]->(f:Person) RETURN p.name, f.name"RedisJSON Path Challenge
You have a RedisJSON document stored under the key product:details:123. The document looks like this:
{
"name": "Wireless Earbuds",
"specs": {
"color": "Black",
"battery_life": "8 hours",
"features": ["Noise Cancelling", "Waterproof"]
}
}Which command correctly retrieves the 'Noise Cancelling' feature?
Recap: JSON & Graphs
You've explored how RedisJSON enables efficient storage and manipulation of JSON documents using commands like JSON.SET and JSON.GET with JSONPath.
You also learned about RedisGraph, a powerful module for building and querying graph structures using Cypher, perfect for modeling relationships between data.
These modules significantly expand Redis's capabilities, letting it tackle a broader range of data modeling challenges!
常见问题解答
「RedisJSON 与 RedisGraph 基础」课时是免费的吗?
是的 — 「RedisJSON 与 RedisGraph 基础」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Redis Caching & Messaging (Pub/Sub, Streams) 课程的其余内容,请升级到 CoddyKit PRO。 Redis Caching & Messaging (Pub/Sub, Streams) 课程共包含 4 节课。
「RedisJSON 与 RedisGraph 基础」这节课中我会学到什么?
探索在 Redis 中存储和查询 JSON 文档,以及利用图数据库功能 你通过在浏览器中直接运行的动手代码来练习 Redis Caching & Messaging (Pub/Sub, Streams),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Redis Caching & Messaging (Pub/Sub, Streams) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Redis Caching & Messaging (Pub/Sub, Streams) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「RedisJSON 与 RedisGraph 基础」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Redis Caching & Messaging (Pub/Sub, Streams) 课中编写并运行代码吗?
能。每节 Redis Caching & Messaging (Pub/Sub, Streams) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Redis 模块概览
- 使用 RediSearch 实现全文搜索
- RedisJSON 与 RedisGraph 基础
- 使用 RedisTimeSeries 处理时间序列