neo4j整理

语法

数据库

列出所有数据库

: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 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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章