Skip to main content

How to query Graph data in Redis using JavaScript

End-of-Life Notice

Redis is phasing out RedisGraph. This blog post explains the motivation behind this decision and the implications for existing Redis customers and community members.

End of support is scheduled for January 31, 2025.

Beginning with Redis Stack 7.2.x-y, Redis Stack will no longer include graph capabilities (RedisGraph).


Profile picture for Ajeet Raina
Author:
Ajeet Raina, Former Developer Growth Manager at Redis

RedisGraph is the fastest graph database that processes complex graph operations in real time, 10x – 600x faster than any other graph database. Show how your data is connected through multiple visualization integrations including RedisInsight, Linkurious, and Graphileon. Query graphs using the industry-standard Cypher query language and easily use graph capabilities from application code.

RedisGraph JavaScript Client

Follow the steps below to get started with RedisGraph with Java:

Step 1. Run Redis Stack Docker container

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

Step 2. Verify if RedisGraph module is loaded

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

Step 3. Clone the repository

 git clone https://github.com/RedisGraph/redisgraph.js

Step 4. Install the packages locally

 npm install redisgraph.js

Step 5. Write a JavaScript code

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

Save the above file as "app.js".

Step 6. Execute the Script

 node app.js
 roi
0.1789
amit
2

Step 7. Monitor the Graph query

 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"

Step 8. Install RedisInsight

Run the RedisInsight container. The easiest way is to run the following command:

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

Step 9. Accessing RedisInsight

Next, point your browser to http://localhost:8001.

Step 10. Run the Graph Query

You can display the number of records returned by a query:

My Image

References

  • Learn more about RedisGraph in the Quickstart tutorial.

Redis Launchpad