Library Version 12.1.3.0

Example9.java: SPARQL OPTIONAL query
Prev | Chapter 8. Quick Start for the RDF Graph Feature | Next

## Example9.java: SPARQL OPTIONAL query

This example shows a SPARQL OPTIONAL query. It inserts triples that assert the following:

- John is a parent of Mary.

- John is a parent of Jack.

- Mary is a parent of Jill.

It then finds parent-child relationships, optionally including any grandchild (gkid) relationships.

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

    public class Example9
    {

      public static void main(String[] args) throws Exception
      {

    String szStoreName  = args[0];
    String szHostName   = args[1];
    String szHostPort   = args[2];
    String szModelName  = args[3];

    // Create Oracle NoSQL connection
    OracleNoSqlConnection conn
              = OracleNoSqlConnection.createInstance(szStoreName,
                                                     szHostName,
                                                     szHostPort);

    // Create model for named graph
    OracleModelNoSql model
                  = OracleModelNoSql.createOracleModelNoSql(szModelName,
                                                            conn);

    // Clear model
    model.removeAll();

    // Get graph from model
    OracleGraphNoSql graph = model.getGraph();

    // Add triples
    graph.add(Triple.create(Node.createURI("u:John"),
                            Node.createURI("u:parentOf"),
                            Node.createURI("u:Mary")));

    graph.add(Triple.create(Node.createURI("u:John"),
                            Node.createURI("u:parentOf"),
                            Node.createURI("u:Jack")));

    graph.add(Triple.create(Node.createURI("u:Mary"),
                            Node.createURI("u:parentOf"),
                            Node.createURI("u:Jill")));

    String szQuery = " SELECT ?s ?o ?gkid "                     +
                     " WHERE { "                                +
                     "    ?s <u:parentOf> ?o  . "               +
                     "    OPTIONAL {?o <u:parentOf> ?gkid } "   +
                     " } ";

    System.out.println("Execute query " + szQuery);

    Query query = QueryFactory.create(szQuery);
    QueryExecution 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 Example9.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 Example9 <store_name> \
    <host_name> <host_port> <graph_name>
    ----------------------------------
    | s        | o        | gkid     |
    ==================================
    | <u:Mary> | <u:Jill> |          |
    | <u:John> | <u:Jack> |          |
    | <u:John> | <u:Mary> | <u:Jill> |
    ----------------------------------

Prev | Up | Next
Example8.java: SPARQL Construct query | Home | Example10.java: SPARQL query with LIMIT and OFFSET