16.18.1.Virtuoso Jena Provider

What is Jena

Jena is an open source Semantic Web framework for Java. It provides an API to extract data from and write to RDF graphs. The graphs are represented as an abstract "model". A model can be sourced with data from files, databases, URIs or a combination of these. A Model can also be queried through SPARQL and updated through SPARUL.

What is the Virtuoso Jena Provider

The Virtuoso Jena RDF Data Provider is a fully operational Native Graph Model Storage Provider for the Jena Framework, which enables Semantic Web applications written using the Jena RDF Frameworks to directly query the Virtuoso RDF Quad Store. Providers are available for the latest Jena 2.6.x and 2.10.x versions.

Figure16.185.Virtuoso Jena RDF Data Provider

Virtuoso Jena RDF Data Provider

Setup

Prerequisites

Download the latest Virtuoso Jena Provider for your Jena framework version, Virtuoso JDBC 3 Driver, Jena Framework, and associated classes and sample programs.

  • Note:

    The Jena Provider is explicitly bound to the Virtuoso JDBC 3 Driver. You cannot use the Virtuoso JDBC 4 Driver for this purpose at this time.

  • The version of the Jena Provider (virt_jena.jar) can be verified with the command:

    $ java -jar virt_jena.jar
    OpenLink Virtuoso(TM) Provider for Jena(TM) Version 2.6.2 [Build 1.2]
    
  • Files contained in the zip files are generally older than specifically linked downloads (e.g., the Virtuoso JDBC Driver, virtjdbc3.jar), so don't replace if prompted during extraction. Instead, rename the file extracted from the zip, and compare their versions to be sure you keep only the most recent.

    $ java -cp virtjdbc3.jar virtuoso.jdbc3.Driver
    OpenLink Virtuoso(TM) Driver for JDBC(TM) Version 3.x [Build 3.57]
    $ java -cp virtjdbc3.fromzip.jar virtuoso.jdbc3.Driver
    OpenLink Virtuoso(TM) Driver for JDBC(TM) Version 3.x [Build 3.11]
    
  • Downloads:

Compiling Jena Sample Programs
  1. Edit the sample programs VirtuosoSPARQLExampleX.java, where X = 1 to 9. Set the JDBC connection strings within to point to a valid Virtuoso Server instance of the form:

    "jdbc:virtuoso://localhost:1111/charset=UTF-8/log_enable=2"
    
    • charset=UTF-8 will be added by Jena provider, if it isn't in connection string. So now you don't need add "charset=UTF-8" to the connection string any more, it is done by Jena provider.

    • log_enable=2: to use row auto commit

    • use these settings to process large rdf data.

  2. Ensure that full paths to

    jena.jar, arq.jar,

    and

    virtjdbc3.jar

    are included in the active CLASSPATH setting.

  3. Compile the Jena Sample applications using the following command:

    javac -cp "jena.jar:arq.jar:virtjdbc3.jar:virt_jena.jar:." VirtuosoSPARQLExample1.java
    VirtuosoSPARQLExample2.java VirtuosoSPARQLExample3.java VirtuosoSPARQLExample4.java
    VirtuosoSPARQLExample5.java VirtuosoSPARQLExample6.java VirtuosoSPARQLExample7.java
    VirtuosoSPARQLExample8.java VirtuosoSPARQLExample9.java
    
Testing

