怎样快速搭建一个知识图谱的小实例呢?这篇博客会详细的介绍从Neo4j环境的搭建,以及csv文件的导入,最后到Cypher命令生成节点和关系。
项目下有以下几个文件:
文件 | 说明 |
---|---|
neo4j-community-4.0.4-unix.tar.gz | neo4j安装包 |
jdk-11.0.7_osx-x64_bin.dmg | jdk安装包 |
import | 包含csv数据文件 |
Cypher.rtf | 创建节点和关系的Cypher语句 |
链接: https://pan.baidu.com/s/1rpwTZ9qBWyMHpQ8GtqVExQ 提取码: 8rj9
1.解压neo4j-community-4.0.4-unix.tar.gz文件到任意指定路径(yourpath),注:不要放在Desktop路径下,不然会Operation not permitted
2.打开Terminal 输入以下命令
cd yourpath/neo4j-community-4.0.4/bin
./neo4j start
过程中可能因为没有java环境报错,直接安装jdk-11.0.7_osx-x64_bin.dmg就可以了
3.在浏览器中输入localhost:7474,可使用即安装成功
4.首次使用,用户名、密码默认为neo4j
5.关闭数据库
./neo4j stop
1.把import文件夹里面的所有csv文件拷贝到yourpath/neo4j-community-4.0.4/import
2.使用Cypher.rtf中的 “2.neo4j载入基本实体信息” Cypher命令创建节点和关系
创建“人”实体,这个人拥有姓名、性别、年龄
LOAD CSV WITH HEADERS FROM "file:///executive.csv" AS row
CREATE (:Person {personID: row.person_id, Name: row.name, Gender: row.gender, Age: row.age})
创建“公司”实体,除了股票代码,还有股票名称
LOAD CSV WITH HEADERS FROM "file:///stock.csv" AS row
CREATE (:Company {stockID: row.stock_id, Name: row.name, Code: row.code})
创建“概念”实体,每个概念都有概念名
LOAD CSV WITH HEADERS FROM "file:///concept.csv" AS row
CREATE (:Concept {conceptID: row.concept_id, Name: row.name})
创建“行业”实体,每个行业都有⾏业名
LOAD CSV WITH HEADERS FROM "file:///industry.csv" AS row
CREATE (:Industry {industryID: row.industry_id, Name: row.name})
创建“人”和“公司”的关系,这个关系有董事长、执行董事等等
LOAD CSV WITH HEADERS FROM "file:///executive_stock.csv" AS line
match (from:Person{personID:line.START_ID}),(to:Company{stockID:line.END_ID})
merge (from)-[r:employ_of{jobs:line.jobs}]-> (to)
创建“公司”和“概念”的关系
LOAD CSV WITH HEADERS FROM "file:///stock_concept.csv" AS line
match (from:Company{stockID:line.START_ID}),(to:Concept{conceptID:line.END_ID})
merge (from)-[r:concept_of{}]-> (to)
创建“公司”和“行业”的关系
LOAD CSV WITH HEADERS FROM "file:///stock_industry.csv" AS line
match (from:Company{stockID:line.START_ID}),(to:Industry{industryID:line.END_ID})
merge (from)-[r:industry_of{}]-> (to)