Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
E
event
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
陈世强
event
Commits
4fc932b9
提交
4fc932b9
authored
6月 12, 2025
作者:
obcy
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
【添加图谱查询功能】
上级
6aebbc65
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
197 行增加
和
4 行删除
+197
-4
Neo4jConfig.java
src/main/java/com/zzsn/event/neo4j/config/Neo4jConfig.java
+10
-2
AtlasController.java
...java/com/zzsn/event/neo4j/controller/AtlasController.java
+41
-0
AtlasService.java
src/main/java/com/zzsn/event/neo4j/service/AtlasService.java
+68
-0
Neo4jUtil.java
src/main/java/com/zzsn/event/neo4j/util/Neo4jUtil.java
+78
-2
没有找到文件。
src/main/java/com/zzsn/event/neo4j/config/Neo4jConfig.java
浏览文件 @
4fc932b9
...
...
@@ -4,6 +4,7 @@ import org.neo4j.driver.AuthToken;
import
org.neo4j.driver.AuthTokens
;
import
org.neo4j.driver.Driver
;
import
org.neo4j.driver.GraphDatabase
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -13,11 +14,18 @@ import org.springframework.context.annotation.Configuration;
*/
@Configuration
public
class
Neo4jConfig
{
@Value
(
"${neo4j.username:neo4j}"
)
private
String
username
;
@Value
(
"${neo4j.password:zzsn9988}"
)
private
String
password
;
@Value
(
"${neo4j.url:bolt://server-1-95-128-5.ciglobal.cn:6595}"
)
private
String
url
;
@Bean
(
"neo4jDriver"
)
public
Driver
driver
(){
AuthToken
basic
=
AuthTokens
.
basic
(
"neo4j"
,
"ZZsn9988"
);
AuthToken
basic
=
AuthTokens
.
basic
(
username
,
password
);
// Driver driver = GraphDatabase.driver("bolt://114.115.132.141:7687", basic);
Driver
driver
=
GraphDatabase
.
driver
(
"bolt://server-1-95-83-185.ciglobal.cn:7687"
,
basic
);
Driver
driver
=
GraphDatabase
.
driver
(
url
,
basic
);
return
driver
;
}
...
...
src/main/java/com/zzsn/event/neo4j/controller/AtlasController.java
0 → 100644
浏览文件 @
4fc932b9
package
com
.
zzsn
.
event
.
neo4j
.
controller
;
import
com.zzsn.event.constant.Result
;
import
com.zzsn.event.neo4j.service.AtlasService
;
import
io.swagger.annotations.Api
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.web.bind.annotation.GetMapping
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
@Slf4j
@Api
(
tags
=
"图谱查询"
)
@RestController
@RequestMapping
(
"/atlas"
)
public
class
AtlasController
{
@Autowired
AtlasService
atlasService
;
@RequestMapping
(
"/test"
)
public
Result
<?>
test
(
String
sql
,
String
databaseName
){
return
atlasService
.
test
(
sql
,
databaseName
);
}
@RequestMapping
(
"/test1"
)
public
Result
<?>
test1
(
String
name
,
String
databaseName
){
return
atlasService
.
test1
(
name
,
databaseName
);
}
@GetMapping
(
"/getAtlasBysubjectId"
)
public
Result
<?>
getAtlasBysubjectId
(
String
subjectId
){
return
atlasService
.
getAtlasBysubjectId
(
subjectId
);
}
@GetMapping
(
"/getAtlasBysubjectIdAndName"
)
public
Result
<?>
getAtlasBysubjectIdAndName
(
String
subjectId
,
String
name
){
return
atlasService
.
getAtlasBysubjectIdAndName
(
subjectId
,
name
);
}
}
src/main/java/com/zzsn/event/neo4j/service/AtlasService.java
0 → 100644
浏览文件 @
4fc932b9
package
com
.
zzsn
.
event
.
neo4j
.
service
;
import
cn.hutool.core.util.StrUtil
;
import
com.zzsn.event.constant.Result
;
import
com.zzsn.event.neo4j.util.Neo4jUtil
;
import
com.zzsn.event.neo4j.vo.GraphInfo
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
java.sql.Struct
;
@Slf4j
@Service
public
class
AtlasService
{
@Autowired
Neo4jUtil
neo4jUtil
;
public
Result
<?>
test
(
String
sql
,
String
databaseName
)
{
GraphInfo
nodeAndLink
=
neo4jUtil
.
getNodeAndLink
(
sql
,
databaseName
);
return
Result
.
OK
(
nodeAndLink
);
}
public
Result
<?>
test1
(
String
name
,
String
databaseName
)
{
StringBuilder
a
=
new
StringBuilder
(
"MATCH p=(n)-[r]->(m) "
)
;
if
(
StrUtil
.
isNotBlank
(
name
)){
a
.
append
(
"WHERE n.entity_id =~ '.*"
+
name
+
".*' "
+
" OR m.entity_id =~ '.*"
+
name
+
".*' "
+
" OR TYPE(r) =~ '.*"
+
name
+
".*' "
);
}
a
.
append
(
"RETURN p, n, m "
);
a
.
append
(
"LIMIT 200"
);
GraphInfo
nodeAndLink
=
neo4jUtil
.
getNodeAndLink
(
a
.
toString
(),
databaseName
);
return
Result
.
OK
(
nodeAndLink
);
}
public
Result
<?>
getAtlasBysubjectId
(
String
subjectId
)
{
GraphInfo
nodeAndLink
=
neo4jUtil
.
getNodeAndLink
(
"MATCH p=(n)-->(m) RETURN p,n,m LIMIT 200"
,
"lgRAGSJ"
+
subjectId
);
return
Result
.
OK
(
nodeAndLink
);
}
public
Result
<?>
getAtlasBysubjectIdAndName
(
String
subjectId
,
String
name
)
{
GraphInfo
nodeAndLink
=
neo4jUtil
.
getNodeAndLink
(
"MATCH (n)-[r]->(m) "
+
"WHERE n.entity_id =~ '.*"
+
name
+
".*' OR "
+
"m.entity_id =~ '.*"
+
name
+
".*' OR "
+
"TYPE(r) =~ '.*"
+
name
+
".*' "
+
"RETURN r, n, m LIMIT 200"
,
"lgRAGSJ"
+
subjectId
);
return
Result
.
OK
(
nodeAndLink
);
}
public
Result
<?>
getAtlasBysubjectIdAndNamePath
(
String
subjectId
,
String
name
)
{
GraphInfo
nodeAndLink
=
neo4jUtil
.
getNodeAndLink
(
"MATCH p=(n)-[r]->(m) "
+
"WHERE n.name =~ '.*"
+
name
+
".*' "
+
" OR m.name =~ '.*"
+
name
+
".*' "
+
" OR TYPE(r) =~ '.*"
+
name
+
".*' "
+
"RETURN p, n, m "
+
"LIMIT 200"
,
"lgRAGSJ"
+
subjectId
);
return
Result
.
OK
(
nodeAndLink
);
}
}
src/main/java/com/zzsn/event/neo4j/util/Neo4jUtil.java
浏览文件 @
4fc932b9
...
...
@@ -26,8 +26,44 @@ public class Neo4jUtil {
@Resource
public
Driver
neo4jDriver
;
/**
* 执行 Cypher 查询并返回处理后的记录列表
*/
public
List
<
Record
>
executeCypher
(
String
cypherSql
,
String
databaseName
)
{
List
<
Record
>
resultList
=
new
ArrayList
<>();
try
(
Session
session
=
neo4jDriver
.
session
(
SessionConfig
.
builder
()
.
withDatabase
(
databaseName
)
.
build
()))
{
Result
result
=
session
.
run
(
cypherSql
);
// 在会话关闭前将结果转换为列表
while
(
result
.
hasNext
())
{
resultList
.
add
(
result
.
next
());
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
log
.
error
(
"执行 Cypher 查询失败: "
+
e
.
getMessage
());
}
return
resultList
;
}
/**
* @description 获取执行结果,指定数据库名称,需要在会话关闭前立即消费数据
*
* @author kongliufeng
* @Date 2022-03-13
*/
public
Result
executeCypherSql
(
String
cypherSql
,
String
databaseName
)
{
Result
run
=
null
;
try
(
Session
session
=
neo4jDriver
.
session
(
SessionConfig
.
builder
()
.
withDatabase
(
databaseName
)
.
build
()))
{
System
.
out
.
println
(
cypherSql
);
run
=
session
.
run
(
cypherSql
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
log
.
error
(
e
.
getMessage
());
}
return
run
;
}
/**
* @description 获取执行结果
*
...
...
@@ -102,6 +138,44 @@ public class Neo4jUtil {
graphInfo
.
setLabels
(
labels
);
return
graphInfo
;
}
/**
* 可传库名
* match (n:{name=''}) return n
* 获取指定节点
* @author kongliufeng
* @data 2019-12-10 下午1:50:18
*/
public
GraphInfo
getNodeAndLink
(
String
cypherSql
,
String
databaseName
)
{
// Result result = executeCypherSql(cypherSql,databaseName);
List
<
Record
>
records
=
executeCypher
(
cypherSql
,
databaseName
);
GraphInfo
graphInfo
=
new
GraphInfo
();
if
(
records
==
null
||
records
.
isEmpty
())
{
return
graphInfo
;
}
// 使用Set避免重复节点和关系
Set
<
Node
>
nodes
=
new
HashSet
<>();
Set
<
Relationship
>
relationships
=
new
HashSet
<>();
records
.
forEach
(
record
->
{
// 处理记录中的每个字段
for
(
String
key
:
record
.
keys
())
{
Value
value
=
record
.
get
(
key
);
processValue
(
value
,
nodes
,
relationships
);
}
});
graphInfo
.
setNodes
(
convertNodes
(
nodes
));
graphInfo
.
setLinks
(
convertRelationships
(
relationships
));
Set
<
String
>
collect
=
graphInfo
.
getNodes
().
stream
().
map
(
NodeData:
:
getLabels
).
flatMap
(
Set:
:
stream
).
collect
(
Collectors
.
toSet
());
List
<
HashMap
<
String
,
String
>>
labels
=
new
ArrayList
<>();
for
(
String
label
:
collect
)
{
HashMap
<
String
,
String
>
map
=
new
HashMap
<>();
map
.
put
(
"name"
,
label
);
labels
.
add
(
map
);
}
graphInfo
.
setLabels
(
labels
);
return
graphInfo
;
}
private
List
<
NodeData
>
convertNodes
(
Set
<
Node
>
nodes
)
{
List
<
NodeData
>
nodeList
=
new
ArrayList
<>();
for
(
Node
node
:
nodes
)
{
...
...
@@ -112,6 +186,7 @@ public class Neo4jUtil {
list
.
add
(
item
);
}
nodeData
.
setLabels
(
list
);
nodeData
.
setName
(
node
.
asMap
().
get
(
"entity_id"
).
toString
());
nodeData
.
setProperties
(
node
.
asMap
());
nodeList
.
add
(
nodeData
);
}
...
...
@@ -127,6 +202,7 @@ public class Neo4jUtil {
linkData
.
setSourceId
(
rel
.
startNodeId
());
linkData
.
setTargetId
(
rel
.
endNodeId
());
linkData
.
setProperties
(
rel
.
asMap
());
linkData
.
setName
(
rel
.
asMap
().
get
(
"keywords"
).
toString
());
linkList
.
add
(
linkData
);
}
return
linkList
;
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论