11 Creating Property Graphs from 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 over 80 pre-built algorithms. You can avail this feature with RDF graphs by creating property graphs on the RDF graphs.

Using the CREATE PROPERTY GRAPH DDL statement supported by SQL (see SQL Property Graphs) and the Property Graph Query Language (PGQL) (see Creating a Property Graph Using PGQL), you can create a SQL or PGQL property graph, respectively, with relational data from the database. The vertices and edges of the property graph are derived from the vertex and edge tables provided in the DDL statement. Therefore, you can run SEM_MATCH queries on your RDF data to create database views or tables to represent vertex and edge tables for property graph creation.

Also, note the following:

  • The vertex and edge tables need a primary key 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 Creating a SQL or PGQL Property Graph from RDF Data

Prerequisites: 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 SQL or PGQL property graph using RDF data:

  1. Run SEM_MATCH queries to create the vertex and edge tables.

    It is recommended that you create indexes on these tables for better SQL property graph query performance. If creating a PGQL property graph, either views or tables can be created for the vertices and edges. The following example code generates the database tables 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 table 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 table 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 table has_genre(has_genre_id, movieId, genreId) as
    select (to_char(movie$rdfvid)||to_char(genre$rdfvid)) as has_genre_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 SQL or PGQL property graph using the tables.

    To create a SQL Property Graph:

    The following example creates the MOVIES SQL property graph. You can use one of the SQL Client Tools to create a SQL property graph.

    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(HAS_GENRE_ID)
        SOURCE KEY (MOVIEID) REFERENCES MOVIE(ID)
        DESTINATION KEY (GENREID) REFERENCES GENRE(ID)
        LABEL HAS_GENRE PROPERTIES ARE ALL COLUMNS
     )

    To create a PGQL Property Graph:

    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(HAS_GENRE_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 SQL 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 SQL 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 25.3.0
Variables instance, session, and analyst ready to use.
opg4j> var graph = session.readGraphByName("MOVIES", GraphSource.PG_SQL,ReadGraphOption.onMissingVertex(OnMissingVertex.IGNORE_EDGE_LOG_ONCE))
graph ==> PgxGraph[name=MOVIES,N=3823,E=7617,created=1751789231753]
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              |
+--------------------------------------------------+
| Mortuary                 | 3.9236201935652636E-5 |
| Karla                    | 3.9236201935652636E-5 |
| Thor: The Dark World     | 3.9236201935652636E-5 |
| August: Osage County     | 3.9236201935652636E-5 |
| Schoolgirl Apocalypse    | 3.9236201935652636E-5 |
| Talk Radio               | 3.9236201935652636E-5 |
| Frozen River             | 3.9236201935652636E-5 |
| Raising Buchanan         | 3.9236201935652636E-5 |
| The Stand Up             | 3.9236201935652636E-5 |
| Chronically Metropolitan | 3.9236201935652636E-5 |
+--------------------------------------------------+
$3 ==> PgqlResultSetImpl[graph=MOVIES,numResults=10]

Alternatively, Using the Graph Visualization Application, you can load the SQL property graph into the graph server (PGX) and run PGQL queries 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