Neo4j(三)数据导入及查询

2020-12-03  本文已影响0人  RacyFu

大多数图数据库都可以导入csv格式的数据,Node数据和Relation数据分开。

现有演员(person.csv),电影(movie.csv),演员和电影关系person_to_movie.csv文件(忽略数据真实性QAQ)csv文件中的值若有‘或者"则需要用三个单引号将整个值括起来:

Person.csv

"pid","birth","death","name"

"643","1965-12-31",,"巩俐"

"695","1937-03-16","1999-04-14","乔宏"

"1336","1963-04-26",,"李连杰"

"1337","1962-06-27",,"梁朝伟"

########################################################

Movie.csv

"mid","title"

"13","Forrest Gump"

"12780","少年黄飞鸿之铁马骝"

"14310","无间道III: 终极无间"

"15384","赤壁"

#########################################################

person_to_movie.csv

"pid","mid"

643,12780

695,13

1336,14310

1337,15384

695,15384

导入数据(需要早命令行中使用):

LOAD CSV WITH HEADERS FROM 'file:///person.csv' AS line MERGE (p:Person { pid:toInteger(line.pid),birth:line.birth,death:line.death,name:line.name})

LOAD CSV WITH HEADERS FROM "file:///movie.csv" AS line

MERGE (p:Movie{mid:toInteger(line.mid),title:line.title})

LOAD CSV WITH HEADERS FROM "file:///person_to_movie.csv" AS line

match (from:Person{pid:toInteger(line.pid)}),(to:Movie{mid:toInteger(line.mid)}) 

merge (from)-[r:actedin{pid:toInteger(line.pid),mid:toInteger(line.mid)}]->(to)

查询(可以在客户端中查询):

查询全部图(节点太多的话不能显示全部节点):match (n) return n

查询出演同一部电影的演员:

match (p1:Person)-[:actedin]->(m:Movie{mid:"15384"})<-[:actedin]-(p2:Person) return m.title

单独创建节点:

create(p:Person {birth:"2000-09-12",death:"",name:"王俊凯"})

创建关系:

create (a:Person{name:"王俊凯"})-[:二级2产品]->(b:Movie{mid:"14310"})

删除全部节点和关系

match(n) detach delete n    

创建唯一约束:

create constraint on (p:Person) assert(p.name) is unique

建索引:

create index on :Person(name)

删除索引:

DROP INDEX ON :Person(name)

删除唯一约束:

drop constraint on (p:Person) assert(p.name) is unique

展示索引:

CALL db.indexes

复合索引在查询的时候要带上所有索引列查询,否则不起作用,而且只对n.property = x 或者 n.property in list这种语法有效,其他不有效

上一篇下一篇

猜你喜欢

热点阅读