def add_node_collection_to_graph(self,
                                     db_name,
                                     collection_name,
                                     label,
                                     limit=0):
        """Adds all nodes in Mongo collection to Neo4j.

        :param str db_name: name of Mongo database
        :param str collection_name: name of collection in the MongoDB
        :param str label:
        :param int limit: if > 0, only limit records are added to the graph
        :rtype None:
        """
        c = self.get_collection(db_name, collection_name)
        cur = c.find(limit=limit)

        # Create a write batch to add nodes to
        batch = neo4j.WriteBatch(self)

        # Iterate over all records in the collection and add corresponding node
        for i, d in enumerate(cur):
            anode = self.get_or_add_node_to_batch(d,
                                                  label_index=label,
                                                  batch=batch,
                                                  stub='False')
            if (i % 100) == 0:
                batch.submit()
                batch = neo4j.WriteBatch(self)
                print 'add_node_collection_to_graph', label, i, self.order
        batch.submit()
示例#2
0
 def test_complex_relate(self):
     alice, bob, carol, dave = self.graph_db.create({"name": "Alice"},
                                                    {"name": "Bob"},
                                                    {"name": "Carol"},
                                                    {"name": "Dave"})
     batch = neo4j.WriteBatch(self.graph_db)
     batch.get_or_create_path(alice, ("IS~MARRIED~TO", {
         "since": 1996
     }), bob)
     #batch.get_or_create((alice, "DISLIKES", carol, {"reasons": ["youth", "beauty"]}))
     batch.get_or_create_path(alice, ("DISLIKES!", {
         "reason": "youth"
     }), carol)
     rels1 = batch.submit()
     self.assertTrue(rels1 is not None)
     self.assertEqual(2, len(rels1))
     batch = neo4j.WriteBatch(self.graph_db)
     batch.get_or_create_path(bob, ("WORKS WITH", {
         "since": 2004,
         "company": "Megacorp"
     }), carol)
     #batch.get_or_create((alice, "DISLIKES", carol, {"reasons": ["youth", "beauty"]}))
     batch.get_or_create_path(alice, ("DISLIKES!", {
         "reason": "youth"
     }), carol)
     batch.get_or_create_path(bob, ("WORKS WITH", {
         "since": 2009,
         "company": "Megacorp"
     }), dave)
     rels2 = batch.submit()
     self.assertTrue(rels2 is not None)
     self.assertEqual(3, len(rels2))
     self.assertEqual(rels1[1], rels2[1])
示例#3
0
    def Test1(self):
        startTime = datetime.datetime.now()

        num_nodes = 100000
        num_labels = 1000

        print "Adding %d labels to %d nodes" % (num_labels, num_nodes)
        print "(# nodes in label ~ Uniform)"

        nodes_in_label = map( lambda x: int(round(x)), np.random.uniform(low=0,high=num_nodes,size=num_labels) )

        cnt = 0        
        wbatch = neo4j.WriteBatch(self.gdb)
        for index, num in enumerate(nodes_in_label):
            print "--adding label_%d to %d nodes" % (index, num)
            wbatch.append_cypher(self._getAddLabelCypher('label_%d' % index), {'num': num})
            cnt += 1
            if cnt == 5:
                wbatch.submit()
                cnt = 0
                wbatch = neo4j.WriteBatch(self.gdb)

        
        endTime = datetime.datetime.now()
        
        print "%d labels added to %d nodes" % (num_labels, num_nodes)
        print endTime - startTime
示例#4
0
def test_is_idempotent(graph):
    alice, = graph.create({"name": "Alice"})
    batch = neo4j.WriteBatch(graph)
    batch.get_or_create_path(alice, "KNOWS", {"name": "Bob"})
    results = batch.submit()
    path = results[0]
    bob = path.nodes[1]
    assert path.nodes[0] == alice
    assert bob["name"] == "Bob"
    batch = neo4j.WriteBatch(graph)
    batch.get_or_create_path(alice, "KNOWS", {"name": "Bob"})
    results = batch.submit()
    path = results[0]
    assert path.nodes[0] == alice
    assert path.nodes[1] == bob
