11 Property Graph Views on RDF Graphs

Oracle Graph supports the property graph data model in addition to the RDF graph data model.

The property graph data model is simpler than the RDF data model in that it has no concept of global resource identification (that is, no URIs) or formal semantics and inference. In addition, property graphs allow direct association of properties (key-value pairs) with edges. RDF, by contrast, needs reification or a quad data model to associate properties with edges (RDF triples).

The property graph feature (see Introduction to Property Graphs in Oracle Database Graph Developer's Guide for Property Graph) of Oracle Database supports analytics capabilities with nearly 60 pre-built algorithms. You can avail this feature with RDF graphs by creating property graph views on the RDF graphs.

The CREATE PROPERTY GRAPH DDL statement supported by the Property Graph Query Language (PGQL) (see Creating a Property Graph Using PGQL) creates a property graph using relational data from the database. The vertices and the edges of the property graph are derived from the vertex and edge tables provided in the DDL statement. So you can run SEM_MATCH queries on your RDF data to create the database views that represent the the vertex and edge tables.

Also, note the following:

  • The vertex and edge views need a primary key column and attributes. If your SPARQL pattern uses a multi-valued property, then you may have repeated rows with the same primary key (usually a repeated subject in a vertex table). For such properties, you need to make them edges or use some aggregate like JSON_ARRAYAGG to collapse the multi-valued property into a single row.
  • The graph server (PGX) which runs the graph algorithms cannot handle composite primary keys. Therefore, you need to build a single key column for edge tables instead of simply using (sourceId, destinationId) as key.

Example 11-1 Create a PGQL Property Graph from RDF Data

Prerequistes: The following example uses the Moviestream RDF data and assumes that this data is loaded into an RDF graph called MOVIESTREAM in a network named RDF_NETWORK owned by RDFUSER. See Bulk Loading RDF Data Using SQL Developer for using SQL Developer to bulk load RDF data.

Perform the following steps to create a PGQL property graph using RDF data:

  1. Run SEM_MATCH queries to create views to represent the vertex and edge tables.
    The following example code generates the database views corresponding to the vertex and edge tables as shown:
    • Vertex Tables: MOVIE, GENRE
    • Edge Table: HAS_GENRE
    /* Vertex Table: Movie
    http://www.example.com/moviestream/Movie
      - http://www.example.com/moviestream/title
      - http://www.example.com/moviestream/sku
      - http://www.example.com/moviestream/year
      - http://www.example.com/moviestream/views
      - http://www.example.com/moviestream/summary
      - http://www.example.com/moviestream/runtimeInMin
      - http://www.example.com/moviestream/grossInUSD
      - http://www.example.com/moviestream/budgetInUSD
      - http://www.example.com/moviestream/openingDate
    */
    
    create or replace view movie(id, title, summary, year, openingDate, runtimeinMin, grossInUSD, budgetInUSD, views) as
    select movie$rdfvid id,
           title,
           summary,
           cast(year as number default null on conversion error) year,
           to_timestamp(openingDate default null on conversion error, 'SYYYY-MM-DD') openingDate,
           cast(runtimeInMin as number default null on conversion error) runtimeinMin,
           cast(grossInUSD as number default null on conversion error) grossInUSD,
           cast(budgetInUSD as number default null on conversion error) budgetInUSD,
           cast(views as number default null on conversion error) views
    from table(sem_match(
    'PREFIX ms: <http://www.example.com/moviestream/>
     SELECT *
     WHERE {
       ?movie ms:title ?title .
       OPTIONAL { ?movie ms:summary ?summary }
       OPTIONAL { ?movie ms:sku ?sku }
       OPTIONAL { ?movie ms:year ?year }
       OPTIONAL { ?movie ms:openingDate ?openingDate }
       OPTIONAL { ?movie ms:runtimeInMin ?runtimeInMin }
       OPTIONAL { ?movie ms:grossInUSD ?grossInUSD }
       OPTIONAL { ?movie ms:budgetInUSD ?budgetInUSD }
       OPTIONAL { ?movie ms:views ?views }
     }',
    sem_models('moviestream'),
    null,null,null,null,
    ' DO_UNESCAPE=T ',
    null,null,
    'RDFUSER','RDF_NETWORK'));
    
    /* Vertex Table: Genre
     
    http://www.example.com/moviestream/Genre
      - http://www.example.com/moviestream/genreName
    */
    create or replace view genre(id, genreName) as
    select genre$rdfvid id, genreName
    from table(sem_match(
    'PREFIX ms: <http://www.example.com/moviestream/>
     SELECT ?genre ?genreName
     WHERE {
       ?genre ms:genreName ?genreName . }',
    sem_models('moviestream'),
    null,null,null,null,
    ' DO_UNESCAPE=T ',
    null,null,
    'RDFUSER','RDF_NETWORK'));
    
    /*
    Edge Table: has_genre
    (:Movie) -[http://www.example.com/moviestream/genre]-> (:Genre)
    */
    create or replace view has_genre(id, movieId, genreId) as
    select (to_char(movie$rdfvid)||to_char(genre$rdfvid)) as id, movie$rdfvid movieId, genre$rdfvid genreId
    from table(sem_match(
    'PREFIX ms: <http://www.example.com/moviestream/>
     SELECT *
     WHERE {
       ?movie ms:genre ?genre .
     }',
    sem_models('moviestream'),
    null,null,null,null,
    ' DO_UNESCAPE=T ',
    null,null,
    'RDFUSER','RDF_NETWORK'));
  2. Create a PGQL property graph using the views.

    You can create PGQL property graphs using either the Graph Clients that are shipped with the Graph server and Client Release or using SQL Client Tools (SQLcl or SQL Developer).

    The following example creates the MOVIES property graph using the PGQL Worksheet in SQL Developer.

    CREATE PROPERTY GRAPH MOVIES
    VERTEX TABLES (
      MOVIE KEY(ID) LABEL MOVIE PROPERTIES ARE ALL COLUMNS,
      GENRE KEY(ID) LABEL GENRE PROPERTIES ARE ALL COLUMNS
    )
    EDGE TABLES (
      HAS_GENRE KEY(ID)
        SOURCE KEY (MOVIEID) REFERENCES MOVIE(ID)
        DESTINATION KEY (GENREID) REFERENCES GENRE(ID)
        LABEL HAS_GENRE PROPERTIES ARE ALL COLUMNS
     ) OPTIONS (PG_PGQL)

You can now query, visualize, and run graph algorithms on the property graph.

Using the Graph Server (PGX) to Run Graph Algorithms on RDF Graph and RDF Data Visualization

The graph server (PGX) of Oracle Graph allows you to run graph algorithms on property graphs. Hence, you can load the property graph, which is created using the RDF data in the views (as explained in Example 11-1), into the graph server (PGX) and run graph analytics. In addition, you can also visualize the RDF data using the Graph Visualization web client. Note that you must install the graph server (PGX) for performing these operations.

See Also:

Example 11-2 Running Graph Algorithms on RDF Graphs and RDF Data Visualization

Prerequisites: Ensure that you meet the following prerequisites for running this example:
  1. Create a PGQL property graph by creating database views on RDF data (see Example 11-1).
  2. As a SYSDBA user grant the GRAPH_DEVELOPER role to RDFUSER.

The following example uses the Java client to load the property graph into the graph server (PGX) and then runs the PageRank algorithm to list the top 10 movies.

For an introduction type: /help intro
Oracle Graph Server Shell 24.2.0
Variables instance, session, and analyst ready to use.
opg4j> var graph = session.readGraphByName("MOVIES", GraphSource.PG_PGQL,ReadGraphOption.onMissingVertex(OnMissingVertex.IGNORE_EDGE_LOG_ONCE))
graph ==> PgxGraph[name=MOVIES,N=3823,E=7617,created=1714231298337]
opg4j> analyst.pagerank(graph)
$3 ==> VertexProperty[name=pagerank,type=double,graph=MOVIES]
opg4j> session.queryPgql("SELECT a.title, a.pagerank FROM MATCH (a:movie) ON MOVIES ORDER BY a.pagerank DESC LIMIT 10").print()
+-------------------------------------------------------+
| title                         | pagerank              |
+-------------------------------------------------------+
| Gang Cops                     | 3.9236201935652636E-5 |
| Blood Street                  | 3.9236201935652636E-5 |
| The Girl on the Train         | 3.9236201935652636E-5 |
| The Girl with the Hungry Eyes | 3.9236201935652636E-5 |
| Debonair Dancers              | 3.9236201935652636E-5 |
| Honky                         | 3.9236201935652636E-5 |
| Edith's Shopping Bag          | 3.9236201935652636E-5 |
| Believe                       | 3.9236201935652636E-5 |
| Taking Liberties              | 3.9236201935652636E-5 |
| Batman Fights Dracula         | 3.9236201935652636E-5 |
+-------------------------------------------------------+
$5 ==> PgqlResultSetImpl[graph=MOVIES,numResults=10]

In addition, you can publish the graph (opg4j> graph.publish()) in the graph server (PGX) and run PGQL queries Using the Graph Visualization Application as shown in the following figure. The example visualization shows all the movies that belong to Sci-Fi genre.

Figure 11-1 RDF Data Visualization