繁体   English   中英

如何在耶拿中使用sparql查询rdf文件

[英]how to query rdf file with sparql in jena

我想在耶拿使用sparql查询rdf文件。 rdf文件如下:


<?xml version="1.0"?>
<rdf:RDF xmlns="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#"
     xml:base="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73"
     xmlns:swp2="http://semwebprogramming.org/2009/ont/chp2#"
     xmlns:untitled-ontology-73="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#"
     xmlns:protege="http://protege.stanford.edu/plugins/owl/protege#"
     xmlns:foaf="http://xmlns.com/foaf/0.1/"
     xmlns:xsp="http://www.owl-ontologies.com/2005/08/07/xsp.owl#"
     xmlns:ns66675="http://www.w3.org/2004/02/skos/core#"
     xmlns:cmp="http://ld.thomsonreuters.com/ContentMarketplace/2013-10-15/#"
     xmlns:ns36343="http://bioportal.bioontology.org/ontologies/umls/"
     xmlns:Ontology1395931632451="http://www.semanticweb.org/ontologies/2014/2/Ontology1395931632451.owl#"
     xmlns:ns20477="http://www.w3.org/2000/01/rdf-schema#"
     xmlns:xml="http://www.w3.org/XML/1998/namespace"
     xmlns:ns28717="http://purl.bioontology.org/ontology/HL7/"
     xmlns:ns91299="http://www.w3.org/2002/07/owl#"
     xmlns:schema="http://schema.org/"
     xmlns:swrl="http://www.w3.org/2003/11/swrl#"
     xmlns:owl2xml="http://www.w3.org/2006/12/owl2-xml#"
     xmlns:dct="http://purl.org/dc/terms/"
     xmlns:admin="http://webn.net/mvcb/"
     xmlns:mds="http://ld.thomsonreuters.com/MetadataSystems/2013-10-15/#"
     xmlns:swrlb="http://www.w3.org/2003/11/swrlb#"
     xmlns:base="http://ld.thomsonreuters.com/MetadataSystems/2013-10-15/"
     xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
    <ns91299:Ontology rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73"/>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Object Properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knowns -->

    <ns91299:ObjectProperty rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knowns">
        <rdf:type rdf:resource="http://www.w3.org/2002/07/owl#ReflexiveProperty"/>
        <ns20477:range rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <ns20477:domain rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
    </ns91299:ObjectProperty>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Data properties
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#name -->

    <ns91299:DatatypeProperty rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#name">
        <ns20477:domain rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <ns20477:range rdf:resource="http://www.w3.org/2001/XMLSchema#string"/>
    </ns91299:DatatypeProperty>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Classes
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person -->

    <ns91299:Class rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person">
        <ns20477:subClassOf>
            <ns91299:Restriction>
                <ns91299:onProperty rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knowns"/>
                <ns91299:someValuesFrom rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
            </ns91299:Restriction>
        </ns20477:subClassOf>
    </ns91299:Class>



    <!-- 
    ///////////////////////////////////////////////////////////////////////////////////////
    //
    // Individuals
    //
    ///////////////////////////////////////////////////////////////////////////////////////
     -->




    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#amina -->

    <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#amina">
        <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <name>amina</name>
        <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#george"/>
        <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#john"/>
    </ns91299:NamedIndividual>



    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#george -->

    <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#george">
        <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <name>george</name>
        <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#amina"/>
        <knowns rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#nacira"/>
    </ns91299:NamedIndividual>



    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#john -->

    <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#john">
        <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <name>john</name>
    </ns91299:NamedIndividual>



    <!-- http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#nacira -->

    <ns91299:NamedIndividual rdf:about="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#nacira">
        <rdf:type rdf:resource="http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#person"/>
        <name>nacira</name>
    </ns91299:NamedIndividual>
</rdf:RDF>



<!-- Generated by the OWL API (version 3.4.5-SNAPSHOT) http://owlapi.sourceforge.net -->

Java代码如下:

package firstProject;

import java.util.Iterator;
//import com.hp.hpl.jena.ontology.OntModel;
//import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;

public class main {
    public static void main(String[] args) {
        // TODO Auto-generated method stub

    sparqltest();}

static void sparqltest()
{

    FileManager.get().addLocatorClassLoader(main.class.getClassLoader());
    Model model= FileManager.get().loadModel("C:/Users/acer/workspace/firstProject/src/firstProject/onto1.rdf");

String queryString=
       "PREFIX rdf:<http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#>"+
             "SELECT * WHERE {"+
      "?person foaf:name ?x. "+
          "?person foaf:knows ?person2."+
          "?person2 foaf:name ?y."+
       "FILTER( ?y = \"john\")"+
       "}";
Query query= QueryFactory.create(queryString);
QueryExecution qexec=QueryExecutionFactory.create(query, model);
try {
    ResultSet results = qexec.execSelect();while ( results.hasNext()){
        QuerySolution soln = results.nextSolution();
        Literal name = soln.getLiteral("x");
        System.out.println(name);
    }
} finally {
qexec.close();
    }}}

我无法执行此查询,因为有很多错误:log4j:WARN找不到记录器(org.apache.jena.riot.stream.JenaIOEnvironment)的附加程序。 log4j:WARN请正确初始化log4j系统。 log4j:WARN有关更多信息,请参见http://logging.apache.org/log4j/1.2/faq.html#noconfig

我希望有人能帮助我。

谢谢?

在您的RDF文件中, knows属性使用默认名称空间,该名称空间在该文件的前缀声明中设置为http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73# 但是,在查询中,您使用的是foaf:knows属性。 在示例代码中,该前缀未在查询中声明(因此该查询不正确),但是我在注释中看到您稍后为其添加了前缀声明(提示:更新您的问题)。

因此,您在数据库中拥有属性http://www.semanticweb.org/acer/ontologies/2014/2/untitled-ontology-73#knows ,但您查询的是http://xmlns.com/foaf/0.1/knows 看到问题了吗? 一切正常,您只是在查询错误的属性。

从更一般的角度来看:仅从Web复制粘贴代码并不是真正学习如何使用这些工具的最佳方法。 我建议您通过其中一本Jena教程来学习,该教程可以教您更多有关如何和为什么的知识。 从长远来看,这将使您的生活更加轻松。

暂无
暂无

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

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