def test_can_add_labels_to_node_in_same_batch(graph):
    batch = neo4j.WriteBatch(graph)
    a = batch.create({"name": "Alice"})
    batch.add_labels(a, "human", "female")
    results = batch.submit()
    alice = results[batch.find(a)]
    assert alice.get_labels() == {"human", "female"}
def test_can_set_property_on_preexisting_node():
    graph_db = neo4j.GraphDatabaseService()
    alice, = graph_db.create({"name": "Alice"})
    batch = neo4j.WriteBatch(graph_db)
    batch.set_property(alice, "age", 34)
    batch.run()
    assert alice["age"] == 34
    def createAnalysis(self,
                       target_uuid=None,
                       target_genid=None,
                       data_link=None,
                       results_uuid=None,
                       description_uuid=None):
        if (target_uuid is None) == (target_genid is None):
            raise Exception(
                "Exactly one of the following parameters must be specified: target_uuid, target_genid "
            )

        if target_genid is not None:
            target_node = self._getNodeByGenid(label=BIOENTITY,
                                               genid=target_genid)
        else:
            target_node = self._getNodeByUUID(label=RAW_DATA, uuid=target_uuid)

        wbatch = neo4j.WriteBatch(self.gdb)
        params = {}
        params['uuid'] = self._getNewUUID()
        if data_link is not None:
            params['data_link'] = data_link
        if results_uuid is not None:
            params['results_uuid'] = results_uuid
        if description_uuid is not None:
            params['description_uuid'] = description_uuid
        analysis_node = wbatch.create(node(params))
        wbatch.add_labels(analysis_node, ANALYSIS)
        wbatch.create(rel(target_node, HAS_ANALYSIS, analysis_node))
        wbatch.submit()
        return params['uuid']
示例#8
0
def test_is_not_idempotent():
    graph_db = neo4j.GraphDatabaseService()
    alice, = graph_db.create({"name": "Alice"})
    batch = neo4j.WriteBatch(graph_db)
    batch.create_path(alice, "KNOWS", {"name": "Bob"})
    results = batch.submit()
    path = results[0]
    bob = path.nodes[1]
    assert path.nodes[0] == alice
    assert bob["name"] == "Bob"
    batch = neo4j.WriteBatch(graph_db)
    batch.create_path(alice, "KNOWS", {"name": "Bob"})
    results = batch.submit()
    path = results[0]
    assert path.nodes[0] == alice
    assert path.nodes[1] != bob
示例#9
0
 def _update_index(self, props):
     for cls in self.__class__.mro():
         if cls.__name__ == 'StructuredNode' or cls.__name__ == 'ReadOnlyNode':
             break
         batch = neo4j.WriteBatch(self.client)
         for key, value in props.iteritems():
             if key in cls.__dict__.keys():
                 node_property = cls.get_property(key)
                 if node_property.unique_index:
                     try:
                         batch.add_indexed_node_or_fail(
                             cls.index.__index__, key, value, self.__node__)
                     except NotImplementedError:
                         batch.get_or_add_indexed_node(
                             cls.index.__index__, key, value, self.__node__)
                 elif node_property.index:
                     batch.add_indexed_node(cls.index.__index__, key, value,
                                            self.__node__)
         requests = batch.requests
         try:
             i = 0
             for r in batch._submit():
                 if r.status == 200:
                     raise UniqueProperty(requests[i], cls.index.name)
                 i = i + 1
         except rest.ResourceConflict as r:
             raise UniqueProperty(requests[r.id], cls.index.name)
示例#10
0
def test_can_delete_property_on_preexisting_node(graph):
    alice, = graph.create({"name": "Alice", "age": 34})
    batch = neo4j.WriteBatch(graph)
    batch.delete_property(alice, "age")
    batch.run()
    assert alice["name"] == "Alice"
    assert alice["age"] is None
def test_can_set_properties_on_preexisting_node(graph):
    alice, = graph.create({})
    batch = neo4j.WriteBatch(graph)
    batch.set_properties(alice, {"name": "Alice", "age": 34})
    batch.run()
    assert alice["name"] == "Alice"
    assert alice["age"] == 34
示例#12
0
文件: graph.py 项目: mt5225/glab
 def add_ref(self, source, relationship, dest):
     """
     add relation for two nodes
     """
     batch = neo4j.WriteBatch(self.graph_db)
     batch.create(rel(source, relationship, dest))
     batch.submit()
