Library Version 12.1.3.0

Example1b.java: Create a named graph and add/delete triples
Prev | Chapter 8. Quick Start for the RDF Graph Feature | Next

## Example1b.java: Create a named graph and add/delete triples

This example describes how to add/remove a set of triples over a named graph stored in an Oracle NoSQL Database.

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

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

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

    OracleNoSqlConnection conn
         = OracleNoSqlConnection.createInstance(szStoreName,
                                                szHostName,
                                                szHostPort);

    // This object will handle operations over the named graph
    OracleGraphNoSql graph
                     = new OracleNamedGraphNoSql(szGraphName, conn);
    // Clear the named graph including inferred triples
    graph.clearRepository();

    // Add triples
    graph.add(Triple.create(Node.createURI("u:John"),
                                Node.createURI("u:parentOf"),
                                Node.createURI("u:Mary")));

    graph.add(Triple.create(Node.createURI("u:Mary"),
                                Node.createURI("u:parentOf"),
                                Node.createURI("u:Jack")));

    String queryString = " select ?x ?y WHERE {?x <u:parentOf> ?y}";
    System.out.println("Execute query " + queryString);

    Model model = new OracleModelNoSql(graph);
    Query query = QueryFactory.create(queryString);
    QueryExecution qexec = QueryExecutionFactory.create(query, model);

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

    finally {
          qexec.close();
        }

    graph.delete(Triple.create(Node.createURI("u:John"),
                                   Node.createURI("u:parentOf"),
                                   Node.createURI("u:Mary")));

    queryString = "select ?x ?y ?z WHERE {?x ?y ?z}";
    System.out.println("Execute query " + queryString);

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

    Execute query select ?x ?y WHERE {?x <u:parentOf>?y}
    -----------------------
    | x        | y        |
    =======================
    | <u:Mary> | <u:Jack> |
    | <u:John> | <u:Mary> |
    -----------------------

    Execute query select ?x ?y ?z WHERE {?x ?y ?z}
    --------------------------------------
    | x        | y            | z        |
    ======================================
    | <u:Mary> | <u:parentOf> | <u:Jack> |
    --------------------------------------

Prev | Up | Next
Chapter 8. Quick Start for the RDF Graph Feature | Home | Example1c.java: Create an inferred graph and add/delete triples