Redis 正在逐步淘汰 RedisGraph. 这篇博文 解释了这一决定的原因以及对现有 Redis 客户和社区成员的影响。
支持将于 2025 年 1 月 31 日结束。
RedisGraph 是最快的图数据库,它可以实时处理复杂的图操作,速度比任何其他图数据库快 10 倍到 600 倍。展示您的数据通过包括 RedisInsight、Linkurious 和 Graphileon 在内的多个可视化集成如何连接。使用行业标准 Cypher 查询语言查询图,并轻松从应用程序代码中使用图功能。
'redisgraph-py' 是一个软件包,它允许查询扩展了 RedisGraph 模块的 Redis 数据库中的图数据。该软件包使用 RedisGraph 的 API 扩展了 redisgraph-py 的接口
按照以下步骤开始使用 RedisGraph 和 Python
docker run -p 6379:6379 --name redis/redis-stack
info modules
# Modules
module:name=graph,ver=20405,api=1,filters=0,usedby=[],using=[],options=[]
pip install redisgraph
import redis
from redisgraph import Node, Edge, Graph, Path
r = redis.Redis(host='localhost', port=6379)
redis_graph = Graph('social', r)
john = Node(label='person', properties={'name': 'John Doe', 'age': 33, 'gender': 'male', 'status': 'single'})
redis_graph.add_node(john)
japan = Node(label='country', properties={'name': 'Japan'})
redis_graph.add_node(japan)
edge = Edge(john, 'visited', japan, properties={'purpose': 'pleasure'})
redis_graph.add_edge(edge)
redis_graph.commit()
query = """MATCH (p:person)-[v:visited {purpose:"pleasure"}]->(c:country)
RETURN p.name, p.age, v.purpose, c.name"""
result = redis_graph.query(query)
# Print resultset
result.pretty_print()
# Use parameters
params = {'purpose':"pleasure"}
query = """MATCH (p:person)-[v:visited {purpose:$purpose}]->(c:country)
RETURN p.name, p.age, v.purpose, c.name"""
result = redis_graph.query(query, params)
# Print resultset
result.pretty_print()
# Use query timeout to raise an exception if the query takes over 10 milliseconds
result = redis_graph.query(query, params, timeout=10)
# Iterate through resultset
for record in result.result_set:
person_name = record[0]
person_age = record[1]
visit_purpose = record[2]
country_name = record[3]
query = """MATCH p = (:person)-[:visited {purpose:"pleasure"}]->(:country) RETURN p"""
result = redis_graph.query(query)
# Iterate through resultset
for record in result.result_set:
path = record[0]
print(path)
# All done, remove graph.
redis_graph.delete()
python3 test.py
+-----------+----------+--------------+-----------+
| b'p.name' | b'p.age' | b'v.purpose' | b'c.name' |
+-----------+----------+--------------+-----------+
| John Doe | 33 | pleasure | Japan |
+-----------+----------+--------------+-----------+
Cached execution 0.0
internal execution time 3.3023
+-----------+----------+--------------+-----------+
| b'p.name' | b'p.age' | b'v.purpose' | b'c.name' |
+-----------+----------+--------------+-----------+
| John Doe | 33 | pleasure | Japan |
+-----------+----------+--------------+-----------+
Cached execution 0.0
internal execution time 0.2475
<(0)-[0]->(1)>
127.0.0.1:6379> monitor
OK
1632661901.024018 [0 172.17.0.1:61908] "GRAPH.QUERY" "social" "CREATE (youkjweasb:person{age:33,gender:\"male\",name:\"John Doe\",status:\"single\"}),(jilbktlmgw:country{name:\"Japan\"}),(youkjweasb:person{age:33,gender:\"male\",name:\"John Doe\",status:\"single\"})-[:visited{purpose:\"pleasure\"}]->(jilbktlmgw:country{name:\"Japan\"})" "--compact"
1632661901.025810 [0 172.17.0.1:61908] "GRAPH.QUERY" "social" "MATCH (p:person)-[v:visited {purpose:\"pleasure\"}]->(c:country)\n\t\t RETURN p.name, p.age, v.purpose, c.name" "--compact"
1632661901.027485 [0 172.17.0.1:61908] "GRAPH.QUERY" "social" "CYPHER purpose=\"pleasure\" MATCH (p:person)-[v:visited {purpose:$purpose}]->(c:country)\n\t\t RETURN p.name, p.age, v.purpose, c.name" "--compact"
1632661901.029539 [0 172.17.0.1:61908] "GRAPH.QUERY" "social" "CYPHER purpose=\"pleasure\" MATCH (p:person)-[v:visited {purpose:$purpose}]->(c:country)\n\t\t RETURN p.name, p.age, v.purpose, c.name" "--compact" "timeout" "10"
1632661901.030965 [0 172.17.0.1:61908] "GRAPH.QUERY" "social" "MATCH p = (:person)-[:visited {purpose:\"pleasure\"}]->(:country) RETURN p" "--compact"
1632661901.032250 [0 172.17.0.1:61908] "GRAPH.RO_QUERY" "social" "CALL db.labels()" "--compact"
1632661901.033323 [0 172.17.0.1:61908] "GRAPH.RO_QUERY" "social" "CALL db.propertyKeys()" "--compact"
1632661901.034589 [0 172.17.0.1:61908] "GRAPH.RO_QUERY" "social" "CALL db.relationshipTypes()" "--compact"
1632661901.035625 [0 172.17.0.1:61908] "GRAPH.DELETE" "social"
让我们注释掉最后一行。尝试通过 RedisInsight 查询图数据。
运行 RedisInsight 容器。最简单的方法是运行以下命令
docker run -d -v redisinsight:/db -p 8001:8001 redislabs/redisinsight:latest
接下来,将您的浏览器指向 https://localhost:8001。
您可以使用 limit 子句来限制查询返回的记录数量
MATCH (n) RETURN n LIMIT 1
MATCH (n) RETURN n