如何附加多个SPARQL查询的结果并将其写入RDF

时间:2013-12-18 21:18:50

标签: java rdf sparql jena

我想追加多个SPARQL查询的结果并将它们写入RDF。我用过Jena API。我试过这样的事情,但没能成功。非常感谢任何解决方案。

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);

1 个答案:

答案 0 :(得分:1)

您在评论中提到您无法使用union。目前还没有明确的为什么你不能,但如果你只有两个简单的查询,这将是最简单的方法。不过,这不是唯一的方式,所以你仍然很幸运。

将ResultSet写入RDF没有多大意义,因为ResultSet不包含三元组;它包含变量绑定。 (但是,有一个基于RDF的结果集表示,如果你想要可以组合它们。)如果你想编写RDF,你需要你的查询来生成RDF,所以你可能想要使用construct查询,因为这将为您提供Model。使用模型,您可以组合查询的结果并将其作为单个RDF图表写出。例如,您可以使用以下代码。

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" );
    }
}

这会产生以下输出:

<!-- 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>