Oracle Developers
Published in

Oracle Developers

Build SPARQL Endpoint with Autonomous Database (Part 2/2)

Photo by Cristian Escobar on Unsplash

In Part 1, we have set up the environment to create a SPARQL endpoint using an Autonomous Database instance and a compute instance, both running as a part of the Always Free Services, which you can sign up for today..

In this part, we will upload an example RDF dataset and publish a SPARQL endpoint, so anyone can access the data using SPARQL queries. This is the first step to share your LOD (Linked Open Data) on the web!

Upload an RDF dataset

Let’s try loading the example RDF dataset which you can find from the W3C Turtle specification page (https://www.w3.org/TR/turtle/) as Example 1.

example1.ttl

@base <http://example.org/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix rel: <http://www.perceive.net/schemas/relationship/> .
<#green-goblin>
rel:enemyOf <#spiderman> ;
a foaf:Person ; # in the context of the Marvel universe
foaf:name "Green Goblin" .
<#spiderman>
rel:enemyOf <#green-goblin> ;
a foaf:Person ;
foaf:name "Spiderman", "Человек-паук"@ru .

First, in the Data tab, specify the data source that you registered earlier (ADB1) and create an RDF Network. Here, an RDF Network in Oracle Database is a logical RDF store consisting of a set of tables and indexes. You can have multiple RDF networks in a database.

- Network owner: ADMIN
- Network name: Any network name (e.g. NETWORK1)
- Tablespace: DATA

In the Data tab, specify the RDF Network that you created earlier (ADMIN.NETWORK1) and click the Upload data icon on the Import tab to upload the file to the staging table.

- Upload: Select example1.ttl
- Staging table: Input any new table name (e.g. EXAMPLE1_TABLE)
- Overwrite: false (unless the table name is used already)

Finally, click on the Bulk load data icon on the Import tab to load the data from the staging table into the model. Here, a “model” in Oracle Graph is an administrative unit of a set of triples, and an RDF Network can hold multiple models. You can also specify a model as the target when you execute SPARQL queries and even define a “virtual model” as a collection of models.

- RDF Data
- Model: Any model name (e.g. EXAMPLE1_MODEL)
- Staging table owner: ADMIN
- Staging table: The staging table created above (EXAMPLE1_TABLE)
- Options
- All items: No need to change
- Event Trace
- All items: No need to change

Try SPARQL queries

Let’s try SPARQL queries on the Query UI of the endpoint management server.

Right-click on the target model (in this case, EXAMPLE1_MODEL) and click Open to show the editor.

Click on the Execute button to run the SPARQL query shown in the editor by default, so the triples will be returned as the result.

Here is a query to find the name (variable ?e_name) of the enemy (?e) of a hero named “Spiderman” (?h).

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rel: <http://www.perceive.net/schemas/relationship/>
SELECT ?e_name
WHERE {
?h foaf:name "Spiderman" .
?e rel:enemyOf ?h .
?e foaf:name ?e_name .
}
LIMIT 500

Green Goblin is returned!

Publish the endpoint

At this point, the SPARQL endpoint is not yet published. To allow the endpoint to be published, update the configuration from the Setting tab selecting the target data source (in this case, ADB1). You will see a pop-up window for confirmation, so click OK to proceed.

Right-click on the model and select Publish. Enter the name of the dataset you want to use in the endpoint URL (in our case, example1).

The endpoint URL like below will be displayed, so save it for the next step.

https://<ip_address>:8001/orardf/api/v1/datasets/query/published/example1

Access the endpoint via REST

Let’s send a SPARQL query to an endpoint using REST, typically POST requests. First, save the SPARQL query above to a file on your desktop.

sparql.rq

PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rel: <http://www.perceive.net/schemas/relationship/>
SELECT ?e_name
WHERE {
?h foaf:name "Spiderman" .
?e rel:enemyOf ?h .
?e foaf:name ?e_name .
}
LIMIT 500

Send a POST request having this file content as its query string. Since the server is using a self-signed certificate, we need to allow this by adding -- insecure option.

$ curl https://<ip_address>:8001/orardf/api/v1/datasets/query/published/example1 --data-binary @sparql.rq --insecure -X POST

The result is returned in the W3C standard JSON format by default.

{
"head": {
"vars": [
"E_NAME"
],
"links": []
},
"results": {
"bindings": [
{
"E_NAME": {
"type": "literal",
"value": "Green Goblin"
}
}
]
}
}

Add a graph name on data loading

When loading RDF data, you may want to batch set the graph names of those triples and load them as a quad. In that case, after uploading the triples to a staging table, you can create a view with the graph names added to that table and load it into the RDF model.

First, upload the data to the staging table as described above (Create RDF Network).

- Upload: Select sample.nt
- Staging table: Input any new table name (e.g. SAMPLE_TABLE)
- Overwrite: Off (unless the table name is used already)

Open Database Actions to execute a SQL query.

Oracle Cloud console
> Oracle Database
> Autonomous Database
> (The ADB instance)
> Tools
> Open Database Actions

Execute the following SQL query to create the view. Replace <http://example.org/graph1> with the name of the graph that you want to specify.

create view SAMPLE_VIEW as
select
RDF$STC_SUB
, RDF$STC_PRED
, RDF$STC_OBJ
, '<http://example.org/graph1>' as RDF$STC_GRAPH
from SAMPLE_TABLE

Return to the SPARQL endpoint and click on the Bulk load data icon on the Import tab to load the data from the created view into the new model.

- RDF Data
- Model: Input any model name (e.g. SAMPLE_MODEL)
- Staging table owner: ADMIN
- Staging table: Select the table created above (e.g. SAMPLE_VIEW)
- Options
- All of the items: Do not need to change
- Event Trace
- All of the items: Do not need to change

You can verify that the graph name is included in the results of the following SPARQL query.

SELECT *
WHERE { GRAPH ?g { ?s ?p ?o } }
LIMIT 500

In this article, we introduced how to set up an always-free SPARQL endpoint and publish your datasets using the management server UI. In this way, you can easily manage your RDF datasets on the Autonomous Database, and keep them connected to the “Web of Data.”

Join the conversation!

If you’re curious about the goings-on of Oracle Developers in their natural habitat, come join us on our public Slack channel! We don’t mind being your fish bowl 🐠

If you would like to share your feedback or ask questions to the community, please post to Stack Overflow with oracle-graph tag, or click here to join the Slack workspace of AnDOUC (Analytics and Data Oracle User Community) and post your comments at the #graph channel.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store