Once the Provider classes and sample program have been successfully compiled, the Provider can be tested using the sample programs included. Ensure your active CLASSPATH includes full paths to all of the following files, before executing the example commands:

  • icu4j_3_4.jar

  • iri.jar

  • xercesImpl.jar

  • axis.jar

  • commons-logging-1.1.1.jar

  • jena.jar

  • arq.jar

  • virtjdbc3.jar

  • virt_jena.jar

  1. VirtuosoSPARQLExample1 returns the contents of the RDF Quad store of the targeted Virtuoso instance, with the following command:

    java VirtuosoSPARQLExample1
    
  2. VirtuosoSPARQLExample2 reads in the contents of the following FOAF URIs --

    http://kidehen.idehen.net/dataspace/person/kidehen#this
    http://www.w3.org/People/Berners-Lee/card#i
    http://demo.openlinksw.com/dataspace/person/demo#this
    

    -- and returns the RDF data stored, with the following command:

    java VirtuosoSPARQLExample2
    
  3. VirtuosoSPARQLExample3 performs simple addition and deletion operation on the content of the triple store, with the following command:

    java VirtuosoSPARQLExample3
    
  4. VirtuosoSPARQLExample4 demonstrates the use of the graph.contains method for searching triples, with the following command:

    java VirtuosoSPARQLExample4
    
  5. VirtuosoSPARQLExample5 demonstrates the use of the graph.find method for searching triples, with the following command:

    java VirtuosoSPARQLExample5
    
  6. VirtuosoSPARQLExample6 demonstrates the use of the graph.getTransactionHandler method, with the following command:

    java VirtuosoSPARQLExample6
    
  7. VirtuosoSPARQLExample7 demonstrates the use of the graph.getBulkUpdateHandler method, with the following command:

    java VirtuosoSPARQLExample7
    
  8. VirtuosoSPARQLExample8 demonstrates how to insert triples into a graph, with the following command:

    java VirtuosoSPARQLExample8
    
  9. VirtuosoSPARQLExample9 demonstrates the use of the CONSTRUCT, DESCRIBE, and ASK SPARQL query forms, with the following command:

    java VirtuosoSPARQLExample9
    

Examples

VirtJenaSPARQLExample1
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.RDFNode;

import virtuoso.jena.driver.*;

public class VirtuosoSPARQLExample1 {

        /**
         * Executes a SPARQL query against a virtuoso url and prints results.
         */
        public static void main(String[] args) {

                String url;
                if(args.length == 0)
                    url = "jdbc:virtuoso://localhost:1111";
                else
                    url = args[0];

/*                      STEP 1                  */
                VirtGraph set = new VirtGraph (url, "dba", "dba");

/*                      STEP 2                  */

/*                      STEP 3                  */
/*              Select all data in virtuoso     */
                Query sparql = QueryFactory.create("SELECT * WHERE { GRAPH ?graph { ?s ?p ?o } } limit 100");

/*                      STEP 4                  */
                VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create (sparql, set);

                ResultSet results = vqe.execSelect();
                while (results.hasNext()) {
                        QuerySolution result = results.nextSolution();
                    RDFNode graph = result.get("graph");
                    RDFNode s = result.get("s");
                    RDFNode p = result.get("p");
                    RDFNode o = result.get("o");
                    System.out.println(graph + " { " + s + " " + p + " " + o + " . }");
                }
        }
}

VirtJenaSPARQLExample2
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.RDFNode;

import virtuoso.jena.driver.*;

public class VirtuosoSPARQLExample2 {

