Chapter 6. Update an RDF Graph

The RDF Graph feature supports SPARQL Update (http://www.w3.org/TR/sparql11-update/), also referred to as SPARUL. The primary programming APIs involve the Apache Jena class UpdateAction (in package com.hp.hpl.jena.update) and the RDF Graph feature classes OracleGraphNoSql and DatasetGraphNoSql. The following example shows a SPARQL Update operation that removes all triples in named graph <http://example/graph> from the relevant model stored in the database.

OracleGraphNoSql oracleGraph = .... ;
DatasetGraphNoSql dsgos = DatasetGraphNoSql.createFrom(oracleGraph); 

// SPARQL Update operation
String szUpdateAction = "DROP GRAPH <http://example/graph>"; 
// Execute the Update against a DatasetGraph instance 
// (can be a Jena Model as well)
UpdateAction.parseExecute(szUpdateAction, dsgos); 

Note that the Oracle NoSQL Database does not keep any information about an empty named graph. This implies that if you invoke the CREATE GRAPH <graph_name> without adding any triples into this graph, then no triples will be created. With an Oracle NoSQL Database, you can safely skip the CREATE GRAPH step, as is the case in the following example.

PREFIX example: <http://example/>
INSERT DATA { 
    GRAPH <http://example/graph> { 
		example:anne example:age 30 . 
		example:peter example:birthyear 1982  
	}
} ;
DELETE DATA { 
     GRAPH <http://example/graph> { example:anne example:age 30 . }
}

The following example shows a SPARQL Update operation (from ARQ 2.9.2) involving multiple insert and delete operations.

PREFIX : <http://example/>
CREATE GRAPH <http://example/graph>;
INSERT DATA { example:anne example:age 30 };
INSERT DATA { example:peter example:birthyear 1982 };
DELETE DATA { example:peter example:birthyear 1982 };
INSERT DATA { 
    GRAPH <http://example/graph> { 
		example:anne example:age 30 . 
		example:peter example:birthyear 1982  
	}
};
DELETE DATA { 
     GRAPH <http://example/graph> { example:anne example:age 30 }
} 

After running the update operation in the previous example against an empty DatasetGraphNoSql, running the SPARQL query SELECT ?s ?p ?o WHERE {?s ?p ?o} generates the following response:

-----------------------------------------------------
| s                     | p                    | o  |
=====================================================
| <http://example/anne> | <http://example/age> | 30 |
-----------------------------------------------------

Using the same data, running the SPARQL query SELECT ?s ?p ?o ?g where {GRAPH ?g {?s ?p ?o}} generates the following response:

-----------------------------------------------------------------
| s                      | p                          | o    |
=================================================================
| <http://example/peter> | <http://example/birthyear> | 1982 |
-----------------------------------------------------------------
--------------------------------
|             g                |
================================
| <http://example/graph> |
--------------------------------

In addition to using the Java API for SPARQL Update operations, you can configure Apache Jena Joseki to accept SPARQL Update operations by removing the comment (##) characters at the start of the following lines in the joseki-config.ttl file.

## <#serviceUpdate>
## rdf:type joseki:Service ;
## rdfs:label "SPARQL/Update" ;
## joseki:serviceRef "update/service" ;
## # dataset part
## joseki:dataset <#oracle>;
## # Service part. 
## # This processor will not allow either the protocol,
## # nor the query, to specify the dataset.
## joseki:processor joseki:ProcessorSPARQLUpdate
## .
## 
## <#serviceRead>
## rdf:type joseki:Service ;
## rdfs:label "SPARQL" ;
## joseki:serviceRef "sparql/read" ;
## # dataset part
## joseki:dataset <#oracle> ; ## Same dataset
## # Service part. 
## # This processor will not allow either the protocol,
## # nor the query, to specify the dataset.
## joseki:processor joseki:ProcessorSPARQL_FixedDS ;
## .

After you edit the joseki-config.ttl file, you must restart the Apache Jena Joseki Web application. You can then try a simple update operation, as follows:

  1. In your browser, go to: http://<hostname>:7001/joseki/update.html

  2. Type or paste the following into the text box:

    PREFIX example: <http://example/>
    INSERT DATA { 
    GRAPH <http://example/g1> { example:peter example:birthyear 1970  }
    }
    
  3. Click Perform SPARQL Update.

To verify that the update operation was successful, go to http://<hostname>:7001/joseki and enter the following query:

SELECT *
WHERE 
{ GRAPH <http://example/g1>}{?s ?p ?o}}

The response should contain the following triple:

<http://example/peter> <http://example/birthyear> "1970"

A SPARQL Update can also be sent using an HTTP POST operation to the http://<hostname>:7001/joseki/update/service. For example, you can use cURL (http://en.wikipedia.org/wiki/CURL) to send an HTTP POST request to perform the update operation:

curl --data "request=PREFIX%20%3A%20%3Chttp%3A%2F%2Fexample \
        %2F%3E%20%0AINSERT%20DATA%20%7B%0A%20%20GRAPH%20%3 \
        Chttp%3A%2F%2Fexample%2Fg1%3E%20%7B%20%3Ar%20%3Ap%20 \
        888%20%7D%0A%7D%0A" \
        http://<hostname>:7001/joseki/update/service

In the preceding example, the URL encoded string is a simple INSERT operation into a named graph. After decoding, it reads as follows:

PREFIX : <http://example/>
INSERT DATA { 
GRAPH <http://example/g1> { :r :p 888 }