学习

如何使用 Python 在 Redis 中查询图数据

Ajeet Raina
作者
Ajeet Raina, Redis 前开发者增长经理
停产通知

Redis 正在逐步淘汰 RedisGraph这篇博文 解释了这一决定的原因以及对现有 Redis 客户和社区成员的影响。

支持将于 2025 年 1 月 31 日结束。

RedisGraph 是最快的图数据库,它可以实时处理复杂的图操作,速度比任何其他图数据库快 10 倍到 600 倍。展示您的数据通过包括 RedisInsight、Linkurious 和 Graphileon 在内的多个可视化集成如何连接。使用行业标准 Cypher 查询语言查询图,并轻松从应用程序代码中使用图功能。

RedisGraph Python 客户端#

'redisgraph-py' 是一个软件包,它允许查询扩展了 RedisGraph 模块的 Redis 数据库中的图数据。该软件包使用 RedisGraph 的 API 扩展了 redisgraph-py 的接口

按照以下步骤开始使用 RedisGraph 和 Python

步骤 1. 运行 Redis Stack Docker 容器#

 docker run -p 6379:6379 --name redis/redis-stack

步骤 2. 验证是否加载了 RedisGraph 模块#

 info modules
 # Modules
 module:name=graph,ver=20405,api=1,filters=0,usedby=[],using=[],options=[]

步骤 3. 加载 Python 模块#

 pip install redisgraph

步骤 4. 编写 Python 代码#

 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()

步骤 5. 执行 Python 脚本#

  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)>

步骤 6. 监控图查询#

 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 查询图数据。

步骤 7. 安装 RedisInsight#

运行 RedisInsight 容器。最简单的方法是运行以下命令

 docker run -d -v redisinsight:/db -p 8001:8001 redislabs/redisinsight:latest

步骤 8. 访问 RedisInsight#

接下来,将您的浏览器指向 https://localhost:8001。

步骤 9. 运行图查询#

您可以使用 limit 子句来限制查询返回的记录数量

MATCH (n) RETURN n LIMIT 1

步骤 10. 运行没有 LIMIT 的图查询#

MATCH (n) RETURN n

参考#

  • 在 快速入门 教程中了解更多关于 RedisGraph 的信息。