Created
March 20, 2016 21:49
-
-
Save jalchr/589c6d492d0676ff774e to your computer and use it in GitHub Desktop.
copy all nodes from one database to another
This file contains hidden or 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
public void dumpAndLoadTo(String targetPath) throws IOException, InterruptedException { | |
// I first tried something like this: | |
// neo4j-shell -path outDir -c "DUMP MATCH n RETURN n;" | neo4j-shell -path targetPath -file - | |
// but performance was horrible upon import. | |
// See http://stackoverflow.com/questions/28246416/neo4j-export-import-data . | |
logger.info("Loading to db " + targetPath); | |
GraphDatabaseService dbSrc = getDb(); | |
GraphDatabaseService dbDest = getDb(targetPath); | |
try (Transaction txSource = dbSrc.beginTx(); Transaction txDest = dbDest.beginTx()) { | |
Map<Long, Long> nodeIdMap = new HashMap<>(); | |
for (Node srcNode : GlobalGraphOperations.at(dbSrc).getAllNodes()) { | |
Node destNode = dbDest.createNode(); | |
nodeIdMap.put(srcNode.getId(), destNode.getId()); | |
for (Label srcLabel : srcNode.getLabels()) | |
destNode.addLabel(srcLabel); | |
for (String srcKey : srcNode.getPropertyKeys()) | |
destNode.setProperty(srcKey, srcNode.getProperty(srcKey)); | |
} | |
for (Relationship srcRel : GlobalGraphOperations.at(dbSrc).getAllRelationships()) { | |
Node startNode = dbDest.getNodeById(nodeIdMap.get(srcRel.getStartNode().getId())); | |
Node endNode = dbDest.getNodeById(nodeIdMap.get(srcRel.getEndNode().getId())); | |
Relationship destRel = startNode.createRelationshipTo(endNode, srcRel.getType()); | |
for (String srcKey : srcRel.getPropertyKeys()) | |
destRel.setProperty(srcKey, srcRel.getProperty(srcKey)); | |
} | |
txSource.success(); | |
txDest.success(); | |
} | |
dbSrc.shutdown(); | |
dbDest.shutdown(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment