Question

I have a SPARQL/XML results stream (happens to be coming back from a Virtuoso server), and I would like to process the results as they stream back from the http connection.

Sesame has a very nice library for doing this - assuming you are speaking full Sesame it's easy and I have that implemented. But instead of having Sesame generate the request I am doing it over a different HTTP connection that doesn't speak full Sesame, and I don't know of an immediate way to make the Sesame library do that.

With Sesame you can use a RepositoryConnection create a TupleQuery with .prepareTupleQuery and then pass .evaluate a solution handler (something that extends TupleQueryResultHandlerBase).

I'm assuming there are some classes under the hood where I could pass a solution handler and an InputStream to and let the Sesame classes take care of the parsing, buffering, and making the callback etc. Certainly the code is in there, but I've been wading through the source for hours and seem like I'm going in circles.

If there is another library or solution that fine too I'll gladly take the pointer, but I thought I would just take advantage of my other Sesame-related code.

Was it helpful?

Solution

You can use Sesame's QueryResultIO utility for this. Simplest way is to just let it parse the stream into a query result object. Like so:

InputStream in = ...;
TupleQueryResult result = QueryResultIO.parse(in, TupleQueryResultFormat.SPARQL);

The above is hardwired to create a result object. If you want more control over the processing, you can create a TupleQueryResultParser object and assign it your own TupleQueryResultHandler implementation. Something like this:

TupleQueryResultParser parser = QueryResultIO.createParser(TupleQueryResultFormat.SPARQL);

TupleQueryResultHandler handler = new MyCustomStreamingResultHandler();
parser.setQueryResultHandler(handler);

parser.parseQueryResult(in); 

Or like this, if you're working with an older version of Sesame (2.6 or older):

TupleQueryResultHandler handler = new MyCustomStreamingResultHandler();
parser.setTupleQueryResultHandler(handler);

parser.parse(in); 

Sesame provides several stock implementation of TupleQueryResultHandler, but it's very straightforward to create your own as well if you wish to have full control over the parser output processing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top