neo4j整理
語(yǔ)法
數(shù)據(jù)庫(kù)
列出所有數(shù)據(jù)庫(kù)
:dbs
選擇數(shù)據(jù)庫(kù)
:use neo4j
一個(gè)實(shí)例就只能有一個(gè)數(shù)據(jù)庫(kù)。
如果節(jié)點(diǎn)分應(yīng)用,則通過label區(qū)分
?
學(xué)習(xí)
:play start?
查詢
match(n:bot) return n limit 20
查詢屬性分組并過濾
with用于分組后再處理。
Match( n:bot)?
with n.name as name ,count(1) as c
?where c > 1?
?return ?name ,c ;
返回:
╒══════╤═══╕ │"name"│"c"│ ╞══════╪═══╡ │"盆地" │2 │ ├──────┼───┤ │"圈閉" │2 │ └──────┴───┘?
通過id刪除實(shí)體及其關(guān)系
MATCH (r:bot)
WHERE id(r) = 377
detach delete r
?
獲取屬性
return properties(n) AS properties,ID(n) as id, labels(n) AS label
返回值:
╒═════════════════════════════════════════════════════════╤════╤═══════╕ │"properties" │"id"│"label"│ ╞═════════════════════════════════════════════════════════╪════╪═══════╡ │{"name":"圈閉","id":"6184e0fa-d1d6-4618-8c9a-ff27f5f2af10"}│361 │["bot"]│ ├─────────────────────────────────────────────────────────┼────┼───────┤ │{"name":"圈閉","id":"syxz-d1d6-4618-8c9a-ff27f5f2af10"} │376 │["bot"]│ └─────────────────────────────────────────────────────────┴────┴───────┘?
Match( n:bot{name:"圈閉"}) return n.name;
返回
╒════════╕ │"n.name"│ ╞════════╡ │"圈閉" │ ├────────┤ │"圈閉" │ └────────┘修改
修改標(biāo)簽
create ( n:AAA {name:"testlabel"}) return n;
Match( n:AAA ) set n:aaa remove n:AAA return n;
?
數(shù)據(jù)加載
加載方式
| ? | create語(yǔ)句 | load csv語(yǔ)句 | Batch Inserter | Batch Import | neo4j-import |
| 適用場(chǎng)景 | 1~1w nodes | 1w~10w nodes | 千萬以上 nodes | 千萬以上 nodes | 千萬以上 nodes |
| 速度 | 很慢(1000 nodes/s) | 一般(5000 nodes/s) | 非常快(數(shù)萬nodes/s) | 非常快(數(shù)萬nodes/s) | 非常快(數(shù)萬nodes/s) |
| 優(yōu)點(diǎn) | 使用方便,可實(shí)時(shí)插入。 | 使用方便,可以加載本地 | 遠(yuǎn)程CSV;可實(shí)時(shí)插入 | 基于Batch Inserter,可以直接運(yùn)行編譯好的jar包;可以在已存在的數(shù)據(jù)庫(kù)中導(dǎo)入數(shù)據(jù) | 官方出品,比Batch Import占用更少的資源 |
| 缺點(diǎn) | 速度慢 | 需要將數(shù)據(jù)轉(zhuǎn)換成csv | 需要轉(zhuǎn)成CSV;只能在JAVA中使用;且插入時(shí)必須停止neo4j | 需要轉(zhuǎn)成CSV;必須停止neo4j | 需要轉(zhuǎn)成CSV;必須停止neo4j;只能生成新的數(shù)據(jù)庫(kù),而不能在已存在的數(shù)據(jù)庫(kù)中插入數(shù)據(jù) |
load csv
使用的是windows下的neo4j browser,在導(dǎo)入數(shù)據(jù)之前,需要將EXCEL另存為CSV,如果有多個(gè)sheet,則需要分開單獨(dú)存儲(chǔ)
USING PERIODIC COMMIT 300 LOAD CSV WITH HEADERS FROM “file:///test.csv” AS line MERGE (a:actors{name:line.name,type:line.type,id:line.id})
本地?cái)?shù)據(jù)文件放在\Neo4j\graph.db\import文件夾內(nèi),遠(yuǎn)程數(shù)據(jù)文件可以使用文件URL
在import文件夾里放了一個(gè)actors.csv 文件,然后指定file:///actors.csv 即可訪問該文件
可變參數(shù)解釋:
1、USING PERIODIC COMMIT 300
使用自動(dòng)提交,每滿300條提交一次,防止內(nèi)存溢出
2、WITH HEADERS
從文件中讀取第一行作為參數(shù)名,只有在使用了該參數(shù)后,才可以使用line.name這樣的表示方式,否則需使用line[0]的表示方式
3、AS line
為每行數(shù)據(jù)重命名
4、MERGE
用merge比用create好一點(diǎn),可以防止數(shù)據(jù)重復(fù) 上面的語(yǔ)句可修改為如下
USING PERIODIC COMMIT 10
LOAD CSV FROM "file:///actors.csv" AS line
create (a:actors{personId:line[0],name:line[1],type:line[2]})
USING PERIODIC COMMIT 10
LOAD CSV FROM “file:///roles.csv” AS line
MATCH (from:movies{movieId:line[2]}),(to:actors{personId:line[0]})
merge (from)-[r:ACTED_IN{miles:line[1]}]-> (to) return r
?
actors.csv 文件位于neo4j 的import 文件目錄下。
?
shell加載文件
cypher-shell可以執(zhí)行腳本文件.
交互式命令
? :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?? Print all currently set query parameters and their values
? :rollback Rollback the currently open transaction
? :source?? Interactively executes cypher statements from a file
? :use????? Set the active database
?
執(zhí)行文件:
:source /data/temp/pt.r.txt
或者
cypher-shell load csv ...
?
?
sh命令
?-host????? Domain name or IP of host to connect to (default: localhost)
?-port????? Port of host to connect to (default: 1337)
?-name????? RMI name, i.e. rmi://<host>:<port>/<name> (default: shell)
?-pid?????? Process ID to connect to
?-c???????? Command line to execute. After executing it the shell exits
?-file????? File containing commands to execute, or '-' to read from stdin. After executing it the shell exits
?-readonly? Connect in readonly mode (only for connecting with -path)
?-path????? Points to a neo4j db path so that a local server can be started there
?-config??? Points to a config file when starting a local server
?Example arguments for remote:
??????? -port 1337
??????? -host 192.168.1.234 -port 1337 -name shell
??????? -host localhost -readonly
??????? ...or no arguments for default values
Example arguments for local:
??????? -path /path/to/db
??????? -path /path/to/db -config /path/to/neo4j.config
??????? -path /path/to/db -readonly
示例:
./bin/neo4j-shell -c < /data/stale/data01/neo4j/create_index.cypher
./bin/neo4j-shell -path /data/stale/data01/neo4j/neo4j-community-3.1.0/data/dbms/ -conf /data/stale/data01/neo4j/neo4j-community-3.1.0/conf/neoo4j.conf -file /data/stale/data01/neo4j/create_index.cypther
?
?
?
總結(jié)
- 上一篇: mongodb语法与spring实现
- 下一篇: Ribbon源码解析(一)