学习

如何在简单的步骤中从 CSV 输入构建 RedisGraph 数据库

Ajeet Raina
作者
Ajeet Raina, Redis 前开发者增长经理
生命周期结束通知

Redis 正在逐步淘汰 RedisGraph。 这篇博客文章 解释了做出此决定的动机以及对现有 Redis 客户和社区成员的影响。

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

RedisGraph 是速度最快的图数据库,能实时处理复杂的图操作,比任何其他图数据库快 10 倍至 600 倍。它通过 RedisInsight、Linkurious 和 Graphileon 等多种可视化集成展示了数据的连接方式。它允许你使用行业标准的 Cypher 查询语言查询图,并且你可以轻松地在应用程序代码中使用图功能。

RedisGraph 批量加载器#

如果你有大量想要加载到 RedisGraph 数据库的 CSV 文件,你一定要试试 这个批量加载工具。这个工具被恰当地命名为 RedisGraph 批量加载器,它用 Python 编写,可以帮助你从 CSV 输入构建 RedisGraph 数据库。这个工具需要 Python 3 解释器。

按照以下步骤将 CSV 数据加载到 RedisGraph 数据库中

步骤 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. 克隆批量加载工具#

 $ git clone https://github.com/RedisGraph/redisgraph-bulk-loader

步骤 4. 安装 RedisGraph 批量加载工具#

批量加载器可以使用 pip 安装

  pip3 install redisgraph-bulk-loader

或者

 pip3 install git+https://github.com/RedisGraph/redisgraph-bulk-loader.git@master

步骤 5. 为此工作创建一个 Python 虚拟环境#

 python3 -m venv redisgraphloader

步骤 6. 进入虚拟环境:#

 source redisgraphloader/bin/activate

步骤 7. 安装批量加载器的依赖项:#

 pip3 install -r requirements.txt

如果上面的命令不起作用,请安装以下模块

 pip3 install pathos
 pip3 install redis
 pip3 install click

步骤 8. 安装 groovy#

 groovy generateCommerceGraphCSVForImport.groovy

步骤 9. 验证创建的 .csv 文件#

 head -n2 *.csv
 ==> addtocart.csv <==
 src_person,dst_product,timestamp
 0,1156,2010-07-20T16:11:20.551748

 ==> contain.csv <==
 src_person,dst_order
 2000,1215

 ==> order.csv <==
 _internalid,id,subTotal,tax,shipping,total
 2000,0,904.71,86.40,81.90,1073.01

 ==> person.csv <==
 _internalid,id,name,address,age,memberSince
  0,0,Cherlyn Corkery,146 Kuphal Isle South Jarvis MS 74838-0662,16,2010-03-18T16:25:20.551748

 ==> product.csv <==
 _internalid,id,name,manufacturer,msrp
 1000,0,Sleek Plastic Car,Thiel Hills and Leannon,385.62

 ==> transact.csv <==
 src_person,dst_order
 2,2000

 ==> view.csv <==
 src_person,dst_product,timestamp
 0,1152,2012-04-14T11:23:20.551748

步骤 10. 运行批量加载脚本#

  python3 bulk_insert.py prodrec-bulk -n person.csv -n product.csv -n order.csv -r view.csv -r addtocart.csv -r transact.csv -r contain.csv
 person  [####################################]  100%
 1000 nodes created with label 'person'
 product  [####################################]  100%
 1000 nodes created with label 'product'
 order  [####################################]  100%
 811 nodes created with label 'order'
 view  [####################################]  100%
 24370 relations created for type 'view'
 addtocart  [####################################]  100%
 6458 relations created for type 'addtocart'
 transact  [####################################]  100%
 811 relations created for type 'transact'
 contain  [####################################]  100%
 1047 relations created for type 'contain'
 Construction of graph 'prodrec-bulk' complete: 2811 nodes created, 32686 relations created in 1.021761 seconds
 graph.query prodrec "match (p:person) where p.id=200 return p.name"
 1) 1) "p.name"
 2) (empty array)
 3) 1) "Cached execution: 0"
    2) "Query internal execution time: 0.518300 milliseconds"

步骤 11 . 安装 RedisInsight#

要在本地 Mac 上使用 RedisInsight,你可以从 RedisLabs 网站上的 RedisInsight 页面下载

点击此链接访问一个表单 ,你可以在其中选择你想要的操作系统。

如果你的系统中安装了 Docker Engine,最快的方法是运行以下命令

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

步骤 12. 访问 RedisInsight#

接下来,在浏览器中访问 http://localhost:8001。

步骤 13. 运行图查询#

 GRAPH.QUERY "prodrec-bulk" "match (p:person) where p.id=199 return p"

参考资料#

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