示例#13
0
def test_can_remove_labels_from_preexisting_node(graph):
    alice, = graph.create({"name": "Alice"})
    alice.add_labels("human", "female")
    batch = neo4j.WriteBatch(graph)
    batch.remove_label(alice, "human")
    batch.run()
    assert alice.get_labels() == {"female"}
def test_can_add_labels_to_preexisting_node():
    graph_db = neo4j.GraphDatabaseService()
    alice, = graph_db.create({"name": "Alice"})
    batch = neo4j.WriteBatch(graph_db)
    batch.add_labels(alice, "human", "female")
    batch.run()
    assert alice.get_labels() == {"human", "female"}
示例#15
0
    def save(self, subj, node=None):
        """ Save an object to a database node.

        :param subj: the object to save
        :param node: the database node to save to (if omitted, will re-save to
            same node as previous save)
        """
        if node is not None:
            subj.__node__ = node
        # naively copy properties from object to node
        props = {}
        for key, value in subj.__dict__.items():
            if not key.startswith("_"):
                props[key] = value
        if hasattr(subj, "__node__"):
            subj.__node__.set_properties(props)
            query = neo4j.CypherQuery(self.graph, "START a=node({A}) "
                                                     "MATCH (a)-[r]->(b) "
                                                     "DELETE r")
            query.execute(A=subj.__node__._id)
        else:
            subj.__node__, = self.graph.create(props)
        # write rels
        if hasattr(subj, "__rel__"):
            batch = neo4j.WriteBatch(self.graph)
            for rel_type, rels in subj.__rel__.items():
                for rel_props, endpoint in rels:
                    end_node = self._get_node(endpoint)
                    if not neo4j.familiar(end_node, self.graph):
                        raise ValueError(end_node)
                    batch.create((subj.__node__, rel_type, end_node, rel_props))
            batch.run()
        return subj
def test_can_delete_properties_on_preexisting_node(graph):
    alice, = graph.create({"name": "Alice", "age": 34})
    batch = neo4j.WriteBatch(graph)
    batch.delete_properties(alice)
    batch.run()
    props = alice.get_properties()
    assert props == {}
def test_can_set_property_on_node_in_same_batch(graph):
    batch = neo4j.WriteBatch(graph)
    alice = batch.create({"name": "Alice"})
    batch.set_property(alice, "age", 34)
    results = batch.submit()
    alice = results[batch.find(alice)]
    assert alice["age"] == 34
