Library Version 12.1.3.0

Example5b.java: SPARQL query with ORDERED query option
Prev | Chapter 8. Quick Start for the RDF Graph Feature | Next

## Example5b.java: SPARQL query with ORDERED query option

This example shows the SPARQL query from Example5.java: SPARQL query with JOIN_METHOD with additional features including the ORDERED query option, where you can specify that the order in which query patterns are executed, is based on the order in which they are defined in the SPARQL query.

    import com.hp.hpl.jena.query.*;
    import org.openjena.riot.Lang;
    import com.hp.hpl.jena.sparql.core.DatasetImpl;
    import oracle.rdf.kv.client.jena.*;

    public class Example5b
    {

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

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

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

        System.out.println("Create datasetgraph");
        OracleGraphNoSql graph = new OracleGraphNoSql(conn);
        DatasetGraphNoSql datasetGraph
                            = DatasetGraphNoSql.createFrom(graph);

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

        System.out.println("Clear dataset");
        datasetGraph.clearRepository();

        System.out.println("Load data from file into DatasetGraphNoSql");
        DatasetGraphNoSql.load("example.nt", Lang.NQUADS, conn,
                               "http://example.com");

        Dataset ds = DatasetImpl.wrap(datasetGraph);

        String queryString =
            " PREFIX ORACLE_SEM_FS_NS: " +
            " <http://oracle.com/semtech#ordered>" +
            " PREFIX foaf: <http://xmlns.com/foaf/0.1/>" +
            " SELECT ?name1 ?name2 " +
            " WHERE { " +
            " graph <http://example.org/alice/foaf.rdf> { " +
            "   ?person1 foaf:name ?name1 . " +
            "   ?person1 foaf:knows ?person2 . " +
            "   ?person2 foaf:name ?name2 . " +
            " } } ";

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

        Query query = QueryFactory.create(queryString);
        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 Example5b.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 Example5b <store_name> \
    <host_name> <host_port>

    -------------------
    | name1   | name2 |
    ===================
    | "Alice" | "Bob" |
    -------------------

Prev | Up | Next
Example5.java: SPARQL query with JOIN_METHOD | Home | Example5c.java: SPARQL query with TIMEOUT and GRACEFUL TIMEOUT