Library Version 12.1.3.0

Example4.java: Query family relationships on a named graph
Prev | Chapter 8. Quick Start for the RDF Graph Feature | Next

## Example4.java: Query family relationships on a named graph

This example specifies that John loves Mary (included in the default graph), and it selects and displays the subject and object in each fatherOf relationship (as JSON output). Example4b.java in the RDF Graph feature describes the same exercise using a named graph.

    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 Example4
    {

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

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

    System.out.println("Create Oracle NoSQL connection");
    OracleNoSqlConnection conn
         = OracleNoSqlConnection.createInstance(szStoreName,
                                                szHostName,
                                                szHostPort);

    System.out.println("Create Oracle NoSQL model");
    Model model = OracleModelNoSql.createOracleDefaultModelNoSql(conn);

    System.out.println("Clear model");
    model.removeAll();

    System.out.println("Add triples");
    model.getGraph().add(
               Triple.create(Node.createURI("http://example.com/John"),
                             Node.createURI("http://example.com/loves"),
                             Node.createURI("http://example.com/Mary")));

    String queryString  =
                  " select ?person1 ?person2 "                   +
                  " where "                                      +
                  " { ?person1 <http://example.com/loves> ?person2 }";

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

    Query query = QueryFactory.create(queryString);
    QueryExecution qexec = QueryExecutionFactory.create(query, model);

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

    finally {
          qexec.close();
        }

    model.close();
    conn.dispose();
      }
    }

The following are the commands to compile and run this, 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 Example4.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 Example4b <store_name> \
    <host_name> <host_port>

    Execute query select ?person1 ?person2 where { ?person1
    <http://example.com/loves> ?person2 }

    {
      "head": {
        "vars": [ "person1" , "person2" ]
      } ,
      "results": {
        "bindings": [
          {
            "person1": { "type": "uri" , "value": "http://example.com/John" } ,
            "person2": { "type": "uri" , "value": "http://example.com/Mary" }
          }
        ]
      }
    }

Prev | Up | Next
Concurrent RDF data loading | Home | Example5.java: SPARQL query with JOIN_METHOD