FalkorDB Java client
<dependencies>
<dependency>
<groupId>com.falkordb</groupId>
<artifactId>jfalkordb</artifactId>
<version>0.4.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>snapshots-repo</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</repository>
</repositories>
and
<dependencies>
<dependency>
<groupId>com.falkordb</groupId>
<artifactId>jfalkordb</artifactId>
<version>0.4.1-SNAPSHOT</version>
</dependency>
</dependencies>
package com.falkordb;
import com.falkordb.graph_entities.Edge;
import com.falkordb.graph_entities.Node;
import com.falkordb.graph_entities.Path;
import com.falkordb.Graph;
import com.falkordb.GraphContext;
import com.falkordb.GraphContextGenerator;
import com.falkordb.GraphTransaction;
import com.falkordb.Record;
import com.falkordb.ResultSet;
import com.falkordb.FalkorDB;
import com.falkordb.Driver;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GraphExample {
public static void main(String[] args) {
// general context api. Not bound to graph key or connection
Driver driver = FalkorDB.driver();
Graph graph = driver.graph("social");
Map<String, Object> params = new HashMap<>();
params.put("age", 30);
params.put("name", "amit");
// send queries to a specific graph called "social"
graph.query("CREATE (:person{name:'roi',age:32})");
graph.query("CREATE (:person{name:$name,age:$age})", params);
graph.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");
ResultSet resultSet = graph.query("MATCH (a:person)-[r:knows]->(b:person) RETURN a, r, b");
// iterate over result set
for(Record record: resultSet) {
// get values
Node n = record.getValue("a");
Edge e = record.getValue("r");
//print record
System.out.println("Node: " + n + ", Edge: " + e);
}
resultSet = graph.query("MATCH p = (:person)-[:knows]->(:person) RETURN p");
for(Record record: resultSet) {
Path p = record.getValue("p");
// More path API at Javadoc.
System.out.println(p.nodeCount());
}
// delete graph
graph.deleteGraph();
GraphContextGenerator contextGraph = driver.graph("contextSocial");
// get connection context - closable object
try(GraphContext context = contextGraph.getContext()) {
context.query("CREATE (:person{name:'roi',age:32})");
context.query("MATCH (a:person), (b:person) WHERE (a.name = 'roi' AND b.name='amit') CREATE (a)-[:knows]->(b)");
// WATCH/MULTI/EXEC
context.watch("contextSocial");
GraphTransaction t = context.multi();
t.query("MATCH (a:person)-[r:knows]->(b:person{name:$name,age:$age}) RETURN a, r, b", params);
// support for Redis/Jedis native commands in transaction
t.set("x", "1");
t.get("x");
// get multi/exec results
List<Object> execResults = t.exec();
System.out.println(execResults.toString());
context.deleteGraph();
}
}
}
JFalkorDB is licensed under the BSD-3-Clause license .