Skip to content

Instantly share code, notes, and snippets.

@aaronzirbes
Last active November 8, 2016 19:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronzirbes/60eff15aeab08abb475c0ab153fa015b to your computer and use it in GitHub Desktop.
Save aaronzirbes/60eff15aeab08abb475c0ab153fa015b to your computer and use it in GitHub Desktop.
Want to view your filesystem as a Gremlin graph?
/**
* 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