Neo4j Integration

These functions abstract complexity of interacting with Neo4j. Instead of writing Cypher queries, you can use Python objects to create, merge and match nodes and relationships.

data2neo.neo4j.create(graph: Subgraph, session: Session)[source]

Creates a graph in the database.

Parameters
  • graph (Subgraph) – The graph to create.

  • session (Session) – The session to use.

data2neo.neo4j.merge(graph: Subgraph, session: Session, primary_label=None, primary_key=None)[source]

Merges a graph into the database.

Parameters
  • graph (Subgraph) – The graph to merge.

  • session (Session) –

    The session to use.

  • primary_label (str) – The primary label to merge on. Has to be provided if the nodes themselves don’t have a primary label (Default: None)

  • primary_key (str) – The primary key to merge on. Has to be provided if the graph elements themselves don’t have a primary label (Default: None)

data2neo.neo4j.push(graph: Subgraph, session: Session)[source]

Updates local graph elements with the database. The graph needs to be already in the database.

Parameters
  • graph (Subgraph) – The graph to create.

  • session (Session) –

    The session to use.

data2neo.neo4j.pull(graph: Subgraph, session: Session)[source]

Pulls remote changes to the graph to the local copy. The graph needs to be already in the database.

Parameters
  • graph (Subgraph) – The graph to create.

  • session (Session) –

    The session to use.

data2neo.neo4j.match_nodes(session: Session, *labels: List[str], **properties: dict)[source]

Matches nodes in the database.

Parameters
  • labels (List[str]) – The labels to match.

  • session (Session) –

    The session to use.

  • properties (dict) – The properties to match.

data2neo.neo4j.match_relationships(session: Session, from_node: Optional[Node] = None, to_node: Optional[Node] = None, rel_type: Optional[str] = None, **properties: dict)[source]

Matches relationships in the database.

Parameters
  • session (Session) –

    The session to use.

  • from_node (Node) – The node to match the relationship from (Default: None)

  • to_node (Node) – The node to match the relationship to (Default: None)

  • rel_type (str) – The type of the relationship to match (Default: None)

  • properties (dict) – The properties to match.

Subgraph

class data2neo.neo4j.Subgraph[source]

Bases: GraphElement

A Subgraph is an arbitrary collection of nodes and relationships. It is also the base class for Node, Relationship and Path.

By definition, a subgraph must contain at least one node; null subgraphs should be represented by None. To test for emptiness the built-in bool() function can be used.

The simplest way to construct a subgraph is by combining nodes and relationships using standard set operations. For example:

>>> s = ab | ac
>>> s
{(alice:Person {name:"Alice"}),
 (bob:Person {name:"Bob"}),
 (carol:Person {name:"Carol"}),
 (Alice)-[:KNOWS]->(Bob),
 (Alice)-[:WORKS_WITH]->(Carol)}
>>> s.nodes()
frozenset({(alice:Person {name:"Alice"}),
           (bob:Person {name:"Bob"}),
           (carol:Person {name:"Carol"})})
>>> s.relationships()
frozenset({(Alice)-[:KNOWS]->(Bob),
           (Alice)-[:WORKS_WITH]->(Carol)})
subgraph | other | ...

Union. Return a new subgraph containing all nodes and relationships from subgraph as well as all those from other. Any entities common to both will only be included once.

subgraph & other & ...

Intersection. Return a new subgraph containing all nodes and relationships common to both subgraph and other.

subgraph - other - ...

Difference. Return a new subgraph containing all nodes and relationships that exist in subgraph but do not exist in other, as well as all nodes that are connected by the relationships in subgraph regardless of whether or not they exist in other.

subgraph ^ other ^ ...

Symmetric difference. Return a new subgraph containing all nodes and relationships that exist in subgraph or other, but not in both, as well as all nodes that are connected by those relationships regardless of whether or not they are common to subgraph and other.

__init__(nodes=None, relationships=None)[source]
property nodes

The set of all nodes in this subgraph.

property relationships

The set of all relationships in this subgraph.

Node

class data2neo.neo4j.Node[source]

Bases: PropertyDict, Subgraph

A node is a fundamental unit of data storage within a property graph that may optionally be connected, via relationships, to other nodes.

