Construct and Java - Part 2

In Part 1 of this series of posts, I described how to add data to Construct. This post will teach you how to get data back out again using the Query Service.

The Java libraries bundled with Construct make use Jena, and the SPARQL query language in the process of querying Construct. If you’re already familiar with Jena, then there is only a couple of additional steps to learn.

Working with Jena and SPARQL

(download the sample code)

To start, we need to open a connection to the Query Service. This is done in the same way as a connection to the Data Port, by creating a new instance of the appropriate class.

  1. final QueryServiceProxy qsProxy = new QueryServiceProxy()

The next step is to create the SPARQL query. In this example, we wish to query for the name property of the subject http://example.com/people/bob that we created in Part 1.

  1. final String query = “SELECT ?name WHERE{
  2. <http://example.com/people/bob>
  3. <http://example.com/terms#name>
  4. ?name}”;

Then we call the query() method of the proxy object, passing the query as an argument. The return value is a Jena ResultSet object corresponding to the result of the query.

  1. final ResultSet resultSet = qsProxy.query(query);

What you do with the result set is obviously dependent on your need for the data. For the purposes of this example, we’ll print the result to the console.

  1. if (resultSet.hasNext()) {
  2. final QuerySolution solution = resultSet.nextSolution();
  3. final Literal name = solution.getLiteral(“name”);
  4. System.out.println(name.toString());
  5. }

Finally, we close the connection to the query service.

  1. qsProxy.close();

That’s the basics of querying data with Construct.

0 Responses to “Construct and Java - Part 2”


  1. No Comments

Leave a Reply