9.1.1 Retrieving Metadata for PGQL Property Graphs

You can retrieve the metadata of PGQL property graphs created in the database using the built-in PROPERTY_GRAPH_METADATA graph in your PGQL queries.

The PROPERTY_GRAPH_METADATA graph structure including properties is as shown:

Figure 9-1 PROPERTY_GRAPH_METADATA Graph Design

Description of Figure 9-1 follows
Description of "Figure 9-1 PROPERTY_GRAPH_METADATA Graph Design"

The following describes the preceding design of the metadata graph:

PROPERTY_GRAPH -[:HAS_VERTEX_TABLE]-> VERTEX_TABLE
               -[:HAS_EDGE_TABLE]-> EDGE_TABLE
 
VERTEX_TABLE -[:HAS_KEY_COLUMN]-> KEY_COLUMN
             -[:HAS_LABEL]-> LABEL
 
EDGE_TABLE -[:HAS_KEY_COLUMN]-> KEY_COLUMN
           -[:HAS_LABEL]-> LABEL
           -[:HAS_SOURCE_TABLE]-> VERTEX_TABLE
           -[:HAS_DESTINATION_TABLE]-> VERTEX_TABLE
 
LABEL -[:HAS_PROPERTY]-> PROPERTY

It is important to note the following when using PROPERTY_GRAPH_METADATA in PGQL queries:

  • The PROPERTY_GRAPH_METADATA graph is automatically created and updated the first time you attempt to access it in a PGQL query.
  • The PROPERTY_GRAPH_METADATA graph is similar to a PGQL property graph and has its own set of metadata tables that describe its structure. In addition to the metadata tables for PGQL property graphs that are described in Table 9-1, the graph data for PROPERTY_GRAPH_METADATA is also stored in database objects that are listed in the following table:

    Table 9-2 Additional Metadata Tables

    Table Name Description
    PROPERTY_GRAPH_METADATA_GRAPH_LIST$ Metadata table describing the list of PGQL property graphs to which the current user has access
    PROPERTY_GRAPH_METADATA_EDGE_KEY_COLUMNS$ Metadata table describing the edge key columns
    PROPERTY_GRAPH_METADATA_EDGE_LABELS$ Metadata table describing the edge labels
    PROPERTY_GRAPH_METADATA_EDGE_TABLES$ Metadata table describing the edge tables
    PROPERTY_GRAPH_METADATA_LABEL_PROPERTIES$ Metadata table describing the vertex and edge label properties
    PROPERTY_GRAPH_METADATA_LABELS$ Metadata table describing the vertex and edge labels
    PROPERTY_GRAPH_METADATA_VERTEX_KEY_COLUMNS$ Metadata table describing the vertex key columns
    PROPERTY_GRAPH_METADATA_VERTEX_LABELS$ Metadata table describing the vertex labels
    PROPERTY_GRAPH_METADATA_VERTEX_TABLES$ Metadata table describing the vertex tables

    Note:

    It is important that you do not alter or remove any of the metadata tables for the PROPERTY_GRAPH_METADATA graph.
  • When running PGQL queries using the Java API, you must disable autocommit on the JDBC connection (conn.setAutoCommit(false)). This ensures that PROPERTY_GRAPH_METADATA graph gets created automatically.

The following examples show using PROPERTY_GRAPH_METADATA in PGQL queries to retrieve the required metadata.

You can retrieve the list of graphs to which you have access as shown:

opg4j> String pgql =
...> "SELECT g.graph_name "
...> +"FROM MATCH (g:property_graph) ON property_graph_metadata "
...> +"ORDER BY g.graph_name"
pgql ==> "SELECT g.graph_name FROM MATCH (g:property_graph) ON property_graph_metadata ORDER BY g.graph_name"
opg4j> pgqlStmt.executeQuery(pgql).print()
String pgql = "SELECT g.graph_name "+
"FROM MATCH (g:property_graph) ON property_graph_metadata "+
"ORDER BY g.graph_name";
PgqlResultSet rs = pgqlStmt.executeQuery(pgql);
rs.print();
>>> pgql = '''
... SELECT g.graph_name
... FROM MATCH (g:property_graph) ON property_graph_metadata
... ORDER BY g.graph_name
... '''
>>> pgql_statement.execute_query(pgql).print()

On execution, the preceding query produces the following result:

+------------------------+
| GRAPH_NAME             |
+------------------------+
| BANK_GRAPH_VIEW        |
| FINANCIAL_TRANSACTIONS |
| FRIENDS                |
+------------------------+

You can retrieve the vertex properties of a graph as shown:

opg4j> String pgql =
...> "SELECT p.property_name "
...> +"FROM MATCH(g:property_graph)-[:has_vertex_table]->(v)-[:has_label]->(l:label)-[:has_property]->(p:property) "
...> +"ON property_graph_metadata "
...> +"WHERE g.graph_name = 'FRIENDS' "
pgql ==> "SELECT p.property_name FROM MATCH(g:property_graph)-[:has_vertex_table]->(v)-[:has_label]->(l:label)-[:has_property]->(p:property) ON property_graph_metadata WHERE g.graph_name = 'FRIENDS' "
opg4j> pgqlStmt.executeQuery(pgql).print()
String pgql = "SELECT p.property_name "+
"FROM MATCH(g:property_graph)-[:has_vertex_table]->(v)-[:has_label]->(l:label)-[:has_property]->(p:property) "+
"ON property_graph_metadata "+
"WHERE g.graph_name = 'FRIENDS' ";
PgqlResultSet rs = pgqlStmt.executeQuery(pgql);
rs.print();
>>> pgql = '''
... SELECT p.property_name
... FROM MATCH(g:property_graph)-[:has_vertex_table]->(v)-[:has_label]->(l:label)-[:has_property]->(p:property)
... ON property_graph_metadata
... WHERE g.graph_name = 'FRIENDS'
... '''
>>> pgql_statement.execute_query(pgql).print()

On execution, the preceding query produces the following result:

+---------------+
| PROPERTY_NAME |
+---------------+
| BIRTHDATE     |
| HEIGHT        |
| NAME          |
+---------------+