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.
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.
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.
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.
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.
if(resultSet.hasNext()){
final QuerySolution solution = resultSet.nextSolution();
After you install Construct, the next thing you might want to do is add some data. This post will describe the basics.
The Java libraries bundled with Construct give you a couple of ways to contribute date: You can either pass RDF triples in directly, or work with Jena and submit a model when you’re ready to publish its contents.
The first step is to create a connection to the DataPort. This is achieved by creating a new instance of the DataPortProxy class.
final DataPortProxy proxy = new DataPortProxy();
Next, we call the add() method of the proxy object, passing the RDF triple as an argument. Multiple triples can be added simultaneously by separating them with a period. The return indicates whether the add operation was successful.
When using Jena, the process is very similar. As before you should begin by creating a new instance of the DataPortProxy object. Next, a new model should be created, and populated with data.
final Model model = ModelFactory.createDefaultModel();
final Resource person = model.createResource(“http://example.com/people/bob”);
final Property name = model.createProperty(“http://example.com/terms#name”);
person.addProperty(name, “Bob Smith”);
Then, the contents of the Jena model is added to Construct through another of the proxy object’s add() methods. Remember to close the connection as before when you are finished.
finalboolean response = proxy.add(model);
That’s the basics of adding data to Construct. The next post in this series will describe how to get data from Construct using the Query Service.