Created
June 8, 2013 20:10
-
-
Save sarmbruster/5736410 to your computer and use it in GitHub Desktop.
simple groovy script to execute a cypher query (args[1]) against a local datastore (args[0]) and format output as csv
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
@Grab(group="org.neo4j", module="neo4j-cypher", version="1.9") | |
@Grab(group='net.sf.opencsv', module='opencsv', version='2.3') | |
import org.neo4j.kernel.EmbeddedGraphDatabase | |
import org.neo4j.cypher.javacompat.ExecutionEngine | |
import au.com.bytecode.opencsv.CSVWriter | |
assert args, "specify location of graph.db" | |
def db = new org.neo4j.kernel.EmbeddedGraphDatabase(args[0]) | |
def ee = new ExecutionEngine(db) | |
def result = ee.execute(args[1]) | |
def columns = result.columns() | |
System.out.withWriter { writer -> | |
CSVWriter csv = new CSVWriter(writer) | |
csv.writeNext(columns as String[]) | |
for (def row in result) { | |
def values = columns.collect {row[it]} | |
csv.writeNext(values as String[]) | |
} | |
} | |
db.shutdown() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment