Library Version 12.1.3.0

Example13.java: SPARQL Query with ARQ Built-in Functions
Prev | Chapter 8. Quick Start for the RDF Graph Feature | Next

## Example13.java: SPARQL Query with ARQ Built-in Functions

This example inserts data about two books, and it displays the book titles in all uppercase characters and the length of each title string.

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

    public class Example13
    {

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

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

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

    // Create model from named graph
    Model model =
          OracleModelNoSql.createOracleModelNoSql(szGraphName, conn);

    // Clear model
    model.removeAll();

    String insertString =
    " PREFIX dc: <http://purl.org/dc/elements/1.1/> "               +
    " INSERT DATA "                                                 +
    " { <http://example/book3> dc:title \"A new book\" ; "          +
    "                          dc:creator \"A.N.Other\" . "         +
    "   <http://example/book4> dc:title \"Semantic Web Rocks\" ; "  +
    "                          dc:creator \"TB\" . "                +
    " } ";

    System.out.println("Execute insert action " + insertString);
    UpdateAction.parseExecute(insertString, model);

    String szQuery
    = "PREFIX dc: <http://purl.org/dc/elements/1.1/> "          +
    "PREFIX fn: <http://www.w3.org/2005/xpath-functions#> "   +
    " SELECT ?subject (fn:upper-case(?object) as ?object1) "  +
    "        (fn:string-length(?object) as ?strlen) "         +
    " WHERE { ?subject dc:title ?object } ";

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

    Query query = QueryFactory.create(szQuery, Syntax.syntaxARQ);
    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 Example13.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 Example13 <store_name> \
    <host_name> <host_port> <graph_name>

    Execute query PREFIX dc: <http://purl.org/dc/elements/1.1/>
    PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
    SELECT ?subject (fn:upper-case(?object) as ?object1)
    (fn:string-length(?object) as ?strlen)
    WHERE { ?subject dc:title ?object }
    ----------------------------------------------------------
    | subject                | object1              | strlen |
    ==========================================================
    | <http://example/book4> | "SEMANTIC WEB ROCKS" | 18     |
    | <http://example/book3> | "A NEW BOOK"         | 10     |
    ----------------------------------------------------------

Prev | Up | Next
Example12.java: SPARQL Involving Named Graphs | Home | Example14: SPARQL Update