1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| from gremlin_python import statics from gremlin_python.structure.graph import Graph from gremlin_python.process.graph_traversal import __ from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
import xlrd
graph = Graph() connection = DriverRemoteConnection('ws://localhost:8182/gremlin','g', username='root', password='admin') g = graph.traversal().withRemote(connection)
def process(): read_xlsx() return
def addV(vertexType, name): if 0 == len(g.V().hasLabel(vertexType).has("name",name).valueMap().toList()): vertHead = g.addV(vertexType).property("name",name).next()
def addE(edgeType, vheadId, vTailId, properties=None): ''' add edge :param properties: property dict, like {'p1': 'value1', 'p2': 'value2'} ''' index_key = vheadId+vTailId if 0 == len(g.E().hasLabel(edgeType).has("index_key",index_key).valueMap().toList()): edge = g.V(vTailId).as_("t").V(vheadId).addE(edgeType).to("t").property("index_key",index_key) if properties: for key in properties.keys(): edge.property(key, properties.get(key)) edge.next()
def get_vertex_id(vertexType, name): return g.V().hasLabel(vertexType).has("name",name).id().next()["@value"]
def read_xlsx(): data = xlrd.open_workbook("triple0707.xlsx")
table0 = data.sheets()[0] nrow0 = table0.nrows for row_index in range(1,nrow0-1): olympics = table0.cell_value(row_index,0) tech_name = table0.cell_value(row_index,2)
addV("olympics", olympics) addV("technology",tech_name) vHeadId = get_vertex_id("olympics", olympics) vTailId = get_vertex_id("technology", tech_name) addE("apply", vHeadId, vTailId)
table1 = data.sheets()[1] nrow1 = table1.nrows for row_index in range(1,nrow0-1): tech_name = table1.cell_value(row_index,0) olympics_aspect_2 = table1.cell_value(row_index,2) olympics_aspect_1 = table1.cell_value(row_index,3)
addV("technology",tech_name) addV("olympics_aspect_2", olympics_aspect_2) addV("olympics_aspect_1", olympics_aspect_1) vHeadId = get_vertex_id("technology", tech_name) vTailId = get_vertex_id("olympics_aspect_2", olympics_aspect_2) addE("support", vHeadId, vTailId)
vHeadId = vTailId vTailId = get_vertex_id("olympics_aspect_1", olympics_aspect_1) addE("contain", vHeadId, vTailId)
table2 = data.sheets()[2] nrow2 = table2.nrows for row_index in range(1,nrow2-1): tech_name = table2.cell_value(row_index,0) domain_name = table2.cell_value(row_index,2)
addV("technology", tech_name) addV("technology_domain",domain_name) vHeadId = get_vertex_id("technology", tech_name) vTailId = get_vertex_id("technology_domain", domain_name) addE("belong_to", vHeadId, vTailId)
table3 = data.sheets()[3] nrow3 = table3.nrows for row_index in range(1,nrow3-1): domain_name = table3.cell_value(row_index,0) olympics = table3.cell_value(row_index,2) weight = table3.cell_value(row_index,3)
addV("technology_domain",domain_name) addV("olympics", olympics) vHeadId = get_vertex_id("technology_domain", domain_name) vTailId = get_vertex_id("olympics", olympics) addE("support", vHeadId, vTailId, properties={"weight":weight}) return
try: process() except Exception as e: print(e) finally: connection.close()
|