Loading a Subgraph from PGQL Property Graphs

You can create a subgraph from a PGQL property graph and load it into memory in the graph server. Instead of loading a full graph into memory, you can load a subgraph. This would consume less memory. The following sections explain in detail on loading and expanding of subgraphs:

PGQL Based Subgraph Loading

You can use the Subgraph Reader API to create an in-memory subgraph from a PGQL property graph using a set of PGQL queries. These PGQL queries define the vertices and edges that are to be loaded into the subgraph. You can also use multiple PGQL queries and the resulting output graph is a union of the subgraphs, each being loaded independently by each PGQL query.

1graph = session.read_subgraph_from_pg_pgql("FRIENDS", [
2   "MATCH (v1:Person)-[e:FRIENDOF]->(v2:Person) WHERE id(v1) = 'PERSONS(1)'",
3   "MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'"
4])

Prepared PGQL Queries

You can also use prepared PGQL queries when loading a subgraph from a PGQL property graph to be able to pass bind variables. The PreparedPgqlQuery and PreparedPgqlQueryStringArgument objects allow to set the bindings for the variables used for the loading of the subgraph.

1from pypgx.api import PreparedPgqlQuery
2from pypgx.api import PreparedPgqlQueryStringArgument
3
4graph = session.read_subgraph_from_pg_pgql("FRIENDS", [
5   PreparedPgqlQuery("MATCH (v1:Person)-[e:FriendOf]->(v2:Person) WHERE id(v2)=?", [PreparedPgqlQueryStringArgument("PERSONS(3)")])
6])

Providing Database Connection Credentials

You can specify the database connection credentials with the Subgraph Reader API instead of using the default credentials of the current user. The following example shows loading of a subgraph for non-default database connection settings:

1graph = session.read_subgraph_from_pg_pgql("FRIENDS", [
2   "MATCH (v1:Person)-[e:FRIENDOF]->(v2:Person) WHERE id(v1) = 'PERSONS(1)'",
3   "MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'"
4], username="graphuser", password="<password_for_graphuser>", keystore_alias="database1", schema="graphuser", jdbcUrl="jdbc:oracle:thin:@localhost:1521/orclpdb", connections=12)

Dynamically Expanding a Subgraph

You can expand an in-memory subgraph by loading another subgraph into memory and merging it with the current in-memory subgraph. The following applies when merging two graphs:

  • Both the graphs can have separate sets of providers.

  • A graph can have some providers same as the other graph.

    • The providers with the same names must have the same labels.

    • The graph being merged must have the same or a common subset of properties as the base graph. However, it is possible that either of the graphs may have more number of properties.

The following example shows the expansion of the subgraph created in PGQL Based Subgraph Loading:

1from pypgx.api import PreparedPgqlQuery
2from pypgx.api import PreparedPgqlQueryStringArgument
3
4graph = graph.expand_with_pgql([
5    "MATCH (v1:PERSON) -[e:FRIENDOF]-> (v2:PERSON) WHERE id(v1) = 'PERSONS(2)'",
6    PreparedPgqlQuery("MATCH (v:Person) WHERE id(v)=?", [PreparedPgqlQueryStringArgument("PERSONS(4)")])
7], pg_view_name="FRIENDS")