9.4.1 PGQL Based Subgraph Loading
You can use the PgViewSubgraphReader#fromPgView
API to
create an in-memory subgraph from a property graph view (PG View) 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.
Note:
- Only non-composite vertex and edge keys are supported.
- Only numeric edge keys are supported.
- PGQL queries with
GROUP BY
orORDER BY
clauses are not supported for loading of subgraphs from a property graph view.
The following example creates a subgraph from a PG View using multiple PGQL queries:
opg4j> var graph = session.readSubgraph().
...> fromPgView("FRIENDS").
...> queryPgql("MATCH (v1:Person)-[e:FRIENDOF]->(v2:Person) WHERE id(v1) = 'PERSONS(1)'").
...> queryPgql("MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'").
...> load()
graph ==> PgxGraph[name=FRIENDS,N=3,E=1,created=1646726883194]
PgxGraph graph = session.readSubgraph()
.fromPgView("FRIENDS")
.queryPgql("MATCH (v1:Person)-[e:FRIENDOF]->(v2:Person) WHERE id(v1) = 'PERSONS(1)'")
.queryPgql("MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'")
.load();
>>> graph = session.read_subgraph_from_pg_view("FRIENDS", ["MATCH (v1:Person)-[e:FRIENDOF]->(v2:Person) WHERE id(v1) = 'PERSONS(1)'",
... "MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'"])
>>> graph
PgxGraph(name: FRIENDS, v: 3, e: 1, directed: True, memory(Mb): 0)
The following displays the output for the preceding PGQL query using the graph visualization tool.
Loading Subgraphs with Custom Names
By default, the new subgraph gets created with the same name as the PG View graph. Alternatively, if you want to load a subgraph with a custom name, then you can configure the subgraph name as shown:
opg4j> var graph = session.readSubgraph().
...> fromPgView("FRIENDS").
...> queryPgql("MATCH (v1:Person)-[e:FRIENDOF]->(v2:Person) WHERE id(v1) = 'PERSONS(1)'").
...> queryPgql("MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'").
...> load("friends_network")
graph ==> PgxGraph[name=friends_network,N=3,E=1,created=1664458398090]
PgxGraph graph = session.readSubgraph()
.fromPgView("FRIENDS")
.queryPgql("MATCH (v1:Person)-[e:FRIENDOF]->(v2:Person) WHERE id(v1) = 'PERSONS(1)'")
.queryPgql("MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'")
.load("friends_network");
>>> graph = session.read_subgraph_from_pg_view("FRIENDS",
... ["MATCH (v1:Person)-[e:FRIENDOF]->(v2:Person) WHERE id(v1) = 'PERSONS(1)'",
... "MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'"],
... graph_name="friends_network")
>>> graph
PgxGraph(name: friends_network, v: 3, e: 1, directed: True, memory(Mb): 0)
Loading a Subgraph by Explicitly Specifying the Schema Name
If you want to load a subgraph by reading a PG View from
another schema, you can additionally provide the schema name as an
argument to the PgViewSubgraphReader#fromPgView
API
. You must also ensure that you have READ
permission on all the underlying metadata and data tables for the PG
View.
For example:
opg4j> var graph = session.readSubgraph()
...> .fromPgView("GRAPHUSER", "FRIENDS")
...> .queryPgql("MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'")
...> .load()
graph ==> PgxGraph[name=FRIENDS,N=1,E=0,created=1672743755511]
PgxGraph graph = session.readSubgraph()
.fromPgView("GRAPHUSER", "FRIENDS")
.queryPgql("MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'")
.load();
>>> graph = session.read_subgraph_from_pg_view("FRIENDS",
... ["MATCH (v:Person) WHERE id(v) = 'PERSONS(2)'"],
... schema="GRAPHUSER")
Parent topic: Loading a Subgraph from Property Graph Views