Library Version 12.1.3.0

Example12.java: SPARQL Involving Named Graphs
Prev | Chapter 8. Quick Start for the RDF Graph Feature | Next

## Example12.java: SPARQL Involving Named Graphs

This example shows a query involving named graphs. It involves a default graph that has information about named graph URIs and their publishers. The query finds graph names, their publishers, and within each named graph finds the mailbox value using the foaf:mbox predicate.

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

    public class Example12
    {

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

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

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

    // Create Oracle NoSQL graph and dataset
    OracleGraphNoSql graph = new OracleGraphNoSql(conn);
    DatasetGraphNoSql datasetGraph = DatasetGraphNoSql.createFrom(graph);

    // Close graph, as it is no longer needed
    graph.close();

    // Clear dataset
    datasetGraph.clearRepository();

    // add data to the default graph
    datasetGraph.add(new Quad(
            Quad.defaultGraphIRI, // specifies default graph
            Node.createURI("http://example.org/bob"),
            Node.createURI("http://purl.org/dc/elements/1.1/publisher"),
            Node.createLiteral("Bob Hacker")));

    datasetGraph.add(new Quad(
            Quad.defaultGraphIRI, // specifies default graph
            Node.createURI("http://example.org/alice"),
            Node.createURI("http://purl.org/dc/elements/1.1/publisher"),
            Node.createLiteral("alice Hacker")));

    // add data to the bob named graph
    datasetGraph.add(new Quad(
            Node.createURI("http://example.org/bob"), // graph name
            Node.createURI("urn:bob"),
            Node.createURI("http://xmlns.com/foaf/0.1/name"),
            Node.createLiteral("Bob")));

    datasetGraph.add(new Quad(
            Node.createURI("http://example.org/bob"), // graph name
            Node.createURI("urn:bob"),
            Node.createURI("http://xmlns.com/foaf/0.1/mbox"),
            Node.createURI("mailto:bob@example")));

    // add data to the alice named graph
    datasetGraph.add(new Quad(
            Node.createURI("http://example.org/alice"), // graph name
            Node.createURI("urn:alice"),
            Node.createURI("http://xmlns.com/foaf/0.1/name"),
            Node.createLiteral("Alice")));

    datasetGraph.add(new Quad(
            Node.createURI("http://example.org/alice"), // graph name
            Node.createURI("urn:alice"),
            Node.createURI("http://xmlns.com/foaf/0.1/mbox"),
            Node.createURI("mailto:alice@example")));

    Dataset ds = DatasetImpl.wrap(datasetGraph);

    String szQuery = " PREFIX foaf: <http://xmlns.com/foaf/0.1/>"    +
    " PREFIX dc: <http://purl.org/dc/elements/1.1/> "    +
    " SELECT ?who ?graph ?mbox "                         +
    " FROM NAMED <http://example.org/alice>"            +
    " FROM NAMED <http://example.org/bob>"              +
    " WHERE "                                            +
    " { "                                                +
    " ?graph dc:publisher ?who . "                       +
    " GRAPH ?graph { ?x foaf:mbox ?mbox } "              +
    " } ";

    Query query = QueryFactory.create(szQuery);
    QueryExecution qexec = QueryExecutionFactory.create(query, ds);

    try {
          ResultSet results = qexec.execSelect();
          ResultSetFormatter.out(System.out, results, query);
        }

    finally {
          qexec.close();
        }

    ds.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 Example12.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 Example12 <store_name> \
    <host_name> <host_port>

    ----------------------------------------------------------------------
    | who            | graph                      | mbox                 |
    ======================================================================
    | "Bob Hacker"   | <http://example.org/bob>   | <mailto:bob@example> |
    |"alice Hacker"  | <http://example.org/alice> | <mailto:alice@example>|
    ---------------------------------------------------------------------

Prev | Up | Next
Example11.java: SPARQL query with SELECT Cast | Home | Example13.java: SPARQL Query with ARQ Built-in Functions