Reputation: 67
I want to append results from multiple SPARQL queries and write them into RDF. I used Jena API. I tried something like this but couldn't succeed. Thanks a lot for any solution.
String query1 = {?s ?p ?o.}
String query2 = {?s1 ?p1 ?o1.}
Query query = QueryFactory.create(query1);
QueryExecution qexec = QueryExecutionFactory.sparqlService("SPARQLEndpoint", query);
ResultSet results = qexec.execSelect();
ResultSetFormatter.out(System.out, results,query) ;
ResultSetFormatter.asRDF(model, results);
Upvotes: 1
Views: 1487
Reputation: 85913
You mentioned in a comment that you can't use union
. It's not immediately clear why you can't, but if you just have two simple queries that would be the easiest way to do this. It's not the only way, though, so you're still in luck.
It doesn't make much sense to write a ResultSet to RDF though, since a ResultSet doesn't contain triples; it contains variable bindings. (There is an RDF based representation of result sets, though, and you could combine those if you wanted to.) If you want to write RDF, you'll need your queries to produce RDF, so you probably want to use a construct
query, as that will give you a Model
back. With a model, you can combine the results of the queries and write them out as a single RDF graph. For instance, you could use code the following.
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
public class AppendSPARQLQueryResults {
public static void main(String[] args) {
// This is the model that we'll store all the results in. We'll
// add some prefixes just to make the output a little nicer.
Model results = ModelFactory.createDefaultModel();
results.setNsPrefix( "dbpedia", "http://dbpedia.org/resource/" );
results.setNsPrefix( "dbpedia-owl", "http://dbpedia.org/ontology/" );
results.setNsPrefix( "schema", "http://schema.org/" );
// Two queries to run
String[] queries = {
"construct where { <http://dbpedia.org/resource/Mount_Monadnock> a ?type } limit 5",
"construct where { <http://dbpedia.org/resource/Mount_Monadnock> <http://dbpedia.org/ontology/locatedInArea> ?place } limit 5"
};
// Run each query, then show its individual results, and add
// them to the combined model
for ( String query : queries ) {
Model result = QueryExecutionFactory.sparqlService( "http://dbpedia.org/sparql", query ).execConstruct();
System.out.println( "\n<!-- results of: " +query+" -->" );
result.write( System.out, "RDF/XML-ABBREV" );
results.add( result );
}
// Show the combined results
System.out.println( "\n<!-- combined results -->" );
results.write( System.out, "RDF/XML-ABBREV" );
}
}
This produces the following output:
<!-- results of: construct where { <http://dbpedia.org/resource/Mount_Monadnock> a ?type } limit 5 -->
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:j.1="http://schema.org/"
xmlns:j.0="http://dbpedia.org/ontology/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Thing rdf:about="http://dbpedia.org/resource/Mount_Monadnock">
<rdf:type rdf:resource="http://dbpedia.org/ontology/Mountain"/>
<rdf:type rdf:resource="http://dbpedia.org/ontology/Place"/>
<rdf:type rdf:resource="http://schema.org/Mountain"/>
<rdf:type rdf:resource="http://dbpedia.org/ontology/NaturalPlace"/>
</owl:Thing>
</rdf:RDF>
<!-- results of: construct where { <http://dbpedia.org/resource/Mount_Monadnock> <http://dbpedia.org/ontology/locatedInArea> ?place } limit 5 -->
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dbpedia-owl="http://dbpedia.org/ontology/"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<rdf:Description rdf:about="http://dbpedia.org/resource/Mount_Monadnock">
<dbpedia-owl:locatedInArea rdf:resource="http://dbpedia.org/resource/United_States"/>
<dbpedia-owl:locatedInArea rdf:resource="http://dbpedia.org/resource/Dublin,_New_Hampshire"/>
<dbpedia-owl:locatedInArea rdf:resource="http://dbpedia.org/resource/New_Hampshire"/>
<dbpedia-owl:locatedInArea rdf:resource="http://dbpedia.org/resource/Cheshire_County,_New_Hampshire"/>
<dbpedia-owl:locatedInArea rdf:resource="http://dbpedia.org/resource/Jaffrey,_New_Hampshire"/>
</rdf:Description>
</rdf:RDF>
<!-- combined results -->
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:dbpedia="http://dbpedia.org/resource/"
xmlns:schema="http://schema.org/"
xmlns:dbpedia-owl="http://dbpedia.org/ontology/">
<schema:Mountain rdf:about="http://dbpedia.org/resource/Mount_Monadnock">
<dbpedia-owl:locatedInArea rdf:resource="http://dbpedia.org/resource/Dublin,_New_Hampshire"/>
<dbpedia-owl:locatedInArea rdf:resource="http://dbpedia.org/resource/New_Hampshire"/>
<rdf:type rdf:resource="http://www.w3.org/2002/07/owl#Thing"/>
<rdf:type rdf:resource="http://dbpedia.org/ontology/Mountain"/>
<dbpedia-owl:locatedInArea rdf:resource="http://dbpedia.org/resource/Jaffrey,_New_Hampshire"/>
<dbpedia-owl:locatedInArea rdf:resource="http://dbpedia.org/resource/Cheshire_County,_New_Hampshire"/>
<rdf:type rdf:resource="http://dbpedia.org/ontology/Place"/>
<rdf:type rdf:resource="http://dbpedia.org/ontology/NaturalPlace"/>
<dbpedia-owl:locatedInArea rdf:resource="http://dbpedia.org/resource/United_States"/>
</schema:Mountain>
</rdf:RDF>
Upvotes: 1