It possible to combine nodes (along with relationships and other graph data objects) into Subgraph objects using set operations. For more details, look at the documentation for the Subgraph class.

All positional arguments passed to the constructor are interpreted as labels and all keyword arguments as properties:

>>> from data2neo.neo4j import Node
>>> a = Node("Person", name="Alice")
static from_attributes(labels: List[Attribute], attributes: List[Attribute] = [], primary_key: Optional[str] = None, primary_label: Optional[str] = None)[source]

Creates a Node from a list of attributes and labels

Parameters
  • labels – List of static attributes specifying the labels of the Node (first label will be the primary label)

  • attributes – List of attributes (only one can be primary)

  • primary_key – Optional key of the primary attribute. Used to merge the Node with existing nodes in the graph (default: None)

  • primary_label – Optional label of the primary attribute. Used to merge the Node with existing nodes in the graph (default: None)

static from_dict(labels: List[str], properties: dict, primary_key: Optional[str] = None, primary_label: Optional[str] = None, identity: Optional[str] = None)[source]

Creates a Node from a list of attributes and labels

Parameters
  • labels – List of static attributes specifying the labels of the Node (first label will be the primary label)

  • properties – Dictionary of attributes (only one can be primary)

  • primary_key – Optional key of the primary attribute. Used to merge the Node with existing nodes in the graph (default: None)

  • primary_label – Optional label of the primary attribute. Used to merge the Node with existing nodes in the graph (default: None)

__init__(*labels: str, **attributes: str) None[source]

Inits a Node with labels and attributes

Parameters
  • labels – List of static attributes specifying the labels of the Node (first label will be the primary label)

  • attributes – Key value pairs of attributes for the Node

set_primary_label(label)[source]

Sets the primary label of the node

Parameters

label – Label of the primary attribute. Used to merge the Node with existing nodes in the graph

property identity

Identity of element

keys()

Returns properties keys

property nodes

The set of all nodes in this subgraph.

property relationships

The set of all relationships in this subgraph.

set_primary_key(key)

Sets the primary key of the element

update(properties)

Updates the properties of the element

Relationship

class data2neo.neo4j.Relationship[source]

Bases: PropertyDict, Subgraph

static from_attributes(start_node: Node, relationship_type: Attribute, end_node: Node, attributes: List[Attribute] = [], primary_key: Optional[str] = None)[source]

Creates a Relationship from a list of attributes and labels

Parameters
  • start_node – Origin of the relationship

  • relationship_type – Type of the relationship

  • end_node – Destination of the relationship

  • attributes – List of attributes for the relationship

  • primary_key – Optional key of the primary attribute. Used to merge the Relationship with existing relationships in the graph (default: None)

static from_dict(start_node: Node, end_node: Node, relationship_type: str, properties: dict, primary_key: Optional[str] = None, identity: Optional[str] = None)[source]

Creates a Relationship from a list of attributes and labels

Parameters
  • start_node – Origin of the relationship

  • end_node – Destination of the relationship

  • relationship_type – Type of the relationship

  • properties – Dictionary of attributes for the relationship

  • primary_key – Optional key of the primary attribute. Used to merge the relationship with existing relations in the graph (default: None)

__init__(start_node: Node, relationship_type: str, end_node: Node, **attributes) None[source]

A relationship represents a typed connection between a pair of nodes.

The positional arguments passed to the constructor identify the nodes to relate and the type of the relationship. Keyword arguments describe the properties of the relationship:

>>> from py2neo import Node, Relationship
>>> a = Node("Person", name="Alice")
>>> b = Node("Person", name="Bob")
>>> a_knows_b = Relationship(a, "KNOWS", b, since=1999)
Parameters
  • start_node – Origin of the relationship

  • end_node – Destination of the relationship

  • relationship_type – Type of the relationship

  • attributes – Key value pairs of attributes for the Relationship

property start_node

Start node of the relationship

property end_node

End node of the relationship

property type

Type of the relationship

property identity

Identity of element

keys()

Returns properties keys

property nodes

The set of all nodes in this subgraph.

property relationships

The set of all relationships in this subgraph.

set_primary_key(key)

Sets the primary key of the element

update(properties)

Updates the properties of the element