parent
eefda6c0e2
commit
a26781be57
@ -0,0 +1,63 @@
|
||||
import json
|
||||
import logging
|
||||
from typing import Dict, List
|
||||
from Util.MySQLUtil import init_mysql_pool
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
async def process_node(node: Dict, parent_id: str = None) -> List[Dict]:
|
||||
"""处理单个知识点节点"""
|
||||
knowledge_point = {
|
||||
"id": node["value"],
|
||||
"title": node["title"],
|
||||
"parent_id": parent_id,
|
||||
"is_leaf": node["isLeaf"],
|
||||
"prerequisite": json.dumps(node.get("PREREQUISITE", [])),
|
||||
"related": json.dumps(node.get("RELATED_TO", []))
|
||||
}
|
||||
|
||||
children = []
|
||||
if not node["isLeaf"] and "children" in node:
|
||||
for child in node["children"]:
|
||||
children.extend(await process_node(child, node["value"]))
|
||||
|
||||
return [knowledge_point] + children
|
||||
|
||||
async def insert_knowledge_points(mysql_pool, knowledge_points: List[Dict]):
|
||||
"""批量插入知识点数据"""
|
||||
async with mysql_pool.acquire() as conn:
|
||||
await conn.ping()
|
||||
async with conn.cursor() as cur:
|
||||
for point in knowledge_points:
|
||||
await cur.execute(
|
||||
"""INSERT INTO knowledge_points
|
||||
(id, title, parent_id, is_leaf, prerequisite, related)
|
||||
VALUES (%s, %s, %s, %s, %s, %s)""",
|
||||
(point["id"], point["title"], point["parent_id"],
|
||||
point["is_leaf"], point["prerequisite"], point["related"])
|
||||
)
|
||||
await conn.commit()
|
||||
|
||||
async def main():
|
||||
"""主函数"""
|
||||
# 初始化MySQL连接池
|
||||
mysql_pool = await init_mysql_pool()
|
||||
|
||||
# 读取JSON文件
|
||||
with open("d:\\dsWork\\dsProject\\dsRag\\Neo4j\\小学数学知识点体系.json", "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
|
||||
# 处理知识点数据
|
||||
knowledge_points = []
|
||||
for node in data["data"]["tree"]:
|
||||
knowledge_points.extend(await process_node(node))
|
||||
|
||||
# 插入数据库
|
||||
await insert_knowledge_points(mysql_pool, knowledge_points)
|
||||
logger.info(f"成功插入 {len(knowledge_points)} 条知识点数据")
|
||||
|
||||
if __name__ == "__main__":
|
||||
import asyncio
|
||||
asyncio.run(main())
|
@ -0,0 +1,10 @@
|
||||
CREATE TABLE knowledge_points (
|
||||
id VARCHAR(32) PRIMARY KEY COMMENT '知识点唯一标识',
|
||||
title VARCHAR(100) NOT NULL COMMENT '知识点标题',
|
||||
parent_id VARCHAR(32) COMMENT '父节点ID',
|
||||
is_leaf BOOLEAN NOT NULL COMMENT '是否为叶子节点',
|
||||
sort INT COMMENT '排序字段',
|
||||
prerequisite JSON COMMENT '先修知识点(仅一级节点)',
|
||||
related JSON COMMENT '相关知识点(仅一级节点)',
|
||||
KEY idx_parent_id (parent_id)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='小学数学知识点体系';
|
Binary file not shown.
Loading…
Reference in new issue