        /**
         * Executes a SPARQL query against a virtuoso url and prints results.
         */
        public static void main(String[] args) {

                String url;
                if(args.length == 0)
                    url = "jdbc:virtuoso://localhost:1111";
                else
                    url = args[0];

/*                      STEP 1                  */
                VirtGraph graph = new VirtGraph ("Example2", url, "dba", "dba");

/*                      STEP 2                  */
/*              Load data to Virtuoso           */
                graph.clear ();

                System.out.print ("Begin read from 'http://www.w3.org/People/Berners-Lee/card#i'  ");
                graph.read("http://www.w3.org/People/Berners-Lee/card#i", "RDF/XML");
                System.out.println ("\t\t\t Done.");

                System.out.print ("Begin read from 'http://demo.openlinksw.com/dataspace/person/demo#this'  ");
                graph.read("http://demo.openlinksw.com/dataspace/person/demo#this", "RDF/XML");
                System.out.println ("\t Done.");

                System.out.print ("Begin read from 'http://kidehen.idehen.net/dataspace/person/kidehen#this'  ");
                graph.read("http://kidehen.idehen.net/dataspace/person/kidehen#this", "RDF/XML");
                System.out.println ("\t Done.");

/*                      STEP 3                  */
/*              Select only from VirtGraph      */
                Query sparql = QueryFactory.create("SELECT ?s ?p ?o WHERE { ?s ?p ?o }");

/*                      STEP 4                  */
                VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create (sparql, graph);

                ResultSet results = vqe.execSelect();
                while (results.hasNext()) {
                        QuerySolution result = results.nextSolution();
                    RDFNode graph_name = result.get("graph");
                    RDFNode s = result.get("s");
                    RDFNode p = result.get("p");
                    RDFNode o = result.get("o");
                    System.out.println(graph_name + " { " + s + " " + p + " " + o + " . }");
                }

                System.out.println("graph.getCount() = " + graph.getCount());
        }
}
VirtJenaSPARQLExample3
import java.util.*;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;

import virtuoso.jena.driver.*;

public class VirtuosoSPARQLExample3
{
    public static void main(String[] args)
    {
        String url;

        if(args.length == 0)
            url = "jdbc:virtuoso://localhost:1111";
        else
            url = args[0];

        Node foo1 = Node.createURI("http://example.org/#foo1");
        Node bar1 = Node.createURI("http://example.org/#bar1");
        Node baz1 = Node.createURI("http://example.org/#baz1");

        Node foo2 = Node.createURI("http://example.org/#foo2");
        Node bar2 = Node.createURI("http://example.org/#bar2");
        Node baz2 = Node.createURI("http://example.org/#baz2");

        Node foo3 = Node.createURI("http://example.org/#foo3");
        Node bar3 = Node.createURI("http://example.org/#bar3");
        Node baz3 = Node.createURI("http://example.org/#baz3");

        List <Triple> triples = new ArrayList <Triple> ();

        VirtGraph graph = new VirtGraph ("Example3", url, "dba", "dba");

        graph.clear ();

        System.out.println("graph.isEmpty() = " + graph.isEmpty());
        System.out.println("Add 3 triples to graph <Example3>.");

        graph.add(new Triple(foo1, bar1, baz1));
        graph.add(new Triple(foo2, bar2, baz2));
        graph.add(new Triple(foo3, bar3, baz3));

        System.out.println("graph.isEmpty() = " + graph.isEmpty());
        System.out.println("graph.getCount() = " + graph.getCount());

        triples.add(new Triple(foo1, bar1, baz1));
        triples.add(new Triple(foo2, bar2, baz2));

        graph.isEmpty();

        System.out.println("Remove 2 triples from graph <Example3>");
        graph.remove(triples);
        System.out.println("graph.getCount() = " + graph.getCount());
        System.out.println("Please check result with isql tool.");

        /* EXPECTED RESULT:

SQL> SPARQL
SELECT ?s ?p ?o
FROM <Example3>
WHERE {?s ?p ?o};
s                                                    p                                                             o
VARCHAR                                    VARCHAR                                              VARCHAR
_______________________________________________________________________________

http://example.org/#foo3              http://example.org/#bar3                         http://example.org/#baz3

1 Rows. -- 26 msec.
SQL>

*/

        }
}
VirtJenaSPARQLExample4
import java.util.*;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;

import virtuoso.jena.driver.*;

public class VirtuosoSPARQLExample4
{

