Loading Subgraphs

In this guide you will learn how to create a subgraph of a graph that is not already loaded into memory. First, we will recap about graph loading and subgraph filtering. Then we will show how to merge these two techniques into subgraph loading.

Warning

Subgraph loading is only available on the following graph formats:
  • Adjacency List

  • Oracle Property Graph loaded from Oracle RDBMS, Oracle NoSQL or Apache HBase

Recap: Graph loading and subgraph creation

Loading a graph

As previously explained you can load a graph like this:

1session = pypgx.get_session(session_name="my-session")
2graph = session.read_graph_with_properties(self.graph_path)

Creating a subgraph from a loaded graph

The loaded graph is based on the sample graph that is used throughout the project. To create a subgraph having only edges and the vertices connecting them, where the source vertex’s property prop equals to 10, you can use the following expression:

src.prop1 == 10

This expression selects the following edges:

pgx sample graph highlight 2

In PGX the subgraph is created by evaluating a filter on a loaded graph:

1subgraph = graph.filter(EdgeFilter("src.prop1 == 10"))

Combining loading and subgraph creation

Now assume that you are only interested in a subgraph from the beginning. In this case loading the whole graph and then creating a subgraph might not the best way: If the full graph is too big to fit into memory, loading the graph is not an option at all and even if the whole graph fits into memory, memory is wasted to hold both the original graph and the subgraph.

Therefore we provide a combined operation to load a subgraph based on a filter expression. Imagine you want to load a subgraph of the above graph based on the filter expression src.prop == 10. To do so you need to specify the filter in the graph-config like this:

{
  "uri": "sample.adj",
  "format": "adj_list",
  "vertex_props": [{
    "name": "prop",
    "type": "integer"
  }],
  "edge_props": [{
    "name": "cost",
    "type": "double"
  }],
  "separator": " ",
  "loading_options": {
    "filter": {
      "type": "edge",
      "expression": "src.prop == 10"
    }
  }
}

Notice the new section loading which contains the configuration for the filter.

If you load the graph using this configuration only the following subgraph will be loaded:

pgx sample graph subgraph 1