本文介绍了以Cypher格式(ASCII文本)导出整个数据库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Cypher中导出整个Neo4J数据库,从而生成Cypher命令的ASCII文件,该文件可以在空的Neo4J数据库上使用以重新创建原始数据库?由于Neo4J的发展如此迅速,因此我担心使用内置备份功能(企业版).

Is there a way to export an entire Neo4J database in Cypher, resulting in an ASCII file of the Cypher commands which could be used on an empty Neo4J database to re-create the original database? Since Neo4J is undergoing such rapid development, I worry about using the built-in backup functionality (of the enterprise version).

例如,对于Oracle,您可以使用SQL * PLUS DML/DDL命令导出整个数据库,这很有用.

For example, with Oracle, you can export the whole database in SQL*PLUS DML/DDL commands, which can be useful.

推荐答案

从Neo4j 2.0开始,有一个在neo4j-shell中执行转储命令.您可以转储特定查询或整个数据库的结果.通过在启动neo4j-shell时将dump命令作为参数传递,您可以将输出重定向到文件以创建密码创建脚本",或重定向到另一个neo4j-shell会话,以在不同的数据库中重新创建图形的全部或部分.

As of Neo4j 2.0 there is a dump command in neo4j-shell that does this. You can dump the results of a specific query or the entire database. By passing the dump command as an argument when starting neo4j-shell you can redirect the output to file to make a 'cypher create script' or to another neo4j-shell session recreating all or parts of the graph in a different database.

平整并转储查询结果

neo4j-sh$ dump MATCH (n:StackOverflow:Question {questionId:18830686})<-[:ANSWERS]-(answer)<-[:WRITES]-(user) RETURN *;

将整个数据库转储到文件中

Dump entire database to a file

usr@term: bin/neo4j-sh -c dump > ./backup/$(date +"%y%m%d_%H%M%S").cypher

将转储发送到另一个Shell会话&数据库

Pipe the dump to another shell session & database

usr@term: db1/bin/neo4j-sh -path db1/data/graph.db/ -c dump | db2/bin/neo4j-shell -path db2/data/graph.db/


偷取者

双打和浮点数以科学计数法导出时存在一些问题,neo4j-shell无法在导入时再次解释(, github )和转义"quoted strings"时存在一些问题( github ).我认为这两个问题都已解决,因此,如果您遇到问题,则可能需要查看最新的版本.

There were some issues with doubles and floats being exported in scientific notation, which neo4j-shell couldn't interpret again on import (SO, github) and there were some problems with escaping "quoted strings" (github). I think these are both resolved, so if you have trouble you may want to check out a recent build.

最后,有一个我认为尚未解决的问题.最近,架构已包含在转储中,因此也导出了create indexcreate constraint语句.但是,所有导出的语句都在一个框架中,并且输出中的事务相同,而neo4j不允许您​​在同一事务中创建架构和数据.因此,如果将转储直接通过管道传输到另一个Shell会话中以重新创建图形,则很可能会遇到

And finally there is one problem that I don't think is resolved yet. Recently schema was included in the dump so create index and create constraint statements are exported as well. However, all the exported statements are framed in one and the same transaction in the output and neo4j won't let you create schema and data in the same transaction. So if you pipe the dump directly into another shell session to recreate the graph, you're likely to run into

> ;
ConstraintViolationException: Cannot perform data updates in a transaction that has performed schema updates.
neo4j-sh (?)$ commit
Failed to commit, transaction rolled back

通过重定向到文件并在最后一个模式语句之后手动添加commitbegin来解决此问题很容易.请务必将它们放在新的一行,它看起来应该像

It's easy to work around this by redirecting to a file and manually adding commit and begin after the last schema statement. Be sure to put them each on a new line, it should look something like

...
create index on :`Person`(`name`)
commit
begin
create (_0:`Person` {`name`:"Paul"})
...

或者您可以即时编辑neo4j-shell的输出并将其添加到其中,例如,如果您以编程方式转储并且不想手动编辑.在osx上,我像这样使用sed

Or you can edit the output from neo4j-shell on the fly and add it there, for instance if you dump programmatically and don't want to edit manually. On osx I have used sed like so

db1/bin/neo4j-shell -path db1/data/graph.db/ -c dump | sed -E 's/create (index|constraint) on .*/&\'$'\ncommit''\'$'\nbegin/' | db2/bin/neo4j-shell -path db2/data/graph.db/

这会在每个架构语句之后添加一个提交,这比必要的提交要多(可以将所有架构语句一起提交),但是,它可以工作.

This adds a commit after each schema statement, which is more than what is necessary (could commit all schema statements together), but hey, it works.

这篇关于以Cypher格式(ASCII文本)导出整个数据库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 08:41