    public static void main(String[] args)
    {
        String url;
        if(args.length == 0)
            url = "jdbc:virtuoso://localhost:1111";
        else
            url = args[0];

        Node foo1 = Node.createURI("http://example.org/#foo1");
        Node bar1 = Node.createURI("http://example.org/#bar1");
        Node baz1 = Node.createURI("http://example.org/#baz1");

        Node foo2 = Node.createURI("http://example.org/#foo2");
        Node bar2 = Node.createURI("http://example.org/#bar2");
        Node baz2 = Node.createURI("http://example.org/#baz2");

        Node foo3 = Node.createURI("http://example.org/#foo3");
        Node bar3 = Node.createURI("http://example.org/#bar3");
        Node baz3 = Node.createURI("http://example.org/#baz3");

        VirtGraph graph = new VirtGraph ("Example4", url, "dba", "dba");

        graph.clear ();

        System.out.println("graph.isEmpty() = " + graph.isEmpty());

        System.out.println("Add 3 triples to graph <Example4>.");

        graph.add(new Triple(foo1, bar1, baz1));
        graph.add(new Triple(foo2, bar2, baz2));
        graph.add(new Triple(foo3, bar3, baz3));

        System.out.println("graph.isEmpty() = " + graph.isEmpty());
        System.out.println("graph.getCount() = " + graph.getCount());

        System.out.println ("graph.contains(new Triple(foo2, bar2, baz2) - " + graph.contains(new Triple(foo2, bar2, baz2)));
        System.out.println ("graph.contains(new Triple(foo2, bar2, baz3) - " + graph.contains(new Triple(foo2, bar2, baz3)));

        graph.clear ();

    }
}
VirtJenaSPARQLExample5
import java.util.*;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;

import virtuoso.jena.driver.*;

public class VirtuosoSPARQLExample5
{

    public static void main(String[] args)
    {
        String url;
        if(args.length == 0)
            url = "jdbc:virtuoso://localhost:1111";
        else
            url = args[0];

        Node foo1 = Node.createURI("http://example.org/#foo1");
        Node bar1 = Node.createURI("http://example.org/#bar1");
        Node baz1 = Node.createURI("http://example.org/#baz1");

        Node foo2 = Node.createURI("http://example.org/#foo2");
        Node bar2 = Node.createURI("http://example.org/#bar2");
        Node baz2 = Node.createURI("http://example.org/#baz2");

        Node foo3 = Node.createURI("http://example.org/#foo3");
        Node bar3 = Node.createURI("http://example.org/#bar3");
        Node baz3 = Node.createURI("http://example.org/#baz3");

        VirtGraph graph = new VirtGraph ("Example5", url, "dba", "dba");

        graph.clear ();

        System.out.println("graph.isEmpty() = " + graph.isEmpty());

        System.out.println("Add 3 triples to graph <Example5>.");

        graph.add(new Triple(foo1, bar1, baz1));
        graph.add(new Triple(foo2, bar2, baz2));
        graph.add(new Triple(foo3, bar3, baz3));
        graph.add(new Triple(foo1, bar2, baz2));
        graph.add(new Triple(foo1, bar3, baz3));

        System.out.println("graph.isEmpty() = " + graph.isEmpty());
        System.out.println("graph.getCount() = " + graph.getCount());

        ExtendedIterator iter = graph.find(foo1, Node.ANY, Node.ANY);
        System.out.println ("\ngraph.find(foo1, Node.ANY, Node.ANY) \nResult:");
        for ( ; iter.hasNext() ; )
            System.out.println ((Triple) iter.next());

        iter = graph.find(Node.ANY, Node.ANY, baz3);
        System.out.println ("\ngraph.find(Node.ANY, Node.ANY, baz3) \nResult:");
        for ( ; iter.hasNext() ; )
            System.out.println ((Triple) iter.next());

        iter = graph.find(foo1, Node.ANY, baz3);
        System.out.println ("\ngraph.find(foo1, Node.ANY, baz3) \nResult:");
        for ( ; iter.hasNext() ; )
            System.out.println ((Triple) iter.next());

        graph.clear ();

    }
}
VirtJenaSPARQLExample6
import java.util.*;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;

import virtuoso.jena.driver.*;

public class VirtuosoSPARQLExample6
{

