def populate_db(self): for d in self.data: d['Property']['IdUnique'] = d['IdUnique'] if d['Kind'] == 'node': if d['DeDuplication'] == None and self.g.nodes.match( IdUnique=d['IdUnique']).count() == 0: create_nodes(self.g.auto(), [d['Property']], labels={*d['Label']}) else: merge_nodes(self.g.auto(), [d['Property']], (tuple(d['Label']), 'IdUnique'), labels={*d['Label']}) else: da = ((d['FromIdMaster']), d['Property'], (d['ToIdMaster'])) if d['DeDuplication'] == None and self.g.relationships.match( IdUnique=d['IdUnique']).count() == 0: create_relationships(self.g.auto(), [da], d['Type'], start_node_key=(d['FromLabel'], 'IdMaster'), end_node_key=(d['ToLabel'], 'IdMaster')) else: merge_relationships(self.g.auto(), [da], (d['Type'], 'IdUnique'), start_node_key=(d['FromLabel'], 'IdMaster'), end_node_key=(d['ToLabel'], 'IdMaster'))
def test_merge_nodes_from_property_dicts(graph): graph.delete_all() with graph.begin() as tx: merge_nodes(tx, DATA_DICTS, ("Person", "name"), labels={"Person", "Employee"}) matched = graph.nodes.match("Person") assert matched.count() == 3 assert all(node.labels == {"Person", "Employee"} for node in matched)
def test_merge_nodes_from_property_lists(graph): graph.delete_all() tx = graph.begin() merge_nodes(tx, DATA_LISTS, ("Person", "name"), labels={"Person", "Employee"}, keys=HEADERS) graph.commit(tx) matched = graph.nodes.match("Person") assert matched.count() == 3 assert all(node.labels == {"Person", "Employee"} for node in matched)
def test_merge_nodes_from_property_lists(graph): graph.delete_all() tx = graph.begin() tx.update("CREATE (a:Person {name:'Alice', age:99})") merge_nodes(tx, DATA_LISTS, ("Person", "name"), labels={"Person", "Employee"}, keys=HEADERS) graph.commit(tx) matched = graph.nodes.match("Person") assert matched.count() == 3 assert all(node.labels == {"Person", "Employee"} for node in matched) assert graph.nodes.match("Person", name="Alice").first()["age"] == 66
def merge_nodes(self, g, data, merge_key=None, keys=None): """ :param tx: Transaction in which to carry out this operation :param data: node data supplied as a list of lists (if keys are provided) or a list of dictionaries (if keys is None :param merge_key: tuple of (label, key1, key2…) on which to merge :param labels: additional labels to apply to the merged nodes :param keys: optional set of keys for the supplied data (if supplied as value lists) """ merge_key = merge_key or self.merge_key keys = keys or list(self.fields.keys()) if len(data) > self.LIMIT: for small_data in self.split_data(data): merge_nodes(g.auto(), small_data, merge_key, keys=keys) else: merge_nodes(g.auto(), data, merge_key, keys=keys)
def merge(self, graph, merge_properties=None, batch_size=None, preserve=None, append_props=None): """ Merge nodes from NodeSet on merge properties. :param merge_properties: The merge properties. """ # overwrite if preserve is passed if preserve: self.preserve = preserve # overwrite if array_props is passed if append_props: self.append_props = append_props log.debug('Merge NodeSet on {}'.format(merge_properties)) if not batch_size: batch_size = self.batch_size if not merge_properties: merge_properties = self.merge_keys log.debug('Batch Size: {}'.format(batch_size)) # use py2neo base functions if no properties are preserved if not self.preserve and not self.append_props: for batch in chunks(self.node_properties(), size=batch_size): merge_nodes(graph, batch, (tuple(self.labels), *merge_properties)) elif self.preserve and not self.append_props: q = nodes_merge_unwind_preserve(self.labels, self.merge_keys, property_parameter='props') for batch in chunks(self.node_properties(), size=batch_size): graph.run(q, props=list(batch), preserve=self.preserve) elif not self.preserve and self.append_props: q = nodes_merge_unwind_array_props(self.labels, self.merge_keys, self.append_props, property_parameter='props') for batch in chunks(self.node_properties(), size=batch_size): graph.run(q, props=list(batch), append_props=self.append_props) elif self.preserve and self.append_props: q = nodes_merge_unwind_preserve_array_props(self.labels, self.merge_keys, self.append_props, self.preserve, property_parameter='props') print(q) for batch in chunks(self.node_properties(), size=batch_size): graph.run(q, props=list(batch), append_props=self.append_props, preserve=self.preserve)
def merge_nodes_from_data_frame(tx, df, merge_key, labels=None, preserve=None): """ Merge nodes from a DataFrame. This function wraps the :func:`py2neo.bulk.merge_nodes` function, allowing a `DataFrame <https://pandas.pydata.org/pandas-docs/stable/dsintro.html#series>`__ object to be passed in place of the regular `data` argument. :param tx: :param df: :param merge_key: :param labels: :param preserve: :return: """ merge_nodes(tx, df.itertuples(index=False, name=None), merge_key, labels=labels, keys=df.keys(), preserve=preserve)