Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.