Read from a graph
You can read data from a graph in several ways.
Example Neo4j graph (same example from Create a graph)
gds.run_cypher(
"""
CREATE
(m: City {name: "Malmö", population: 360000}),
(l: City {name: "London", population: 8800000}),
(s: City {name: "San Mateo", population: 105000}),
(m)-[:FLY_TO {cost: 200}]->(l),
(l)-[:FLY_TO {cost: 200}]->(m),
(l)-[:FLY_TO {cost: 1000}]->(s),
(s)-[:FLY_TO {cost: 1000}]->(l)
"""
)
It can be projected into the graph catalog as follows:
G, result = gds.graph.project.native(
graph_name="offices", # Graph name
node_projection=["City"], # Node projection
relationship_projection=["FLY_TO"], # Relationship projection
node_properties=["population"], # Node properties
relationship_properties=["cost"] # Relationship properties
)
G, result = gds.graph.project.native(
graph_name="offices", # Graph name
node_label_filter=["City"], # Node labels to include
relationship_type_filter=["FLY_TO"], # Relationship types to include
node_properties=["population"], # Node properties
relationship_properties=["cost"] # Relationship properties
)
Read node properties
The methods to read node and relationship properties from a graph are the same as in the Cypher API (see Streaming nodes).
Assuming the GraphDataScience object is called gds:
-
gds.graph.node_property.stream(previouslygds.graph.nodeProperty.stream) returns a single node property -
gds.graph.node_properties.stream(previouslygds.graph.nodeProperties.stream) returns multiple node properties
gds.graph.node_property.stream(G, node_property="population")
| nodeId | propertyValue |
|---|---|
3 |
360000 |
4 |
8800000 |
5 |
105000 |
gds.graph.node_properties.stream(G, node_properties=["population"])
| nodeId | population |
|---|---|
3 |
360000 |
4 |
8800000 |
5 |
105000 |
Separate node properties in resultRemoved in 2.0
Before version 2.0, the node_properties.stream and relationship_properties.stream methods included an additional separate_property_columns keyword parameter (default True) to return a Pandas DataFrame in which each property had its own column.
Starting with version 2.0, this behaviour is the default and the parameter has been removed.
Include node properties from Neo4j
Node properties such as names and descriptions are useful to understand the output of an algorithm, even if not needed to run the algorithm itself.
When a graph is projected from a Neo4j database, the db_node_properties client-only parameter for the node_property.stream method adds this information to the result.
In the following example, the stream method retrieves the values of the database-only name property alongside the values of the projected population property.
gds.graph.node_properties.stream(G, node_properties=["population"], db_node_properties=["name"])
| nodeId | population | name |
|---|---|---|
3 |
360000 |
Malmö |
4 |
8800000 |
London |
5 |
105000 |
San Mateo |
Read relationship properties
The methods to read node and relationship properties from a graph are the same as in the Cypher API (see Streaming relationships).
Assuming the GraphDataScience object is called gds:
-
gds.graph.relationship_property.stream(previouslygds.graph.relationshipProperty.stream) returns a single relationship property -
gds.graph.relationship_properties.stream(previouslygds.graph.relationshipProperties.stream) returns multiple relationship properties
gds.graph.relationship_property.stream(G, relationship_property="cost")
| sourceNodeId | targetNodeId | relationshipType | propertyValue |
|---|---|---|---|
3 |
4 |
FLY_TO |
200.0 |
4 |
3 |
FLY_TO |
200.0 |
4 |
5 |
FLY_TO |
1000.0 |
5 |
4 |
FLY_TO |
1000.0 |
gds.graph.relationship_properties.stream(G, relationship_properties=["cost"])
| sourceNodeId | targetNodeId | relationshipType | cost |
|---|---|---|---|
3 |
4 |
FLY_TO |
200.0 |
4 |
3 |
FLY_TO |
200.0 |
4 |
5 |
FLY_TO |
1000.0 |
5 |
4 |
FLY_TO |
1000.0 |
Read the graph topology
When reading the graph topology via the client equivalent to gds.graph.relationships.stream, the return type is RelationshipsDataFrame (a subtype of DataFrame).
The result is a node-to-node table with a row for each connected pair of nodes.
gds.graph.relationships.stream(G)
| sourceNodeId | targetNodeId | relationshipType |
|---|---|---|
3 |
4 |
FLY_TO |
4 |
3 |
FLY_TO |
4 |
5 |
FLY_TO |
5 |
4 |
FLY_TO |
The RelationshipsDataFrame class includes a by_rel_type method to return a map between relationship types and lists of source node IDs and target node IDs respectively.
gds.graph.relationships.stream(G).by_rel_type()
{'FLY_TO': [[3, 4, 4, 5], [4, 3, 5, 4]]}
Apache Arrow speedup
All the streaming methods can run faster when the Apache Arrow Flight Server is enabled for GDS.
The server is available in GDS Enterprise and is enabled by default on Aura Graph Analytics sessions and AuraDS.