    public static void main(String[] args)
    {
        String url;
        if(args.length == 0)
            url = "jdbc:virtuoso://localhost:1111";
        else
            url = args[0];

        Node foo1 = Node.createURI("http://example.org/#foo1");
        Node bar1 = Node.createURI("http://example.org/#bar1");
        Node baz1 = Node.createURI("http://example.org/#baz1");

        Node foo2 = Node.createURI("http://example.org/#foo2");
        Node bar2 = Node.createURI("http://example.org/#bar2");
        Node baz2 = Node.createURI("http://example.org/#baz2");

        Node foo3 = Node.createURI("http://example.org/#foo3");
        Node bar3 = Node.createURI("http://example.org/#bar3");
        Node baz3 = Node.createURI("http://example.org/#baz3");

        VirtGraph graph = new VirtGraph ("Example6", url, "dba", "dba");

        graph.clear ();

        System.out.println("graph.isEmpty() = " + graph.isEmpty());

        System.out.println("test Transaction Commit.");
        graph.getTransactionHandler().begin();
        System.out.println("begin Transaction.");
        System.out.println("Add 3 triples to graph <Example6>.");

        graph.add(new Triple(foo1, bar1, baz1));
        graph.add(new Triple(foo2, bar2, baz2));
        graph.add(new Triple(foo3, bar3, baz3));

        graph.getTransactionHandler().commit();
        System.out.println("commit Transaction.");
        System.out.println("graph.isEmpty() = " + graph.isEmpty());
        System.out.println("graph.getCount() = " + graph.getCount());

        ExtendedIterator iter = graph.find(Node.ANY, Node.ANY, Node.ANY);
        System.out.println ("\ngraph.find(Node.ANY, Node.ANY, Node.ANY) \nResult:");
        for ( ; iter.hasNext() ; )
            System.out.println ((Triple) iter.next());

        graph.clear ();
        System.out.println("\nCLEAR graph <Example6>");
        System.out.println("graph.isEmpty() = " + graph.isEmpty());

        System.out.println("Add 1 triples to graph <Example6>.");
        graph.add(new Triple(foo1, bar1, baz1));

        System.out.println("test Transaction Abort.");
        graph.getTransactionHandler().begin();
        System.out.println("begin Transaction.");
        System.out.println("Add 2 triples to graph <Example6>.");

        graph.add(new Triple(foo2, bar2, baz2));
        graph.add(new Triple(foo3, bar3, baz3));

        graph.getTransactionHandler().abort();
        System.out.println("abort Transaction.");
        System.out.println("graph.isEmpty() = " + graph.isEmpty());
        System.out.println("graph.getCount() = " + graph.getCount());

        iter = graph.find(Node.ANY, Node.ANY, Node.ANY);
        System.out.println ("\ngraph.find(Node.ANY, Node.ANY, Node.ANY) \nResult:");
        for ( ; iter.hasNext() ; )
            System.out.println ((Triple) iter.next());

        graph.clear ();
        System.out.println("\nCLEAR graph <Example6>");

    }
}

VirtJenaSPARQLExample7
import java.util.*;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Triple;

import virtuoso.jena.driver.*;

public class VirtuosoSPARQLExample7
{

