2014-03-30 54 views
0

我想用jena中的sparql查询rdf文件。 RDF文件是如下:如何在jena中用sparql查询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代码如下: 的log4j:

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

东阳存在很多错误,我不能执行这个查询: WARN记录器(org.apache.jena.riot.stream.JenaIOEnvironment)找不到appender。 log4j:WARN请正确初始化log4j系统。 log4j:警告有关更多信息,请参见http://logging.apache.org/log4j/1.2/faq.html#noconfig

我希望有人能帮助我做到这一点。

Thx?

+0

您忘记包含您的代码和您的查询。 –

+1

为什么没有指定前缀的第二个'PREFIX'? –

+0

我在网上发现了这段代码,但它不工作! – Nanis

回答

3

在您的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。看到问题了吗?一切正常,你只是查询错误的属性。

在更广泛的注意:只需从网站是不是真的要学习如何使用这些工具的最佳方式复制粘贴代码。我建议你按照Jena的一个教程来学习,教你一些关于如何和为什么的信息。这将使你的生活在更长的时间内更容易。

+0

感谢您的答案,您是对的。我用rdf 替换了foaf“PREFIX rdf:”+ //“PREFIX foaf:"+ “SELECT * WHERE {”+ “?rdf:name?x”+ \t \t “}”; – Nanis

+0

但为什么这个查询不返回任何结果?“SELECT * WHERE {” + “人RDF:名X? ”+ \t \t // “人RDF:知道PERSON2?” + \t \t //“ PERSON2 RDF:姓名Y?” + //“FILTER(?y = \”john \“)”+ “}”; – Nanis

+1

因为你已经定义了rdf:与数据不同。 – AndyS