Library Version 12.1.3.0

Example11.java: SPARQL query with SELECT Cast
Prev | Chapter 8. Quick Start for the RDF Graph Feature | Next

## Example11.java: SPARQL query with SELECT Cast

This example "converts" two Fahrenheit temperatures (18.1 and 32.0) to Celsius temperatures.

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

    public class Example11
    {

      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 from named graph
    Model model =
          OracleModelNoSql.createOracleModelNoSql(szModelName, conn);

    // Clear model
    model.removeAll();

    String insertString =
    " PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "           +
    " INSERT DATA "                                               +
    " { "                                                         +
    "     <u:Object1> <u:temp> \"18.1\"^^xsd:float ; "            +
    "     <u:name> \"Foo... \" . "                                +
    "     <u:Object2> <u:temp> \"32.0\"^^xsd:float ; "            +
    "     <u:name> \"Bar... \" . "                                +
    " } ";

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

    String szQuery =
    " PREFIX fn: <http://www.w3.org/2005/xpath-functions#> "         +
    " SELECT ?subject ?temp ((?temp - 32.0)*5/9 as ?celsius_temp) "   +
    " WHERE { ?subject <u:temp> ?temp } ";

    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 Example11.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 Example11 <store_name> \
    <host_name> <host_port> <graph_name>

    Execute insert action  PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
    INSERT DATA  {
    <u:Object1> <u:temp> "18.1"^^xsd:float ; <u:name> "Foo... " .
    <u:Object2> <u:temp> "32.0"^^xsd:float ; <u:name> "Bar... " . }

    Execute query  PREFIX fn: <http://www.w3.org/2005/xpath-functions#>
    SELECT ?subject ?temp ((?temp - 32.0)*5/9 as ?celsius_temp)
    WHERE { ?subject <u:temp> ?temp }
    ----------------------------------------------------
    | subject     | temp | celsius_temp                |
    ====================================================
    | <u:Object1> | 18.1 | -7.722222222222222222222223 |
    | <u:Object2> | 32   | 0.                          |
    ----------------------------------------------------

Prev | Up | Next
Example10.java: SPARQL query with LIMIT and OFFSET | Home | Example12.java: SPARQL Involving Named Graphs