1>owl文件还是上述creature.owl文件(protege导出的)
2>jar包:rdf2rdf-1.0.1-2.3.1.jar下载
3>将jar包与owl文件放在同一目录下
4> 进入owl文件所在目录,进入cmd,输入并执行命令:
java -jar rdf2rdf-1.0.1-2.3.1.jar creature.owl creature.turtle
在neo4j里先输入:
CREATE INDEX ON:Resource(uri)
再输入:
CALL semantics.importRDF('file:///C:/Users/user/Desktop/creature.turtle', 'RDF/XML',{})
查一下所有节点
match(n) return n
详情情况参考:
导入数据步骤https://blog.csdn.net/weixin_45426939/article/details/113487216?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522166010025616782395345669%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=166010025616782395345669&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduend~default-1-113487216-null-null.142%5Ev40%5Epc_rank_34_2,185%5Ev2%5Econtrol&utm_term=protege%E5%AF%BC%E5%85%A5neo4j&spm=1018.2226.3001.4187
1.清空当前数据库数据
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r
2.从文件导入数据
// 将hudong_pedia.csv 导入
LOAD CSV WITH HEADERS FROM "file:///hudong_pedia.csv" AS line
CREATE (p:HudongItem{title:line.title,image:line.image,detail:line.detail,url:line.url,openTypeList:line.openTypeList,baseInfoKeyList:line.baseInfoKeyList,baseInfoValueList:line.baseInfoValueList})
// 新增了hudong_pedia2.csv
LOAD CSV WITH HEADERS FROM "file:///hudong_pedia2.csv" AS line
CREATE (p:HudongItem{title:line.title,image:line.image,detail:line.detail,url:line.url,openTypeList:line.openTypeList,baseInfoKeyList:line.baseInfoKeyList,baseInfoValueList:line.baseInfoValueList})
导入概念节点
LOAD CSV WITH HEADERS FROM "file:///cnc_concept_node.csv" AS line
CREATE (n:ConceptNode { title: line.title,type:line.type })
//插入一个节点
CREATE (n:ConceptNode { title: "图谱",type:"类" })
//查找一个节点
MATCH (n:ConceptNode{title:"需求"}) RETURN n
//补充节点信息
LOAD CSV WITH HEADERS FROM "file:///static_weather_list.csv" AS line
MERGE (:Weather { title: line.title })
导入概念间关系
LOAD CSV WITH HEADERS FROM "file:///cnc_concept_relation.csv" AS line
MATCH (entity1:ConceptNode{title:line.node1}) , (entity2:ConceptNode{title:line.node2})
CREATE (entity1)-[:RELATION { type: line.relation }]->(entity2)
导入属性节点
LOAD CSV WITH HEADERS FROM "file:///cnc_node.csv" AS line
CREATE (n:NewNode { title: line.title })
LOAD CSV WITH HEADERS FROM "file:///cnc_node.csv" AS line
CREATE (n:NewNode { title: line.title,type:line.type })
数值Value节点
LOAD CSV WITH HEADERS FROM "file:///cnc_ep_relation.csv" AS line
CREATE (n:Value { value: line.value1 })
- 加载csv文件,定义为 line
- 创建节点:节点名字是p,标签名是HudongItem,定义节点的属性是title,并赋其值为line.title ......
- 语法:
CREATE (
:
{
:
........
:
}
)
- 为刚才导入的节点,创建约束,这样就可以保证titel不会被重复插入
// 创建索引
CREATE CONSTRAINT ON (c:HudongItem)
ASSERT c.title IS UNIQUE
CREATE CONSTRAINT ON (c:NewNode)
ASSERT c.title IS UNIQUE
- 把标签为HudongItem的节点,的title属性定义为唯一的索引
-创建约束删除约束,Neo4j CQL - UNIQUE约束_w3cschool
- 导入关系
// 导入新的节点
LOAD CSV WITH HEADERS FROM "file:///new_node.csv" AS line
CREATE (:NewNode { title: line.title })
//添加索引
CREATE CONSTRAINT ON (c:NewNode)
ASSERT c.title IS UNIQUE
//导入hudongItem和新加入节点之间的关系
LOAD CSV WITH HEADERS FROM "file:///wikidata_relation2.csv" AS line
MATCH (entity1:HudongItem{title:line.HudongItem}) , (entity2:NewNode{title:line.NewNode})
CREATE (entity1)-[:RELATION { type: line.relation }]->(entity2)
LOAD CSV WITH HEADERS FROM "file:///wikidata_relation.csv" AS line
MATCH (entity1:HudongItem{title:line.HudongItem1}) , (entity2:HudongItem{title:line.HudongItem2})
CREATE (entity1)-[:RELATION { type: line.relation }]->(entity2)
LOAD CSV WITH HEADERS FROM "file:///cnc_ep_relation.csv" AS line
MATCH (entity1:NewNode{title:line.node1}) , (entity2:Value{value:line.value1})
CREATE (entity1)-[:RELATION { type: line.relation2 }]->(entity2)
- 标签的变量是临时变量,作用域是当前想要执行的语句,可以根据导入的便捷性自己定义一下。
- 查找节点
查找属性title的值为‘公式1’的节点
match(n:NewNode) where n.title="公式1" return n
Graph连接数据库
Graph的run函数执行cql
from py2neo import Graph, Node, Relationship, cypher, Path
import neo4j
class Neo4j():
graph = None
def __init__(self):
print("create neo4j class ...")
def connectDB(self):
self.graph = Graph("http://localhost:7474", username="neo4j", password="neo4j")
def matchItembyTitle(self,value):
sql = "MATCH (n:Item { title: '" + str(value) + "' }) return n;"
answer = self.graph.run(sql).data()
return answer