    public static void main(String[] args)
    {
        String url;
        if(args.length == 0)
            url = "jdbc:virtuoso://localhost:1111";
        else
            url = args[0];

        Node foo1 = Node.createURI("http://example.org/#foo1");
        Node bar1 = Node.createURI("http://example.org/#bar1");
        Node baz1 = Node.createURI("http://example.org/#baz1");

        Node foo2 = Node.createURI("http://example.org/#foo2");
        Node bar2 = Node.createURI("http://example.org/#bar2");
        Node baz2 = Node.createURI("http://example.org/#baz2");

        Node foo3 = Node.createURI("http://example.org/#foo3");
        Node bar3 = Node.createURI("http://example.org/#bar3");
        Node baz3 = Node.createURI("http://example.org/#baz3");

        List triples1 = new ArrayList();
        triples1.add(new Triple(foo1, bar1, baz1));
        triples1.add(new Triple(foo2, bar2, baz2));
        triples1.add(new Triple(foo3, bar3, baz3));

        List triples2 = new ArrayList();
        triples2.add(new Triple(foo1, bar1, baz1));
        triples2.add(new Triple(foo2, bar2, baz2));

        VirtGraph graph = new VirtGraph ("Example7", url, "dba", "dba");

        graph.clear ();

        System.out.println("graph.isEmpty() = " + graph.isEmpty());
        System.out.println("Add List with 3 triples to graph <Example7> via BulkUpdateHandler.");

        graph.getBulkUpdateHandler().add(triples1);

        System.out.println("graph.isEmpty() = " + graph.isEmpty());
        System.out.println("graph.getCount() = " + graph.getCount());

        ExtendedIterator iter = graph.find(Node.ANY, Node.ANY, Node.ANY);
        System.out.println ("\ngraph.find(Node.ANY, Node.ANY, Node.ANY) \nResult:");
        for ( ; iter.hasNext() ; )
            System.out.println ((Triple) iter.next());

        System.out.println("\n\nDelete List of 2 triples from graph <Example7> via BulkUpdateHandler.");

        graph.getBulkUpdateHandler().delete(triples2);

        System.out.println("graph.isEmpty() = " + graph.isEmpty());
        System.out.println("graph.getCount() = " + graph.getCount());

        iter = graph.find(Node.ANY, Node.ANY, Node.ANY);
        System.out.println ("\ngraph.find(Node.ANY, Node.ANY, Node.ANY) \nResult:");
        for ( ; iter.hasNext() ; )
            System.out.println ((Triple) iter.next());

        graph.clear ();
        System.out.println("\nCLEAR graph <Example7>");

    }
}
VirtJenaSPARQLExample8
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.RDFNode;

import virtuoso.jena.driver.*;

public class VirtuosoSPARQLExample8 {

        /**
         * Executes a SPARQL query against a virtuoso url and prints results.
         */
        public static void main(String[] args) {

                String url;
                if(args.length == 0)
                    url = "jdbc:virtuoso://localhost:1111";
                else
                    url = args[0];

/*                      STEP 1                  */
                VirtGraph set = new VirtGraph (url, "dba", "dba");

/*                      STEP 2                  */
System.out.println("\nexecute: CLEAR GRAPH <http://test1>");
                String str = "CLEAR GRAPH <http://test1>";
                VirtuosoUpdateRequest vur = VirtuosoUpdateFactory.create(str, set);
                vur.exec();

System.out.println("\nexecute: INSERT INTO GRAPH <http://test1> { <aa> <bb> 'cc' . <aa1> <bb1> 123. }");
                str = "INSERT INTO GRAPH <http://test1> { <aa> <bb> 'cc' . <aa1> <bb1> 123. }";
                vur = VirtuosoUpdateFactory.create(str, set);
                vur.exec();

/*                      STEP 3                  */
/*              Select all data in virtuoso     */
System.out.println("\nexecute: SELECT * FROM <http://test1> WHERE { ?s ?p ?o }");
                Query sparql = QueryFactory.create("SELECT * FROM <http://test1> WHERE { ?s ?p ?o }");

/*                      STEP 4                  */
                VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create (sparql, set);

                ResultSet results = vqe.execSelect();
                while (results.hasNext()) {
                        QuerySolution rs = results.nextSolution();
                    RDFNode s = rs.get("s");
                    RDFNode p = rs.get("p");
                    RDFNode o = rs.get("o");
                    System.out.println(" { " + s + " " + p + " " + o + " . }");
                }

System.out.println("\nexecute: DELETE FROM GRAPH <http://test1> { <aa> <bb> 'cc' }");
                str = "DELETE FROM GRAPH <http://test1> { <aa> <bb> 'cc' }";
                vur = VirtuosoUpdateFactory.create(str, set);
                vur.exec();

System.out.println("\nexecute: SELECT * FROM <http://test1> WHERE { ?s ?p ?o }");
                vqe = VirtuosoQueryExecutionFactory.create (sparql, set);
                results = vqe.execSelect();
                while (results.hasNext()) {
                        QuerySolution rs = results.nextSolution();
                    RDFNode s = rs.get("s");
                    RDFNode p = rs.get("p");
                    RDFNode o = rs.get("o");
                    System.out.println(" { " + s + " " + p + " " + o + " . }");
                }

        }
}
VirtJenaSPARQLExample9
import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.Graph;
import com.hp.hpl.jena.rdf.model.*;
import java.util.Iterator;

