def create_graph(GRAPHDB): # Do we have a node that has a 'name' property, which is set to the value 'Neo'? # We've probably been here before. data, metadata = cypher.execute(GRAPHDB, "START n=node(*) where n.name='Neo' return n") if not data: # Create two nodes, one for us and one for you. # Make sure they both have 'name' properties with values. from_node, to_node = GRAPHDB.create({"name": "Neo"}, {"name": "you"}) # create a 'loves' relationship from the 'from' node to the 'to' node GRAPHDB.create((from_node, "loves", to_node),)
def saveToNeo4j(found_pages, found_links): print "++: saving to neo4j" title_index = GRAPHDB.get_or_create_index(neo4j.Node, "TitleIndex") i=0 for link in found_links: pageA, pageB = link nodeA = title_index.get_or_create("title", pageA, {"title": pageA}) nodeB = title_index.get_or_create("title", pageB, {"title": pageB}) GRAPHDB.create((nodeA, "links_to", nodeB)) if not i % 100: print i i += 1
def saveToNeo4jBatch(found_pages, found_links): url_index = GRAPHDB.get_or_create_index(neo4j.Node, "UrlIndex") pageToNode = {} for page in found_pages: name = getNameFromLink(page) node = GRAPHDB.create({"name":name, "url":page})[0] # TODO: add labels based on infobox pageToNode[page] = node # save links i = 0 batch = neo4j.WriteBatch(GRAPHDB) for link in found_links: pageA, pageB = link nodeA = pageToNode.get(pageA) nodeB = pageToNode.get(pageB) batch.get_or_create_path(nodeA, "links_to", nodeB) if not i % 100: print "i: " + str(i) batch.run() batch = neo4j.WriteBatch(GRAPHDB) i += 1 batch.run() print "total num links created: " + str(i)
def handle_row_inner(inner_row): nodeB = inner_row[0] weight = random.random() print "w: " + str(weight) if nodeA != nodeB: GRAPHDB.create((nodeA, "RELEVANCY", nodeB, {"weight": weight}),)