def update_relationship(self, document, root_type, doc_type, doc_id):
     builder = NodesAndRelationshipsBuilder(document, doc_type, doc_id,
                                            [root_type])
     builder.build_relationships_query(root_type, doc_type, doc_id, doc_id,
                                       document.get('_r_dir', 1))
     self.statements_with_params.append(builder.query_nodes)
     self.statements_with_params.append(builder.relationships_query)
    def iterate_chunks():
        more_chunks = True

        while more_chunks:
            tx = self.graph.cypher.begin()
            metadata = { "_ts": timestamp }
            for i in range(self.chunk_size):
                try:
                    doc = next(docs)
                    index, doc_type = self._index_and_mapping(namespace)
                    doc_id = u(doc.pop("_id"))
                    doc = self._formatter.format_document(doc)
                    builder = NodesAndRelationshipsBuilder(doc, doc_type, doc_id, metadata)
                    self.apply_id_constraint(builder.doc_types)
                    for statement in builder.query_nodes.keys():
                        tx.append(statement, builder.query_nodes[statement])
                    for relationship in builder.relationships_query.keys():
                        tx.append(relationship, builder.relationships_query[relationship])
                except StopIteration:
                    more_chunks = False
                    if i > 0:
                        yield tx
                    break
            if more_chunks:
                yield tx
    def upsert(self, doc, namespace, timestamp):
        """Inserts a document into Neo4j."""
        index, doc_type = self._index_and_mapping(namespace)
        doc_id = u(doc.pop("uid"))
        metadata = { "_ts": timestamp }
        doc = self._formatter.format_document(doc)
        builder = NodesAndRelationshipsBuilder(doc, doc_type, doc_id, metadata)
        self.apply_id_constraint(builder.doc_types)
        tx = self.graph.cypher.begin()
        for statement in builder.query_nodes.keys():
            tx.append(statement, builder.query_nodes[statement])
        for query in builder.cypher_list:
            tx.append(query)
            # Adding cyphers from cypher list
        for relationship, params in builder.relationships_query:
            tx.append(relationship, params)
        for statement in builder.statements_with_params:
            for key in statement.keys():
                tx.append(key, statement[key])
        commit_result = None
        try:
            commit_result = tx.commit()
            print commit_result
        except Exception as e:
            LOG.error('{}'.format(e.message))
            pass

        if commit_result:
            nodeids_list = self._get_nodeids(commit_result)
            self.create_geospatial_indices(nodeids_list)
 def bulk_upsert(self, docs, namespace, timestamp):
     """Insert multiple documents into Neo4j."""
     """Maximum chunk size is 1000. Transaction blocks won't have more than 1000 statements."""
     metadata = { "_ts": timestamp }
     tx = self.graph.cypher.begin()
     for doc in docs:
         index, doc_type = self._index_and_mapping(namespace)
         doc_id = u(doc.pop("uid"))
         doc = self._formatter.format_document(doc)
         builder = NodesAndRelationshipsBuilder(doc, doc_type, doc_id, metadata)
         self.apply_id_constraint(builder.doc_types)
         for statement in builder.query_nodes.keys():
             tx.append(statement, builder.query_nodes[statement])
         for query in builder.cypher_list:
             tx.append(query)
             # Adding cyphers from cypher list
         for relationship, params in builder.relationships_query:
             tx.append(relationship, params)
         for statement in builder.statements_with_params:
             for key in statement.keys():
                 tx.append(key, statement[key])
     try:
         tx.commit()
     except Exception as e:
         LOG.error('{}'.format(e.message))
         pass
Exemplo n.º 5
0
 def upsert(self, doc, namespace, timestamp):
   """Inserts a document into Neo4j."""
   index, doc_type = self._index_and_mapping(namespace)
   doc_id = u(doc.pop("_id"))
   metadata = { "_ts": timestamp }
   doc = self._formatter.format_document(doc)
   builder = NodesAndRelationshipsBuilder(doc, doc_type.replace("-","_"), doc_id, metadata)
   self.apply_id_constraint(builder.doc_types)
   tx = self.graph.begin()
   for statement in builder.query_nodes.keys():
     tx.run(statement, builder.query_nodes[statement])
   for relationship in builder.relationships_query.keys():
     tx.run(relationship, builder.relationships_query[relationship])
   tx.commit()
Exemplo n.º 6
0
 def bulk_upsert(self, docs, namespace, timestamp):
   """Insert multiple documents into Neo4j."""
   """Maximum chunk size is 1000. Transaction blocks won't have more than 1000 statements."""
   metadata = { "_ts": timestamp }
   tx = self.graph.begin()
   for doc in docs:
     index, doc_type = self._index_and_mapping(namespace)
     doc_id = u(doc.pop("_id"))
     doc = self._formatter.format_document(doc)
     builder = NodesAndRelationshipsBuilder(doc, doc_type.replace("-","_"), doc_id, metadata)
     self.apply_id_constraint(builder.doc_types)
     for statement in builder.query_nodes.keys():
       tx.run(statement, builder.query_nodes[statement])
     for relationship in builder.relationships_query.keys():
       tx.run(relationship, builder.relationships_query[relationship])
   tx.commit()
 def update_relationship(self, document, root_type, doc_type, doc_id):
   builder = NodesAndRelationshipsBuilder(document, doc_type, doc_id, [root_type])
   builder.build_relationships_query(root_type, doc_type, doc_id, doc_id, document.get('_r_dir', 1))
   self.statements_with_params.append(builder.query_nodes)
   self.statements_with_params.append(builder.relationships_query)