語法
數據庫
列出所有數據庫
:dbs
選擇數據庫
:use neo4j
一個實例就只能有一個數據庫。
如果節點分應用,則通過label區分
學習
: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刪除實體及其關係
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"│ ╞════════╡ │"圈閉" │ ├────────┤ │"圈閉" │ └────────┘
修改
修改標籤
create ( n:AAA {name:"testlabel"}) return n;
Match( n:AAA ) set n:aaa remove n:AAA return n;
數據加載
加載方式
create語句 | load csv語句 | Batch Inserter | Batch Import | neo4j-import | |
適用場景 | 1~1w nodes | 1w~10w nodes | 千萬以上 nodes | 千萬以上 nodes | 千萬以上 nodes |
速度 | 很慢(1000 nodes/s) | 一般(5000 nodes/s) | 非常快(數萬nodes/s) | 非常快(數萬nodes/s) | 非常快(數萬nodes/s) |
優點 | 使用方便,可實時插入。 | 使用方便,可以加載本地 | 遠程CSV;可實時插入 | 基於Batch Inserter,可以直接運行編譯好的jar包;可以在已存在的數據庫中導入數據 | 官方出品,比Batch Import佔用更少的資源 |
缺點 | 速度慢 | 需要將數據轉換成csv | 需要轉成CSV;只能在JAVA中使用;且插入時必須停止neo4j | 需要轉成CSV;必須停止neo4j | 需要轉成CSV;必須停止neo4j;只能生成新的數據庫,而不能在已存在的數據庫中插入數據 |
load csv
使用的是windows下的neo4j browser,在導入數據之前,需要將EXCEL另存爲CSV,如果有多個sheet,則需要分開單獨存儲
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})
本地數據文件放在\Neo4j\graph.db\import文件夾內,遠程數據文件可以使用文件URL
在import文件夾裏放了一個actors.csv 文件,然後指定file:///actors.csv 即可訪問該文件
可變參數解釋:
1、USING PERIODIC COMMIT 300
使用自動提交,每滿300條提交一次,防止內存溢出
2、WITH HEADERS
從文件中讀取第一行作爲參數名,只有在使用了該參數後,纔可以使用line.name這樣的表示方式,否則需使用line[0]的表示方式
3、AS line
爲每行數據重命名
4、MERGE
用merge比用create好一點,可以防止數據重複 上面的語句可修改爲如下
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可以執行腳本文件.
交互式命令
: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
執行文件:
: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 serverExample 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