X

Best Practices from Oracle Development's A‑Team

RDF model from RDF view - Spatial and Graph part 2

Articles in this series:

https://www.ateam-oracle.com/rdf-view-from-relational-spatial-and-graph-part-1

https://www.ateam-oracle.com/rdf-model-from-rdf-view-spatial-and-graph-part-2
https://www.ateam-oracle.com/property-graph-from-relational-spatial-and-graph-part-3
https://www.ateam-oracle.com/visualize-property-graph-with-cytoscape-spatial-and-graph-part-4
https://www.ateam-oracle.com/oracle-20c-pgx-and-graphviz-spatial-and-graph-part-5

 

 

Introduction

This article will show how to take an RDF view and convert it to a regular RDF model.

Prerequisites

This document describes steps with Oracle Enterprise Edition 18.3.0.0.0 on Oracle Linux 7.7

If you are using any other version of the DB, the steps may be different. It is assumed you have already followed the prerequisites in the first article of this series as they apply to the steps in this one as well.

Create RDF staging table

We can take the data from the views and dump it an actual RDF table. You copy it to an RDF staging table first, then create an RDF model from the staging table.

As the nwuser in nwpdb, execute the following sql:

CREATE TABLE NW_RDF_STAGE (
                     RDF$STC_sub varchar2(4000) not null,
                     RDF$STC_pred varchar2(4000) not null,
                     RDF$STC_obj varchar2(4000) not null,
                     RDF$STC_graph varchar2(4000)
);

This creates a temporary table that will hold data from the RDF view.

Dump the RDF view to the staging table

Dump the view data to stage table – this may take a few minutes depending on the volume of data. This command takes the contents of the NW_RDFVIEW and exports it into the NW_RDF_STAGE table we created.

BEGIN
  sem_apis.export_rdfview_model(
    model_name => 'NW_RDFVIEW',
    rdf_table_owner => 'NWUSER',
    rdf_table_name => 'NW_RDF_STAGE'
  );
END;
/

Give mdsys select access

grant SELECT on NW_RDF_STAGE to MDSYS;

This is required for functions to work properly.

Bulk load the staging table data into a regular model

Using sqldeveloper for this step is easier.
In sqldeveloper, go to rdfnetwork. right click on regular models. select bulk load into model from staging table.

Enter NW_MODEL as the model name. Click apply

SQL steps

Here are the steps with SQL if you do not use SQLDeveloper

Create SEM table

Create a table to hold references to semantic technology data for this model. You need to also give insert permissions to MDSYS.

CREATE TABLE NW_MODEL_TPL (TRIPLE SDO_RDF_TRIPLE_S);

GRANT INSERT on  NW_MODEL_TPL to MDSYS;

Create the model

Create the regular model we will load our data in to. The model is NW_MODEL, and it references the table NW_MODEL_TPL we just created. The column name if name of the column of type SDO_RDF_TRIPLE_S.

BEGIN                    
SEM_APIS.CREATE_SEM_MODEL(
     model_name  => 'NW_MODEL',
     table_name  => 'NW_MODEL_TPL',
     column_name => 'TRIPLE'); 

END;
/

Bulk load data

Load the data from the staging table into the regular model.

BEGIN
SEM_APIS.BULK_LOAD_FROM_STAGING_TABLE(
     model_name    => 'NW_MODEL',
     table_owner   => 'NWUSER',
     table_name    => 'NW_RDF_STAGE',
     flags         => '  PARSE ',
     debug         =>  null,
     start_comment => null,
     end_comment   => null);
END;
/

You should now have a real RDF model named NW_MODEL. This should perform significantly faster than the RDF view, and let you do more with the data.

Dump your RDF triples from NW_MODEL to a file

If you want to dump the triples from the model you just created to a file, here is the command. This data can be useful to feed to other tools for visualization or reformatting. You can also dump quads with this command, but I left the graph name out of the output since I am not using that field right now. Just add g$rdfterm into the select if you want them.

sqlplus nwuser@nwpdb
set linesize 2000;
set long 2000;
set longchunksize 2000;
set echo off; 
set feedback off; 
set heading off;
SET TRIMSPOOL ON;
set trimout on;
SET PAGESIZE 0;
set termout off;
spool nw_rdf_model_triple-dump.nt
select x$rdfterm ||' '|| p$rdfterm ||' '|| y$rdfterm || ' .' from table(sem_match('Select ?g ?x ?p ?y FROM NAMED <http://examples.com/ns#Graph1> {GRAPH ?g {?x ?p ?y}}',sem_models('NW_MODEL'),null,null,null,null,' GRAPH_MATCH_UNNAMED=T PLUS_RDFT=T '));

spool off
quit

You should have a file called nw_rdf_model_triple-dump.nt with the triples in it. Edit the file - remove the first and last lines.

Execute some SPARQL queries againt NW_MODEL

PREFIX  rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  owl: <http://www.w3.org/2002/07/owl#>
PREFIX  xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX   dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

describe <http://nwrdf/PRODUCTS/PRODUCTID=24>

Some other queries to get you started:

PREFIX  rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  owl: <http://www.w3.org/2002/07/owl#>
PREFIX  xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX   dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?p ?o
{ 
  <http://nwrdf/PRODUCTS/PRODUCTID=24>  ?p ?o
}
PREFIX  rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  owl: <http://www.w3.org/2002/07/owl#>
PREFIX  xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX   dc: <http://purl.org/dc/elements/1.1/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?product_name
WHERE
{
  <http://nwrdf/PRODUCTS/PRODUCTID=24>  <http://nwrdf/PRODUCTS#PRODUCTNAME> ?product_name .
}

 

References

https://docs.oracle.com/en/database/oracle/oracle-database/18/index.html

https://docs.oracle.com/en/database/oracle/oracle-database/18/spgdg/index.html

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha