Created
September 26, 2024 12:00
-
-
Save Podbrushkin/e256302a26abe595a327e53a5fb927f1 to your computer and use it in GitHub Desktop.
Neo4j Export to Graphviz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myapp; | |
import java.util.HashSet; | |
import java.util.Set; | |
import org.neo4j.graphdb.Node; | |
import org.neo4j.graphdb.Relationship; | |
import org.neo4j.procedure.Description; | |
import org.neo4j.procedure.Name; | |
import org.neo4j.procedure.UserAggregationFunction; | |
import org.neo4j.procedure.UserAggregationResult; | |
import org.neo4j.procedure.UserAggregationUpdate; | |
public class GraphvizFunction { | |
@UserAggregationFunction | |
@Description("myapp.exportGraphviz(nodes, rels) - draws specified nodes and relationships with Graphviz") | |
public NodesRelsAggregator exportGraphviz() { | |
return new NodesRelsAggregator(); | |
} | |
public static class NodesRelsAggregator { | |
private Set<Node> nodes = new HashSet<>(); | |
private Set<Relationship> relationships = new HashSet<>(); | |
@UserAggregationUpdate | |
public void exportGraphviz(@Name("node") Node node, @Name("relationship") Relationship rel) { | |
nodes.add(node); | |
relationships.add(rel); | |
} | |
@UserAggregationResult | |
public String result() { | |
return MyGraphvizWriter.getGraphvizForNodesAndRels(nodes, relationships); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package myapp; | |
import java.io.ByteArrayOutputStream; | |
import java.nio.charset.Charset; | |
import java.util.Set; | |
import org.neo4j.graphdb.Node; | |
import org.neo4j.graphdb.Relationship; | |
import org.neo4j.visualization.graphviz.GraphvizWriter; | |
import org.neo4j.walk.Visitor; | |
import org.neo4j.walk.Walker; | |
public class MyGraphvizWriter { | |
public static String getGraphvizForNodesAndRels(Set<Node> nodes, Set<Relationship> relationships) { | |
try { | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
GraphvizWriter writer = new GraphvizWriter(); | |
writer.emit(out, getWalkerForNodesAndRels(nodes, relationships)); | |
return out.toString(Charset.defaultCharset()); | |
} catch (Exception e) {e.printStackTrace();} | |
return null; | |
} | |
private static Walker getWalkerForNodesAndRels(Set<Node> nodes, Set<Relationship> relationships) { | |
return new Walker() { | |
@Override | |
public <R, E extends Throwable> R accept(Visitor<R,E> visitor) throws E { | |
for (Node node : nodes) { | |
visitor.visitNode(node); | |
} | |
for (Relationship relationship : relationships) { | |
visitor.visitRelationship(relationship); | |
} | |
return visitor.done(); | |
} | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>mygroup</groupId> | |
<artifactId>Neo4jGraphvizPlugin</artifactId> | |
<version>0.0.1-SNAPSHOT</version> | |
<properties> | |
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | |
</properties> | |
<dependencies> | |
<dependency> | |
<groupId>org.neo4j</groupId> | |
<artifactId>neo4j</artifactId> | |
<version>5.22.0</version> | |
<scope>provided</scope> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>17</source> | |
<target>17</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<artifactId>maven-shade-plugin</artifactId> | |
<executions> | |
<execution> | |
<phase>package</phase> | |
<goals> | |
<goal>shade</goal> | |
</goals> | |
</execution> | |
</executions> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment