简体   繁体   English

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

[英]How to append results from multiple SPARQL queries and write them into RDF

I want to append results from multiple SPARQL queries and write them into RDF. 我想附加来自多个SPARQL查询的结果并将其写入RDF。 I used Jena API. 我使用了耶拿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);

You mentioned in a comment that you can't use union . 您在评论中提到不能使用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; 但是,将ResultSet写入RDF并没有多大意义,因为ResultSet不包含三元组。 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. (但是,有一个基于RDF的结果集表示形式,如果需要, 可以将其组合。)如果要编写RDF,则需要使用查询来生成RDF,因此您可能想使用construct查询,因为这样可以为您提供Model With a model, you can combine the results of the queries and write them out as a single RDF graph. 使用模型,您可以合并查询结果并将其作为单个RDF图写出。 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM