insert.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import pymysql
  2. import uuid
  3. # 连接 MySQL
  4. conn = pymysql.connect(
  5. host='dujunlong',
  6. user='root',
  7. password='123456',
  8. database='knowledge_graph',
  9. charset='utf8mb4'
  10. )
  11. cursor = conn.cursor()
  12. # 1. 获取 Block 类型节点
  13. cursor.execute("SELECT node_guid FROM t_node WHERE node_type = 'Block'")
  14. block_ids = [row[0] for row in cursor.fetchall()]
  15. # 2.1 获取 Node(dd接口) 类型节点
  16. cursor.execute("SELECT node_guid FROM t_node WHERE node_type = 'Node(dd接口)'")
  17. node_ids = [row[0] for row in cursor.fetchall()]
  18. # 2.2 获取 DD-DB 类型节点
  19. cursor.execute("SELECT node_guid FROM t_node WHERE node_type = 'DD-DB'")
  20. dd_ids = [row[0] for row in cursor.fetchall()]
  21. # 3.1 获取关系 id(Block-Node)
  22. cursor.execute("SELECT rela_guid FROM t_rela WHERE rela_name = 'Block-Node'")
  23. relation_block_node = cursor.fetchone()
  24. if not relation_block_node:
  25. raise Exception("关系 Block-Node 不存在")
  26. rela_id_block_node = relation_block_node[0]
  27. # 3.2 获取关系 id(Node-DDDB)
  28. cursor.execute("SELECT rela_guid FROM t_rela WHERE rela_name = 'Node-DDDB'")
  29. relation_node_dd = cursor.fetchone()
  30. if not relation_node_dd:
  31. raise Exception("关系 Node-DDDB 不存在")
  32. rela_id_node_dd = relation_node_dd[0]
  33. # 4. 插入 Block → Node 的三元组
  34. triples_block_node = []
  35. for head_id, tail_id in zip(block_ids, node_ids):
  36. triple_id = uuid.uuid4().hex
  37. triples_block_node.append((triple_id, head_id, rela_id_block_node, tail_id))
  38. cursor.executemany(
  39. "INSERT INTO t_triplet (tri_guid, head_guid, rela_guid, tail_guid) VALUES (%s, %s, %s, %s)",
  40. triples_block_node
  41. )
  42. print(f"成功插入 Block-Node 三元组:{len(triples_block_node)} 条")
  43. # 5. 插入 Node → DD-DB 的三元组
  44. triples_node_dd = []
  45. for head_id, tail_id in zip(node_ids, dd_ids):
  46. triple_id = uuid.uuid4().hex
  47. triples_node_dd.append((triple_id, head_id, rela_id_node_dd, tail_id))
  48. cursor.executemany(
  49. "INSERT INTO t_triplet (tri_guid, head_guid, rela_guid, tail_guid) VALUES (%s, %s, %s, %s)",
  50. triples_node_dd
  51. )
  52. print(f"成功插入 Node-DDDB 三元组:{len(triples_node_dd)} 条")
  53. # 提交事务并关闭连接
  54. conn.commit()
  55. cursor.close()
  56. conn.close()
  57. # import pymysql
  58. # import uuid
  59. # import re
  60. #
  61. # conn = pymysql.connect(
  62. # host='dujunlong',
  63. # user='root',
  64. # password='123456',
  65. # database='knowledge_graph',
  66. # charset='utf8mb4'
  67. # )
  68. # cursor = conn.cursor()
  69. #
  70. # # 获取 Node(dd接口) 节点(假设从 t_node 中 name 字段取出)
  71. # cursor.execute("SELECT node_guid, node_name FROM t_node WHERE node_type = 'Node(dd接口)'")
  72. # node_records = cursor.fetchall()
  73. #
  74. # # 获取关系 ID
  75. # cursor.execute("SELECT rela_guid FROM t_rela WHERE rela_name = 'Node-DDDB'")
  76. # rela_id = cursor.fetchone()
  77. # if not rela_id:
  78. # raise Exception("关系 Node-DDDB 不存在")
  79. # rela_guid = rela_id[0]
  80. #
  81. # triples = []
  82. #
  83. # for node_guid, node_name in node_records:
  84. # # 假设 Node 名称中关联了 DD-DB,比如:G1G2G3
  85. # dd_names = re.findall(r'G\d+', node_name) # 正则提取 G+数字,例如 G1、G2、G10
  86. #
  87. # for dd_name in dd_names:
  88. # # 查询 DD-DB 节点 GUID
  89. # cursor.execute("SELECT node_guid FROM t_node WHERE node_type = 'DD-DB' AND node_name = %s", (dd_name,))
  90. # row = cursor.fetchone()
  91. # if row:
  92. # dd_guid = row[0]
  93. # triple_id = uuid.uuid4().hex
  94. # triples.append((triple_id, node_guid, rela_guid, dd_guid))
  95. # else:
  96. # print(f"警告:未找到名称为 {dd_name} 的 DD-DB 节点")
  97. #
  98. # # 批量插入
  99. # if triples:
  100. # cursor.executemany(
  101. # "INSERT INTO t_triplet (tri_guid, head_guid, rela_guid, tail_guid) VALUES (%s, %s, %s, %s)",
  102. # triples
  103. # )
  104. # print(f"成功插入 Node-DDDB 三元组:{len(triples)} 条")
  105. # else:
  106. # print("没有可插入的三元组")
  107. #
  108. # conn.commit()
  109. # cursor.close()
  110. # conn.close()