学习

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

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

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

支持结束日期定为 2025 年 1 月 31 日。

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

RedisGraph JavaScript 客户端#

按照以下步骤开始使用 Java 的 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.js

步骤 4. 在本地安装软件包#

 npm install redisgraph.js

步骤 5. 编写 JavaScript 代码#

const RedisGraph = require('redisgraph.js').Graph;

let graph = new RedisGraph('social');

(async () => {
  await graph.query("CREATE (:person{name:'roi',age:32})");
  await graph.query("CREATE (:person{name:'amit',age:30})");
  await graph.query(
    "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)",
  );

  // Match query.
  let res = await graph.query(
    'MATCH (a:person)-[:knows]->(:person) RETURN a.name',
  );
  while (res.hasNext()) {
    let record = res.next();
    console.log(record.get('a.name'));
  }
  console.log(res.getStatistics().queryExecutionTime());

  // Match with parameters.
  let param = { age: 30 };
  res = await graph.query('MATCH (a {age: $age}) return a.name', param);
  while (res.hasNext()) {
    let record = res.next();
    console.log(record.get('a.name'));
  }

  // Named paths matching.
  res = await graph.query('MATCH p = (a:person)-[:knows]->(:person) RETURN p');
  while (res.hasNext()) {
    let record = res.next();
    // See path.js for more path API.
    console.log(record.get('p').nodeCount);
  }
  graph.deleteGraph();
  graph.close();
})();

将以上文件保存为“app.js”。

步骤 6. 执行脚本#

 node app.js
 roi
 0.1789
 amit
 2

步骤 7. 监控图查询#

 1632898652.415702 [0 172.17.0.1:64144] "info"
 1632898652.418225 [0 172.17.0.1:64144] "graph.query" "social" "CREATE (:person{name:'roi',age:32})" "--compact"
 1632898652.420399 [0 172.17.0.1:64144] "graph.query" "social" "CREATE (:person{name:'amit',age:30})" "--compact"
 1632898652.421857 [0 172.17.0.1:64144] "graph.query" "social" "MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)" "--compact"
 1632898652.424911 [0 172.17.0.1:64144] "graph.query" "social" "MATCH (a:person)-[:knows]->(:person) RETURN a.name" "--compact"
 1632898652.429658 [0 172.17.0.1:64144] "graph.query" "social" "CYPHER age=30  MATCH (a {age: $age}) return a.name" "--compact"
 1632898652.431221 [0 172.17.0.1:64144] "graph.query" "social" "MATCH p = (a:person)-[:knows]->(:person) RETURN p" "--compact"
 1632898652.433146 [0 172.17.0.1:64144] "graph.query" "social" "CALL db.labels()" "--compact"
 1632898652.434781 [0 172.17.0.1:64144] "graph.query" "social" "CALL db.propertyKeys()" "--compact"
 1632898652.436574 [0 172.17.0.1:64144] "graph.query" "social" "CALL db.relationshipTypes()" "--compact"
 1632898652.438559 [0 172.17.0.1:64144] "graph.delete" "social"

步骤 8. 安装 RedisInsight#

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

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

步骤 9. 访问 RedisInsight#

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

步骤 10. 运行图查询#

您可以显示查询返回的记录数量

参考资料#

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