How to Import Large Data into neo4j Database

How to Import Large Data into Neo4j Database

Preface

We only introduce how to import large data into graph database neo4j run on a docker container environment operated on Linux. Here, we use the official method provided by neo4j database:

neo4j-admin import

This method:

  • Very fast. For data 10^6 \sim 10^7 nodes and relations, it usually takes only several seconds to import.
  • Need the datafile to be some specific form.
  • Can only import the data to unused database. Once the database is activated, data can not be imported by this method.

Step 1. Setup the Neo4j database

We first pull the image of neo4j database.

docker pull neo4j:enterprise

Here, it is necessary to use the enterprise version of neo4j database. This is because we need to create different database in one container, and this function is not included in community version.
Now, build the database container with name, say neo4j.

sudo docker run   -idt \
--env=NEO4J_ACCEPT_LICENSE_AGREEMENT=yes \
--name neo4j \
-p 7474:7474   -p 7687:7687  \
-v $HOME/workspace/neo4j/data:/var/lib/neo4j/data \
-v $HOME/workspace/neo4j/logs:/var/lib/neo4j/logs \
-v $HOME/workspace/neo4j/import:/var/lib/neo4j/import \
-v $HOME/workspace/neo4j/plugins:/var/lib/neo4j/plugins \
-v $HOME/workspace/neo4j/conf:/var/lib/neo4j/conf \
neo4j:enterprise

Step 2. Make Your Datafile of the Required Form

Generally, we need two kinds of .csv files: the nodes and the relationships.
The nodes.csv should be like this:

personId:ID,name,:LABEL
keanu,"Keanu Reeves",Actor
laurence,"Laurence Fishburne",Actor
carrieanne,"Carrie-Anne Moss",Actor

and the relationships.csv should be like this:

:START_ID,role,:END_ID,:TYPE
keanu,"Neo",tt0133093,ACTED_IN
keanu,"Neo",tt0234215,ACTED_IN
keanu,"Neo",tt0242653,ACTED_IN
laurence,"Morpheus",tt0133093,ACTED_IN
laurence,"Morpheus",tt0234215,ACTED_IN
laurence,"Morpheus",tt0242653,ACTED_IN
carrieanne,"Trinity",tt0133093,ACTED_IN
carrieanne,"Trinity",tt0234215,ACTED_IN
carrieanne,"Trinity",tt0242653,ACTED_IN

The ID should be the unique tag for each node in this graph.
For more details, please refer to the official guide.
Please note: a variable like personId:ID means that it is bounded with some specific property. Although you name the variable personId, you will not see this tag personId for your nodes in your neo4j database being imported.
If you want to add such tag, you can name two variable with same value, for instance:

personId:ID, persionId

and the a tag personId will be add for these nodes.

Step 3. Import the Data

Now, we have finished the preliminary work, and can begin to import the data.
First we need to move the datafile into the import directory of the neo4j database. Since we have set the directory projection from our host machine to our container by

-v $HOME/workspace/neo4j/import:/var/lib/neo4j/import

, we only need to move our datafile to the directory

/workspace/neo4j/import

by using the command cp or mv command on linux command line.

After this, we can import the data to a new database, say database1 by

docker exec --interactive --tty neo4j neo4j-admin import --database=database1 --nodes=import/nodes.csv --relationships=import/relations.csv

if we need to ignore the bad nodes or bad relationships, we can add the parameters

--skip-bad-relationships=true

Step4. Create the Database

First, get in the neo4j browser, by imputing the ip address(say localhost) and the port number(say 7474).

http://localhost:7474

Normally, you will be asked to input the username and password. The default username is neo4j and the default password is neo4j. After imputing, you will be asked to set a new password.
After logging in, we can see neo4j(default) as the default database. Now, switch the current cypher command line to system, and then create new database with the same name as you imported data to

system$ :CREATE DATABASE database1

and then you may see the database that you just created. However, this when you use this database, it will tell you this database is still not available. What you need to do is to restart the whole neo4j database.
A simple way to do this is to restart the neo4j container by the command

docker stop neo4j

and

docker start neo4j

Now, when you log in your neo4j by neo4j browser, you will see your data imported to the database1! Congratulations!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,084评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,623评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,450评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,322评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,370评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,274评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,126评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,980评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,414评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,599评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,773评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,470评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,080评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,713评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,852评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,865评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,689评论 2 354

推荐阅读更多精彩内容