import virtuoso.jena.driver.*;

public class VirtuosoSPARQLExample9 {

        /**
         * Executes a SPARQL query against a virtuoso url and prints results.
         */
        public static void main(String[] args) {

                String url;
                if(args.length == 0)
                    url = "jdbc:virtuoso://localhost:1111";
                else
                    url = args[0];

/*                      STEP 1                  */
                VirtGraph set = new VirtGraph (url, "dba", "dba");

/*                      STEP 2                  */
                String str = "CLEAR GRAPH <http://test1>";
                VirtuosoUpdateRequest vur = VirtuosoUpdateFactory.create(str, set);
                vur.exec();

                str = "INSERT INTO GRAPH <http://test1> { <http://aa> <http://bb> 'cc' . <http://aa1> <http://bb> 123. }";
                vur = VirtuosoUpdateFactory.create(str, set);
                vur.exec();

/*              Select all data in virtuoso     */
                Query sparql = QueryFactory.create("SELECT * FROM <http://test1> WHERE { ?s ?p ?o }");
                VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create (sparql, set);
                ResultSet results = vqe.execSelect();
                System.out.println("\nSELECT results:");
                while (results.hasNext()) {
                        QuerySolution rs = results.nextSolution();
                    RDFNode s = rs.get("s");
                    RDFNode p = rs.get("p");
                    RDFNode o = rs.get("o");
                    System.out.println(" { " + s + " " + p + " " + o + " . }");
                }

                sparql = QueryFactory.create("DESCRIBE <http://aa> FROM <http://test1>");
                vqe = VirtuosoQueryExecutionFactory.create (sparql, set);

                Model model = vqe.execDescribe();
                Graph g = model.getGraph();
                System.out.println("\nDESCRIBE results:");
                for (Iterator i = g.find(Node.ANY, Node.ANY, Node.ANY); i.hasNext();)
                   {
                      Triple t = (Triple)i.next();
                      System.out.println(" { " + t.getSubject() + " " +
                                                 t.getPredicate() + " " +
                                                 t.getObject() + " . }");
                }

                sparql = QueryFactory.create("CONSTRUCT { ?x <http://test> ?y } FROM <http://test1> WHERE { ?x <http://bb> ?y }");
                vqe = VirtuosoQueryExecutionFactory.create (sparql, set);

                model = vqe.execConstruct();
                g = model.getGraph();
                System.out.println("\nCONSTRUCT results:");
                for (Iterator i = g.find(Node.ANY, Node.ANY, Node.ANY); i.hasNext();)
                   {
                      Triple t = (Triple)i.next();
                      System.out.println(" { " + t.getSubject() + " " +
                                                 t.getPredicate() + " " +
                                                 t.getObject() + " . }");
                }

                sparql = QueryFactory.create("ASK FROM <http://test1> WHERE { <http://aa> <http://bb> ?y }");
                vqe = VirtuosoQueryExecutionFactory.create (sparql, set);

                boolean res = vqe.execAsk();
                System.out.println("\nASK results: "+res);

        }
}

Javadoc API Documentation

Javadocs covers the complete set of classes, interfaces, and methods implemented by the provider: