导入大量数据时使用CSV 文件比较方便。下面分导入节点
和导入关系
两部分说明。
1 节点信息导入
首先导入岗位信息,这里我们用id
来标识,其中id
对于每一个岗位来说是唯一的。
id
信息我们已经事先处理好保存在了csv文件中,格式如下,其中第一行是列名。
csv
id
0
1
2
3
4
5
我们首先要把这个csv 文件复制到 Neo4j 的 import 文件夹下。(具体:在我的机器中为'D:\App\neo4j\Data\relate-data\dbmss\dbms-278f05b5-0b41-40cf-883f-a5617288cb48\import'
,里面的dbms-278f05b5-0b41-40cf-883f-a5617288cb48
对应着希望导入节点的项目。
导入的Cypher语句为:
Cypher
LOAD CSV WITH HEADERS FROM 'file:///node_id.csv' AS line FIELDTERMINATOR ','
CREATE (:Job { name: line.id})
看一下添加后的结果。 按照同样的方法添加公司、平均工资、岗位名和教育背景。
Cypher
LOAD CSV WITH HEADERS FROM 'file:///node_salary.csv' AS line FIELDTERMINATOR ','
CREATE (:Salary { name: line.salary})
LOAD CSV WITH HEADERS FROM 'file:///node_title.csv' AS line FIELDTERMINATOR ','
CREATE (:Title { name: line.title})
LOAD CSV WITH HEADERS FROM 'file:///node_company.csv' AS line FIELDTERMINATOR ','
CREATE (:Company { name: line.company})
LOAD CSV WITH HEADERS FROM 'file:///node_education.csv' AS line FIELDTERMINATOR ','
CREATE (:Education { name: line.education})
characters、duties和skills 数据是使用 entity extraction 技术对岗位描述进行提取得到的,我们首先把每一个处理后的数据保存到 json 文件中。
json
{
"skill": [
"前端",
"CSS3",
"Sass",
"Less",
"Vue",
"JavaScript",
"HTML5;"
],
"character": [
"None;"
],
"duty": [
"前端模块化,组件化开发",
"Vue",
"element",
"UI",
"Sass",
"Less",
"CSS3",
"HTML5",
"uni-app",
"flex/grid布局",
"项目经验者优先;"
]
}
然后遍历文件夹下所有 json 文件,保持数据唯一之后存至 csv 文件中。
Cypher
LOAD CSV WITH HEADERS FROM 'file:///unique_characters.csv' AS line FIELDTERMINATOR ','
CREATE (:Characters { name: line.Data})
LOAD CSV WITH HEADERS FROM 'file:///unique_duties.csv' AS line FIELDTERMINATOR ','
CREATE (:Duties { name: line.Data})
LOAD CSV WITH HEADERS FROM 'file:///unique_skills.csv' AS line FIELDTERMINATOR ','
CREATE (:Skills { name: line.Data})
同样查看一下 技能 的节点添加情况:
注意:由于这些节点信息在保存到csv 文件的过程中我就已经去重了,如果没有提前去重,可以把上面的 Cypher 语句中的所有 CREATE 替换为 MERGE 从而实现添加&&去重。
2 关系信息导入
现在有格式如下的csv 文件:
csv
id,company,title,education,salary
0,广东倾云科技有限公司,【初级】web前端开发工程师,大专,39.0
1,火眼科技(天津)有限公司,IT运维工程师,大专,36.0
2,郑州玉带信息技术有限责任公司,实习web前端开发工程师,大专,42.0
同样首先把文件复制到项目对应文件夹下的import 文件夹中,然后使用Cypher 语句实现数据导入BELONG关 系:
Cypher
LOAD CSV WITH HEADERS FROM 'file:///relation1.csv' AS row
MATCH (a:Job {name: row.id})
MATCH (b:Company {name: row.company})
MERGE (a)-[:BELONG]->(b);
看一下岗位和公司之间的关系 添加 id 和 education 、salary 之间的关系
Cypher
LOAD CSV WITH HEADERS FROM 'file:///relation1.csv' AS row
MATCH (a:Job {name: row.id})
MATCH (b:Education {name: row.education})
MERGE (a)-[:NEED]->(b);
LOAD CSV WITH HEADERS FROM 'file:///relation1.csv' AS row
MATCH (a:Job {name: row.id})
MATCH (b:Salary {name: row.salary})
MERGE (a)-[:OFFER]->(b);
添加 id 和 skill 、duty 、 character 之间的关系
Cypher
LOAD CSV WITH HEADERS FROM 'file:///relation_characters.csv' AS row
MATCH (a:Job {name: row.id})
MATCH (b:Characters {name: row.character})
MERGE (a)-[:REQUIRE]->(b);
LOAD CSV WITH HEADERS FROM 'file:///relation_skills.csv' AS row
MATCH (a:Job {name: row.id})
MATCH (b:Skills {name: row.skill})
MERGE (a)-[:MASTER]->(b);
LOAD CSV WITH HEADERS FROM 'file:///relation_duties.csv' AS row
MATCH (a:Job {name: row.id})
MATCH (b:Duties {name: row.duty})
MERGE (a)-[:RESPONSIBEL]->(b);
id 和 duty 之间的关系