从jena

时间:2016-07-28 15:01:40

标签: java sparql rdf jena

我正在运行查询并将结果保存到JSON文件中。我正在加载json文件作为模型,并尝试检索主题,对象和谓词列表。但是我收到以下错误:

  

线程“main”中的异常org.apache.jena.riot.RiotException:[line:   2,col:3]相对IRI:头部   org.apache.jena.riot.system.ErrorHandlerFactory $ ErrorHandlerStd.error(ErrorHandlerFactory.java:128)     在   org.apache.jena.riot.checker.CheckerIRI.iriViolations(CheckerIRI.java:138)     在   org.apache.jena.riot.checker.CheckerIRI.iriViolations(CheckerIRI.java:100)     在   org.apache.jena.riot.system.ParserProfileChecker.makeIRI(ParserProfileChecker.java:66)     在   org.apache.jena.riot.system.ParserProfileChecker.resolveIRI(ParserProfileChecker.java:59)     在   org.apache.jena.riot.system.ParserProfileChecker.createURI(ParserProfileChecker.java:112)     在   org.apache.jena.riot.lang.LangRDFJSON.tryParseTriples(LangRDFJSON.java:121)     在   org.apache.jena.riot.lang.LangRDFJSON.tryParseGraph(LangRDFJSON.java:68)     在   org.apache.jena.riot.lang.LangRDFJSON.runParser(LangRDFJSON.java:56)     在org.apache.jena.riot.lang.LangBase.parse(LangBase.java:42)at   org.apache.jena.riot.RDFParserRegistry $ ReaderRIOTLang.read(RDFParserRegistry.java:176)     在org.apache.jena.riot.RDFDataMgr.process(RDFDataMgr.java:861)at   org.apache.jena.riot.RDFDataMgr.parse(RDFDataMgr.java:667)at at   org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:212)at   org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:145)at   org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:134)at   org.apache.jena.riot.adapters.AdapterFileManager.readModelWorker(AdapterFileManager.java:282)     在   org.apache.jena.util.FileManager.loadModelWorker(FileManager.java:305)     at org.apache.jena.util.FileManager.loadModel(FileManager.java:292)     在Main.main(Main.java:51)

我的代码是:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.util.FileManager;

public class Main {

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

        String queryString = "PREFIX owl: <http://www.w3.org/2002/07/owl#>"
                + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
                + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
                + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
                + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "PREFIX dc: <http://purl.org/dc/elements/1.1/>"
                + "PREFIX dbr: <http://dbpedia.org/resource/>" + "PREFIX dbpedia2: <http://dbpedia.org/property/>"
                + "PREFIX dbpedia: <http://dbpedia.org/>" + "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>"
                + "PREFIX dbo: <http://dbpedia.org/ontology/>" + "select distinct ?p ?o where {"
                + "dbr:Cristiano_Ronaldo ?p ?o " + "filter(langMatches(lang(?o),'en'))" +

        "}";
        Query query = QueryFactory.create(queryString);
        QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);
        ResultSet results = qexec.execSelect();
        FileOutputStream oFile;
        try {
            oFile = new FileOutputStream("output4.json", false);
            ResultSetFormatter.outputAsJSON(oFile, results);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        qexec.close();

        FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
        Model model = FileManager.get().loadModel("output4.json", null, "RDF/JSON");

        StmtIterator iter = model.listStatements();
        try {
            while (iter.hasNext()) {
                Statement stmt = iter.next();
                Resource s = stmt.getSubject();
                Resource p = stmt.getPredicate();
                RDFNode o = stmt.getObject();
                System.out.println(s.toString());
                System.out.println(p.toString());
                System.out.println(o.toString());
            }
        } finally {
            if (iter != null)
                iter.close();
        }

    }

}

根据AKSW的建议,我尝试使用构造查询:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ResultSetFactory;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.query.ResultSetRewindable;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.rdf.model.Statement;
import org.apache.jena.rdf.model.StmtIterator;
import org.apache.jena.util.FileManager;

public class Main {

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

        String queryString = "PREFIX owl: <http://www.w3.org/2002/07/owl#>"
                + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>"
                + "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>"
                + "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
                + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "PREFIX dc: <http://purl.org/dc/elements/1.1/>"
                + "PREFIX dbr: <http://dbpedia.org/resource/>" + "PREFIX dbpedia2: <http://dbpedia.org/property/>"
                + "PREFIX dbpedia: <http://dbpedia.org/>" + "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>"
                + "PREFIX dbo: <http://dbpedia.org/ontology/>" + "select distinct ?p ?o where {"
                + "dbr:Cristiano_Ronaldo ?p ?o " + "filter(langMatches(lang(?o),'en'))" +

        "}";
        Query query = QueryFactory.create(queryString);
        QueryExecution qexec = QueryExecutionFactory.sparqlService("http://dbpedia.org/sparql", query);


        FileOutputStream oFile;
        try {
            oFile = new FileOutputStream("output4.ttl", false);
            Model result21 = qexec.execConstruct();
            result21.write(oFile, "TURTLE");

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        qexec.close();
        FileManager.get().addLocatorClassLoader(Main.class.getClassLoader());
        Model model = FileManager.get().loadModel("output4.ttl", null, "TURTLE");

        StmtIterator iter = model.listStatements();
        try {
            while (iter.hasNext()) {
                Statement stmt = iter.next();
                Resource s = stmt.getSubject();
                Resource p = stmt.getPredicate();
                RDFNode o = stmt.getObject();
                System.out.println(s.toString());
                System.out.println(p.toString());
                System.out.println(o.toString());
            }
        } finally {
            if (iter != null)
                iter.close();
        }

    }

}

打印结果很奇怪,并没有正确地提供主题,对象和谓词。 以下是控制台中打印的响应: result

1 个答案:

答案 0 :(得分:0)

问题是您正在运行SELECT查询,该查询会返回结果集。此结果集并不代表您可能期望的RDF三元组。特别是因为您只选择seq entity value_date ---------- ---------- ---------- 1580 12001 31/05/2016 1579 12001 30/06/2016 1578 15003 30/06/2016 1577 12001 30/06/2016 1576 15003 30/06/2016 1575 15003 30/06/2016 1574 12004 30/06/2016 1573 15003 31/05/2016 1572 12004 31/05/2016 1571 67677 30/04/2016 p,因此主题将会丢失。然后将结果集保存为JSON,有关格式的说明,请参阅https://www.w3.org/TR/rdf-sparql-json-res/。因此,JSON文件中没有可以加载到JENA模型中的三元组。

解决方案是使用SPARQL CONSTRUCT代替。查询可以如下:

o

此外,您必须在Jena API中调整方法调用:

PREFIX dbr: <http://dbpedia.org/resource/> 
CONSTRUCT {
   dbr:Cristiano_Ronaldo ?p ?o .
} WHERE {
   dbr:Cristiano_Ronaldo ?p ?o . 
   FILTER(LANGMATCHES(LANG(?o),'en'))
}