Chapter 8. Quick Start for the RDF Graph Feature

Table of Contents

Example1.java: Create a default graph and add/delete triples
Example1b.java: Create a named graph and add/delete triples
Example1c.java: Create an inferred graph and add/delete triples
Example1d.java: Create an inferred graph and add/delete triples
Example2.java: Load an RDF file
Concurrent RDF data loading
Example4.java: Query family relationships on a named graph
Example5.java: SPARQL query with JOIN_METHOD
Example5b.java: SPARQL query with ORDERED query option
Example5c.java: SPARQL query with TIMEOUT and GRACEFUL TIMEOUT
Example5d.java: SPARQL query with DOP
Example5e.java: SPARQL query with INFERENCE/ASSERTED ONLY hints
Example5g.java: SPARQL query with PLAN query hint
Example6.java: SPARQL ASK query
Example7.java: SPARQL Describe query
Example8.java: SPARQL Construct query
Example9.java: SPARQL OPTIONAL query
Example10.java: SPARQL query with LIMIT and OFFSET
Example11.java: SPARQL query with SELECT Cast
Example12.java: SPARQL Involving Named Graphs
Example13.java: SPARQL Query with ARQ Built-in Functions
Example14: SPARQL Update
Example15.java: Oracle NOSQL Database Connection Pooling
Generate Data sampling for a graph in the Oracle NoSQL Database
Example16b. Generate Data sampling for the dataset in the Oracle NoSQL Database
Build an Ontology Model using Jena OntModel APIs

This section provides examples for the major capabilities of the RDF Graph feature. Each example is self-contained: it typically creates a graph, adds triples, performs a query that may involve inference, displays the result, and drops the model.

These examples can be found in the examples/ directory.

This section includes examples that do the following:

To run a query, you must do the following:

  1. Include the code in a Java source file. The examples used in this section are provided as files in the examples directory of the package.

  2. Compile the Java source file. For example:

    javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
    ./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
    ./slf4j-api-1.6.4.jar:./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
    ./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example.java
  3. Run the compiled file. For example:

    java -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
    ./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
    ./slf4j-api-1.6.4.jar:./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
    ./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example <store_name \
    > <host_name> <host_port>

Example1.java: Create a default graph and add/delete triples

This example shows how to add/remove a set of triples over a default graph stored in an Oracle NoSQL Database.

import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.Model;
import oracle.rdf.kv.client.jena.*;

public class Example1 {
  
  public static void main(String[] args) throws Exception
  {
    
String szStoreName = args[0];
String szHostName = args[1];
String szHostPort = args[2];

OracleNoSqlConnection conn 
     = OracleNoSqlConnection.createInstance(szStoreName,
                                            szHostName, 
                                            szHostPort);
    
// This object will handle operations over the default graph
OracleGraphNoSql graph = new OracleGraphNoSql(conn);
graph.clearRepository(); //Clear the graph including inferred triples
    
graph.add(Triple.create(Node.createURI("u:John"),
                        Node.createURI("u:parentOf"),
                        Node.createURI("u:Mary")));
        
graph.add(Triple.create(Node.createURI("u:Mary"), 
                        Node.createURI("u:parentOf"),
                        Node.createURI("u:Jack")));

String queryString = " select ?x ?y WHERE {?x <u:parentOf> ?y}";
System.out.println("Execute query " + queryString);

Model model = new OracleModelNoSql(graph);
Query query = QueryFactory.create(queryString);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
    
try {
      ResultSet results = qexec.execSelect();
      ResultSetFormatter.out(System.out, results, query);
    }
    
finally {
      qexec.close();
    }

graph.delete(Triple.create(Node.createURI("u:John"), 
                               Node.createURI("u:parentOf"),
                               Node.createURI("u:Mary")));

queryString = "select ?x ?y ?z WHERE {?x ?y ?z}";
System.out.println("Execute query " + queryString);

query = QueryFactory.create(queryString) ;
qexec = QueryExecutionFactory.create(query, model);
    
try {
      ResultSet results = qexec.execSelect();
      ResultSetFormatter.out(System.out, results, query);
    }
    
finally {
      qexec.close();
    }
    
model.close();
conn.dispose();
    
  }
} 

The following are the commands to compile and run this example, as well as the expected output of the java command.

javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
./slf4j-api-1.6.4.jar: ./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar Example.java

javac -classpath ./:./jena-core-2.7.4.jar:./jena-arq-2.9.4.jar: \
./sdordfnosqlclient.jar:./kvclient.jar:./xercesImpl-2.10.0.jar: \
./slf4j-api-1.6.4.jar: ./slf4j-log4j12-1.6.4.jar:./log4j/1.2.16.jar: \
./jena-iri-0.9.4.jar:./xml-apis-1.4.01.jar \
Example1 <store_name> <host_name> <host_port>


Execute query select ?x ?y WHERE {?x <u:parentOf> ?y} 
-----------------------
| x        | y        |
=======================
| <u:Mary> | <u:Jack> |
| <u:John> | <u:Mary> |
-----------------------


Execute query select ?x ?y ?z WHERE {?x ?y ?z}
--------------------------------------
| x        | y            | z        |
======================================
| <u:Mary> | <u:parentOf> | <u:Jack> |
--------------------------------------