1. 工具准备
1.1 Protege 安装
版本说明:Protégé-5.5.0
了解其基本使用。
1.2 Neo4j 安装
注意 JDK 版本的切换。
2. Neo4j 导入 Protege 文件
2.1 启动 Neo4j
版本:JDK11 win+R 输入 cmd
neo4j.bat console
2.2 Protege 导出 OWL 文件转 Turtle 文件
在 Protege 中导出 owl 文件后,需转换为 turtle 格式。若遇到无法生成或生成文件为 0KB 的情况,请检查 JDK 版本配置。
确保系统环境变量 JAVA_HOME 指向正确的 JDK 版本(如 JDK11)。
在包含 jar 包的文件夹中,打开 PowerShell 执行以下指令(替换源文件和输出文件名):
java -jar rdf2rdf-1.0.1-2.3.1.jar creature.owl creature.turtle
转换成功后,确认文件大小不为 0KB。
2.3 导入 Neo4j
1. 清除数据库中的所有数据
MATCH (n) DETACH DELETE n;
2. 初始化 RDF 导入配置
CALL n10s.graphconfig.init();
3. 导入 RDF 数据
CALL n10s.rdf.import.fetch("file:///E:/BaiduNetdiskDownload/owl 导入 neo4j/第一单元_test.turtle", "RDF/XML", {handleVocabUris: "IGNORE"})
注意修改你自己的文件地址。
4. 查询所有(部分)数据
MATCH (n) WHERE n:`节点名称 1` OR n:`节点名称 2` OR n:`节点名称 3` RETURN n LIMIT 500;
5. 查询边关系
MATCH ()-[r]->() RETURN DISTINCT type(r) AS relationshipType
6. 一些细节
节点信息的 URI 有较长前缀,可使用以下指令清理:
MATCH (n) SET n.uri = REPLACE(n.uri, 'http://www.semanticweb.org/florence/ontologies/2025/1/untitled-ontology-9#', '') RETURN n
注意更换你自己的 URI 前缀。
3. Neo4j 导出 JSON 文件
导出 JSON 文件的目的是方便后续利用 Echarts 进行可视化。需要借助 APOC 库。
检验是否下载成功:
RETURN apoc.version()
导出文件:
CALL apoc.export.json.query(
"MATCH (n) WHERE n:`节点名称 1` OR n:`节点名称 2` OR n:`节点名称 3` OPTIONAL MATCH (n)-[r]->(m) RETURN n, r, m LIMIT 500",
"file:///E:/neo4j/neo4j-community-4.4.41/import/output_3.0.json",
{format: 'PLAIN'}
)
代码主体说明:
- 第一行:查询所有节点 + 匹配关系 + 返回所有信息
- 第二行:导出文件路径及文件名称
- 第三行:指定导出的格式为
PLAIN,即简单的 JSON 格式
至此,原始数据已经获得。下面将进行数据的处理,然后利用 Echarts 和 Vue 实现前端可视化。
4. 可视化前的操作
4.1 利用 Python 对数据进行处理
在导入 Echarts 之前,发现从 Neo4j 导出的 JSON 文件并不符合 Echarts 的数据格式,因此需要利用 Python 对数据进行处理。
以下是 Python 代码示例,记得修改输入和输出文件的地址:
import json
def convert_to_echarts_data(file_path):
# 加载 Neo4j 导出的 JSON 数据
with open(file_path, 'r', encoding='utf-8') as f:
neo4j_data = json.load(f)
# 初始化 ECharts 数据
echarts_data = {
"nodes": [],
"links": []
}
# 节点处理
node_map = {}
for item in neo4j_data:
node = item.get("n", None)
if node:
node_id = node.get("id")
node_labels = node.get("labels", [])
node_properties = node.get("properties", {})
node_name = node_properties.get("名称", node_properties.get("name", node_id))
node_uri = node_properties.get("uri", node_id)
if node_id not in node_map:
echarts_node = {
"id": node_id,
"name": node_name,
"category": node_labels[1] if node_labels else "其他",
"value": node_uri,
"properties": node_properties
}
echarts_data["nodes"].append(echarts_node)
node_map[node_id] = echarts_node
m_node = item.get("m", None)
if m_node:
node_id = m_node.get("id")
node_labels = m_node.get(, [])
node_properties = m_node.get(, {})
node_name = node_properties.get(, node_properties.get(, node_id))
node_uri = node_properties.get(, node_id)
node_id node_map:
echarts_node = {
: node_id,
: node_name,
: node_labels[] node_labels ,
: node_uri,
: node_properties
}
echarts_data[].append(echarts_node)
node_map[node_id] = echarts_node
item neo4j_data:
relationship = item.get(, )
relationship:
start_node = item.get().get()
start_id = start_node.get()
end_node = item.get().get()
end_id = end_node.get()
relationship_label = relationship.get()
echarts_link = {
: start_id,
: end_id,
: relationship_label,
}
echarts_data[].append(echarts_link)
echarts_data
__name__ == :
input_file =
output_file =
echarts_data = convert_to_echarts_data(input_file)
(output_file, , encoding=) f:
json.dump(echarts_data, f, ensure_ascii=, indent=)
()
最后输出的 JSON 数据将包含 nodes 和 links 数组,符合 Echarts 格式要求。
4.2 学习 Vue & Echarts
结合上述处理后的数据,利用 Vue 框架和 Echarts 库即可实现知识图谱的前端可视化展示。