def getShortestPaths():
    batch = neo4j.WriteBatch(DB)
    
    # try and link user data from user->explore to user->trending
    queryString = """
    MATCH (a:FourSqrVenues_explore)<-->(b:FourSqrUsers) 
    MATCH  p = allShortestPaths((a)<-->(b))
    RETURN p
    limit 100
    """
    """
    START n = node:points_hk('bbox:[ 22.3, 115.0, 22.271, 114.18 ]') 
    WHERE (n:FourSquareVenues_explore) 
    RETURN n
    """
    """START n=node:points_hk('withinDistance:[ 22.35, 114.2, 15.0 ]')
    WHERE (n:FourSqrVenues_explore) return n"""
    
    queryString = '''MATCH (u:FourSqrVenues_explore) RETURN u.lat, u.lon''' 
    
    batch.append_cypher(queryString)
    results = batch.submit() 
    for i in results[0]:
        lat, lon = i[0], i[1]
        batch.append_cypher("START n=node:points_hk('withinDistance:[ %s, %s, 0.10 ]') WHERE (n:FourSqrVenues_explore) return n" %(lat, lon))
    new_results = batch.submit() 
    
    for i in new_results:
        if i:
            if isinstance(i, list):
                for new_i in i:
                    print u'%s' %(new_i.values[0])
            else: print i._id
            #print u'{0}'.format(i[0])
    '''
示例#19
0
def test_can_delete_properties_on_preexisting_node():
    graph_db = neo4j.GraphDatabaseService()
    alice, = graph_db.create({"name": "Alice", "age": 34})
    batch = neo4j.WriteBatch(graph_db)
    batch.delete_properties(alice)
    batch.run()
    props = alice.get_properties()
    assert props == {}
def test_can_delete_properties_on_node_in_same_batch(graph):
    batch = neo4j.WriteBatch(graph)
    alice = batch.create({"name": "Alice", "age": 34})
    batch.delete_properties(alice)
    results = batch.submit()
    alice = results[batch.find(alice)]
    props = alice.get_properties()
    assert props == {}
示例#21
0
def test_can_delete_property_on_node_in_same_batch(graph):
    batch = neo4j.WriteBatch(graph)
    alice = batch.create({"name": "Alice", "age": 34})
    batch.delete_property(alice, "age")
    results = batch.submit()
    alice = results[batch.find(alice)]
    assert alice["name"] == "Alice"
    assert alice["age"] is None
示例#22
0
def restoreWG(wgList):
    labels = ['Social', 'WG']
    batch = neo4j.WriteBatch(gdb)
    for wgName in wgList:
        wgNode = batch.create(node(identifier=wgName))
        batch.add_labels(wgNode, *labels)
    results = batch.submit()
    batch.clear()
示例#23
0
 def setUp(self):
     self.graph_db = default_graph_db()
     self.friendships = self.graph_db.get_or_create_index(
         neo4j.Relationship, "Friendships")
     self.alice, self.bob = self.graph_db.create({"name": "Alice"},
                                                 {"name": "Bob"})
     self.batch = neo4j.WriteBatch(self.graph_db)
     self.recycling = []
示例#24
0
def test_can_delete_property_on_preexisting_node():
    graph_db = neo4j.GraphDatabaseService()
    alice, = graph_db.create({"name": "Alice", "age": 34})
    batch = neo4j.WriteBatch(graph_db)
    batch.delete_property(alice, "age")
    batch.run()
    assert alice["name"] == "Alice"
    assert alice["age"] is None
def test_can_create_path_with_new_nodes(graph):
    batch = neo4j.WriteBatch(graph)
    batch.create_path({"name": "Alice"}, "KNOWS", {"name": "Bob"})
    results = batch.submit()
    path = results[0]
    assert len(path) == 1
    assert path.nodes[0]["name"] == "Alice"
    assert path.relationships[0].type == "KNOWS"
    assert path.nodes[1]["name"] == "Bob"
示例#26
0
def test_can_set_properties_on_node_in_same_batch():
    graph_db = neo4j.GraphDatabaseService()
    batch = neo4j.WriteBatch(graph_db)
    alice = batch.create({})
    batch.set_properties(alice, {"name": "Alice", "age": 34})
    results = batch.submit()
    alice = results[batch.find(alice)]
    assert alice["name"] == "Alice"
    assert alice["age"] == 34
示例#27
0
def test_can_add_labels_to_node_in_same_batch():
    graph_db = neo4j.GraphDatabaseService()
    batch = neo4j.WriteBatch(graph_db)
    alice = batch.create({"name": "Alice"})
    batch.add_labels(alice, "human", "female")
    batch.remove_label(alice, "female")
    results = batch.submit()
    alice = results[batch.find(alice)]
    assert alice.get_labels() == {"human"}
示例#28
0
def test_can_set_labels_on_preexisting_node(graph):
    if not graph.supports_node_labels:
        return
    alice, = graph.create({"name": "Alice"})
    alice.add_labels("human", "female")
    batch = neo4j.WriteBatch(graph)
    batch.set_labels(alice, "mystery", "badger")
    batch.run()
    assert alice.get_labels() == {"mystery", "badger"}
def test_can_create_path_with_existing_nodes(graph):
    alice, bob = graph.create({"name": "Alice"}, {"name": "Bob"})
    batch = neo4j.WriteBatch(graph)
    batch.create_path(alice, "KNOWS", bob)
    results = batch.submit()
    path = results[0]
    assert len(path) == 1
    assert path.nodes[0] == alice
    assert path.relationships[0].type == "KNOWS"
    assert path.nodes[1] == bob
示例#30
0
def test_can_use_return_values_as_references():
    batch = neo4j.WriteBatch(neo4j.GraphDatabaseService())
    a = batch.create(node(name="Alice"))
    b = batch.create(node(name="Bob"))
    batch.create(rel(a, "KNOWS", b))
    results = batch.submit()
    ab = results[2]
    assert isinstance(ab, neo4j.Relationship)
    assert ab.start_node["name"] == "Alice"
    assert ab.end_node["name"] == "Bob"