zoukankan      html  css  js  c++  java
  • neo4j 数据库导入导出

    工作中需要将 A 图数据库的数据完全导出,并插入到 B 图数据库中。查找资料,好多都是通过导入,导出 CSV 文件来实现。然而,经过仔细研究发现,导出的节点/关系 都带有 id 属性 ,因为 A B 两个库的节点和关系是各自生成的,它们数据的 id 有重复。因此我担心通过CSV 的方式插入数据库 会造成数据的覆盖或者某些数据导入不成功之类的问题,所以,就一直想找一种方式,通过 命令的方式来导入导出数据。也就是说:把 数据库 A 中的节点/关系的创建命令导出来,在 B 中运行,从而会自动生成新的节点和关系,这样就不会有 id 冲突的问题。经过查找资料,步骤如下:

    Neo4j 图数据库通过命令导入导出数据库

    首先 阅读一下该文档

    https://github.com/jexp/neo4j-shell-tools#cypher-import

    一 安装 neo4j-shell-tools
    cd /path/to/neo4j-community-3.0.1
    curl http://dist.neo4j.org/jexp/shell/neo4j-shell-tools_3.0.1.zip -o neo4j-shell-tools.zip
    unzip neo4j-shell-tools.zip -d lib
    

    二 重启数据库

    cd /path/to/neo4j-community-3.0.1
    sudo ./bin/neo4j restart
    

    三 关闭数据库,导出命令

    suo ./neo4j stop
    sudo ./neo4j-shell -path /opt/neo4j/data/databases/graph.db
    

    会显示如下内容:

    an@an-virtual-machine:/opt/neo4j/bin$ sudo ./neo4j-shell -path /opt/neo4j/data/databases/graph.db
    NOTE: Local Neo4j graph database service at '/opt/neo4j/data/databases/graph.db'
    Welcome to the Neo4j Shell! Enter 'help' for a list of commands. Please note that neo4j-shell is deprecated and to be replaced by cypher-shell.
    
    
    neo4j-sh (?)$ 
    

    四 导出命令

    neo4j-sh (?)$ export-cypher -r -o /home/an/sisi.cypher match(n)-[r]-(m)return n,r,m
    
    命令运行完毕,会显示如下输出,并生成相应文件 /home/an/sisi.cypher.
    经测试,(n)-[r]-(m) 虽然没有指定方向,但是由于程序会生成一些特殊的label,从而确保不会出现双方向重复匹配情况。能够确保数据的单一性。
    Wrote Nodes 0. 100%: nodes = 167276 rels = 1915138 properties = 0 time 15891 ms total 15891 ms
    Wrote Relationships 1. 100%: nodes = 167276 rels = 2082410 properties = 190061 time 3139 ms total 19030 ms
    Wrote to Cypher-file /home/an/sisi.cypher 2. 100%: nodes = 167276 rels = 2082410 properties = 190061 time 2 ms total 19032 ms
    
    

    五 修改导出的命令文件

    导出的命令文件内容是 一些 cypher 命令,和一些事务相关命令。这些命令是要在 cypher-shell中运行的,因此要适合它的要求。

    修改内容如下:
    1. begin =>  :begin
    2. commit => :commit
    3. 删掉 schema await 语句
    

    六 打开数据库,运行命令

    打开数据库,并进入 cypher-shell 界面

    sudo ./bin/neo4j start
    sudo ./cypher-shell -u neo4j -p neo4j //用户名  密码
    
    显示如下:
    an@an-virtual-machine:/opt/neo4j/bin$ sudo ./cypher-shell -u neo4j -p root
    Connected to Neo4j 3.2.0 at bolt://localhost:7687 as user neo4j.
    Type :help for a list of available commands or :exit to exit the shell.
    Note that Cypher queries must end with a semicolon.
    neo4j>
    为什么之前要修改导出的命令文件,在begin和commit前加 : ,是因为如下原因:
    neo4j> :help
    
    Available commands:
      :begin    Open a transaction
      :commit   Commit the currently open transaction
      :exit     Exit the logger
      :help     Show this help message
      :history  Print a list of the last commands executed
      :param    Set the value of a query parameter
      :params   Prints all currently set query parameters and their values
      :rollback Rollback the currently open transaction
    
    

    这里如果发现无法启动数据库,就看一看刚才打开的 neo4j-shell 终端有没有关闭,如果没有关闭,首先关闭该终端,然后在重启数据库。

    七 运行命令

    将导出文件sisi.cypher 中的 cypher 命令 贴入 cypher-shell` 终端并运行即可。

    导出的文件应该是 :begin 为起始行,:commit 为结束行。

    如果,你有更好的方式来实现类似的需求,请一定要告诉我,不胜感激。

  • 相关阅读:
    算法竞赛入门经典训练指南——UVA 11300 preading the Wealth
    hiho一下 第148周
    ajax总结及案例
    Spring事务
    Struts2拦截器介绍
    Struts2的拦截器----Dog实例
    Struts2文件的下载
    Struts2文件的上传
    Struts2类型转换
    Struts2 属性驱动、模型驱动、异常机制
  • 原文地址:https://www.cnblogs.com/jijizhazha/p/7207381.html
Copyright © 2011-2022 走看看