Last active
November 8, 2016 19:03
-
-
Save aaronzirbes/60eff15aeab08abb475c0ab153fa015b to your computer and use it in GitHub Desktop.
Want to view your filesystem as a Gremlin graph?
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
/** | |
* Run this on your DSE graph instance via DSE studio to see some pretty graphs. | |
* Feel free to change folderDepth or rootPath. | |
* | |
* Note, this only works in DSE if development mode is on, and vertex scanning is enabled. | |
*/ | |
// schema.config().option("graph.allow_scan").set(true) | |
// schema.config().option("graph.schema_mode").set("Development") | |
import org.apache.tinkerpop.gremlin.structure.Edge | |
import org.apache.tinkerpop.gremlin.structure.Vertex | |
final int folderDepth = 3 | |
final String rootPath = '/' | |
Map getFile(File file, int remaining, int depth = 0) { | |
Map tree = [ depth: depth, file: file, children: [] ] | |
if (remaining > 0) { | |
tree.children = tree.file.listFiles().collect { getFile(it, remaining - 1, depth + 1) } | |
} | |
return tree | |
} | |
Vertex treeToGraph(Map fileTree) { | |
Vertex v = graph.addVertex(T.label, 'file', | |
'id', UUID.randomUUID(), | |
'name', fileTree.file.name) | |
fileTree.children.each { v.addEdge('child', treeToGraph(it)) } | |
return v | |
} | |
Vertex root = treeToGraph(getFile(new java.io.File(rootPath), folderDepth, 0)) | |
g.V() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment