# 请注意,跨docker访问的时候尽量还是配置一下neo4j的配置文件
FROM neo4j
EXPOSE 7474
EXPOSE 7687
此处由于导入csv到dockerfile有点复杂,还是直接手动添加数据,下面算是自己实际的一个例子,可惜了Cypher不能编写函数。
// 加载滚石节点数据 LOAD CSV WITH HEADERS FROM "file:///data/data/RockfallHazardKGtailnode.csv" AS row CREATE (n:RockfallHazardKGnode {id: row.ID, name: row.name, level: row.level}) WITH n // 加载关系数据 LOAD CSV WITH HEADERS FROM "file:///data/data/RockfallHzazrdKGrelation2.csv" AS line OPTIONAL MATCH (start:RockfallHazardKGnode {id: line.START_ID}) OPTIONAL MATCH (end:RockfallHazardKGnode {id: line.END_ID}) MERGE (start)-[:RELATION {type: line.TYPE, community: line.community}]->(end)
// 加载滑坡节点数据 LOAD CSV WITH HEADERS FROM "file:///data/data/LandslidesHazardKGtailnode.csv" AS row CREATE (n:LandslidesHazard2KGnode {id: row.ID, name: row.name, level: row.level}) WITH n // 加载关系数据 LOAD CSV WITH HEADERS FROM "file:///data/data/LandslidesHzazrdKGrelation.csv" AS line OPTIONAL MATCH (start:LandslidesHazard2KGnode {name: line.START_ID}) OPTIONAL MATCH (end:LandslidesHazard2KGnode {name: line.END_ID}) MERGE (start)-[:RELATION {type: line.TYPE, community: line.community}]->(end)
// 加载泥石流节点数据 LOAD CSV WITH HEADERS FROM "file:///data/data/DebrisflowHazardKGnode.csv" AS row CREATE (n:DebrisflowHazardKGnode {id: row.ID, name: row.name, level: row.level}) WITH n // 加载关系数据 LOAD CSV WITH HEADERS FROM "file:///data/data/DebrisflowHazardKGrelation.csv" AS line OPTIONAL MATCH (start:DebrisflowHazardKGnode {id: line.START_ID}) OPTIONAL MATCH (end:DebrisflowHazardKGnode {id: line.END_ID}) MERGE (start)-[:RELATION {type: line.TYPE, community: line.community}]->(end)
"/neo4j": {
target: "http://localhost:7474",
changeOrigin: true,
rewrite(path) {
return path.replace(/^\/neo4j/, "/db/neo4j/tx/commit") # 注意版本
}
},
请注意,如果你请求不到数据,一定要注意neo4j的版本,不同版本的提交地址不同,比如上面的/db/neo4j/tx/commit。同时/db/database
/tx/commit中的数据库换成你现在激活的数据库(存放数据的)。同时为了方便,在Vite项目依赖的.env环境文件中配置图形数据库配置项。
# 图形数据库账号和密码
VITE_NEO4J_USERNAME = 'neo4j'
VITE_NEO4J_PASSWORD = 'password'
//封装获取neo4j数据请求
async function query(query) {
//创建axios实例
const neo4jApi = axios.create({
baseURL: "/neo4j",
auth: {
username: import.meta.env.VITE_NEO4J_USERNAME,
password: import.meta.env.VITE_NEO4J_PASSWORD
},
headers: {
"Content-Type": "application/json",
Accept: "application/json"
}
})
//发送请求
const response = await neo4jApi.post("", {
statements: [
{
statement: query,
resultDataContents: ["graph"]
}
]
})
//提取图谱数据
const graphData = response.data.results[0].data.map(
(graphObj) => graphObj.graph
)
return graphData
}
query组的查询指令(Cypher查询语句)
let queryStr: Array<Array<string>> = [
[
//滑坡
"MATCH (n:LandslidesHazard2KGnode) return n", //节点
"MATCH p=(:LandslidesHazard2KGnode)-->(:LandslidesHazard2KGnode) RETURN p"
],
[
//落石
"MATCH (n:RockfallHazardKGnode) return n", //节点
"MATCH p=(:RockfallHazardKGnode)-->(:RockfallHazardKGnode) RETURN p"
],
[
//泥石流
"MATCH (n:DebrisflowHazardKGnode) return n", //节点
"MATCH p=(:DebrisflowHazardKGnode)-->(:DebrisflowHazardKGnode) RETURN p"
]
]
# 选择对应的属性列
match (n:xxxNode {name: 'xxx'}) return n.name,id(n),n.xxxProperty
# 多个节点变量
match (n:xxxNode {name: 'xxx'}),(m:yyyNode) where m.xxxProperty='sss' return n.xxx1,m.xxx2,m.xxx3
# 创建节点
create (n:xxxNode {name: 'xxx',age: 'xxx'}) return n
# 创建的节点具有两个节点(抽象父类)
create (n:xxxNode:yyyNode {name: 'xxx'}) return n
# 创建关系(node1节点和node2节点之间的关系->同一类型节点,关系名称为RELATION,这个关系为一个json对象)
create (node1)-[:RELATION {type: line.TYPE, community: line.community}]-->(node2)
# 删除两个节点的关系(如果存在关系则不能进行删除)
match (n:xxxNode)-[r:RELATION]-->(m:xxxNode) where n.name='xxx' delete r
# 删除节点
match (n:xxxNode) delete n
# 查询指定节点的关系
match p=(:xxxNode {name:'xxx'})-[r:`xxxx关系`]-->() return p
# 属性移除(取消继承的节点)
match (n:xxxNode {name:'xxxx'}) remove n:xxxNode return n