NEO4J 圖數據庫使用APOC數據導入


                                                                 Neo4j 數據導入

一、安裝與部署

               直接在官網下載安裝包安裝,解壓即可。

二、下載相應的jar包

apoc 包下載鏈接: https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases

1.sqlserver 數據導入neo4j的jar包

apoc-3.4.0.1-all.jar     mssql-jdbc-6.2.2.jre8.jar     sqljdbc4-4.0.jar
  
  
 
 
         

 

2.mysql 數據導入neo4j的jar包

 

apoc-3.3.0.1-all.jar    mysql-connector-java-8.0.8-dmr.jar
  
  
 
 
         

 

3.將對應jar包放在安裝目錄plugins文件目錄里,然后conf目錄里的neo4j.conf的后面加上

 


   
   
  
  
          
  1. dbms.security.procedures.unrestricted=apoc.*
  2. apoc. import.file.enabled= true
apoc.export.file.enabled=true
  
  
 
 
         

 

 

4.restart neo4j,運行return apoc.version(),若有版本號,則成功。

 

三、導數據


   
   
  
  
          
  1. import org.neo4j.driver.v1.*;
  2. public class Connect{
  3. public static void main(String[] args){
  4. Driver driver = GraphDatabase.driver( "bolt://localhost:7687",AuthTokens.basic( "neo4j", "neo4j"));
  5. Session session = driver.session();
  6. String cypher= "create constraint on (n:ITEM) ASSERT n.itemid is unique"; //創建唯一索引,這樣可以更快的導入數據
  7. Session.run(cypher);
  8. cypher= "CALL apoc.periodic.iterate(\"CALL apoc.load.jdbc('jdbc:sqlserver://localhost;username=name;password=word;database=db;characterEncoding=utf-8',\\\"SELECT * FROM TABLE1\\\")\",\"MERGE(n:ITEM{itemid:row.mitemid}) with * MERGE(m:ITEM{itemid:row.itemid}) with * create p=(n)-[r:rel{rels:row.rels}]->(m)\",{batchSize:10000,iterateList:true})"; //連接sqlserve數據庫和設計創建neo4j圖數據庫數據模型
  9. Session.run(cypher);
  10. session.close();
  11. driver.close();
  12. }
  13. }

mysql數據庫類似,不再贅述。

補充:1.使用neo4j-import導入數據的命令

neo4j-admin import --nodes:item  "nodes.csv"  --relationships:rel "rel_header.csv,rel.csv" --ignore-missing-nodes

2.apoc 導出命令


    
    
   
   
           
  1. call apoc.export.cypher.query(
  2. "MATCH (p1:Person)-[r:KNOWS]->(p2:Person) RETURN p1,r,p2",
  3. "/tmp/friendships.cypher",
  4. { format: 'plain',cypherFormat: 'updateStructure'}) `

參考: http://neo4j-contrib.github.io/neo4j-apoc-procedures/#_export_import

call apoc.export.cypher.query("match (n:lable) where not (n)--() and n.properties = '400' return distinct(n)","C://User/Desktop/test",{format:'plain',cypherFormat:'create'})
   
   
  
  
          

 3.不用解壓也能導數據load csv

load csv from "file:/twitter-2010.txt.gz" as line fieldterminator ' ' with toInt(line[0]) as id,toInt(line[1]) as id1 return id,id1 limit 10
   
   
  
  
          

    
    
   
   
           
  1. using periodic commit 1000
  2. load csv from "file:/twitter-2010.txt.gz" as line fieldterminator ' ' create (item:ITEM{ id:line[ 0],item:line[ 1]})

 

原文地址:https://blog.csdn.net/FFFSSSFFF6/article/details/80711202


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM