简体   繁体   English

从耶拿模型中检索项目

[英]Retrieve items from a model in jena

I am running a query and saving the result into a JSON file. 我正在运行查询并将结果保存到JSON文件中。 I am loading the json file as a model and trying to retrieve the list of subjects, objects and predicates. 我正在将json文件作为模型加载,并尝试检索主题,对象和谓词的列表。 However I am getting the following error: 但是我收到以下错误:

Exception in thread "main" org.apache.jena.riot.RiotException: [line: 2, col: 3 ] Relative IRI: head at org.apache.jena.riot.system.ErrorHandlerFactory$ErrorHandlerStd.error(ErrorHandlerFactory.java:128) at org.apache.jena.riot.checker.CheckerIRI.iriViolations(CheckerIRI.java:138) at org.apache.jena.riot.checker.CheckerIRI.iriViolations(CheckerIRI.java:100) at org.apache.jena.riot.system.ParserProfileChecker.makeIRI(ParserProfileChecker.java:66) at org.apache.jena.riot.system.ParserProfileChecker.resolveIRI(ParserProfileChecker.java:59) at org.apache.jena.riot.system.ParserProfileChecker.createURI(ParserProfileChecker.java:112) at org.apache.jena.riot.lang.LangRDFJSON.tryParseTriples(LangRDFJSON.java:121) at org.apache.jena.riot.lang.LangRDFJSON.tryParseGraph(LangRDFJSON.java:68) at org.apache.jena.riot.lang.LangRDFJSON.runParser(LangRDFJSON.java:56) at org.apache.jena.riot.lang.LangBase.parse(LangBase.java:42) at org.apache.jena.riot.RDFParserRegistry$ReaderRIOTLang.read(RDFParserRegi 线程“主”中的异常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 org.apache.jena.riot.system.ParserProfileChecker.resolveIRI(ParserProfileChecker.java:59)的org.apache.jena.riot.system.ParserProfileChecker.createURI的.riot.system.ParserProfileChecker.makeIRI(ParserProfileChecker.java:66) (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.LangBase.parse(LangBase.java:42)上的org.apache.jena.riot.lang.LangRDFJSON.runParser(LangRDFJSON.java:56)在org.apache.jena.riot上。 RDFParserRegistry $ ReaderRIOTLang.read(RDFParserRegi stry.java:176) at org.apache.jena.riot.RDFDataMgr.process(RDFDataMgr.java:861) at org.apache.jena.riot.RDFDataMgr.parse(RDFDataMgr.java:667) 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) at org.apache.jena.util.FileManager.loadModelWorker(FileManager.java:305) at org.apache.jena.util.FileManager.loadModel(FileManager.java:292) at Main.main(Main.java:51) stry.java:176)位于org.apache.jena.riot.RDFDataMgr.process(RDFDataMgr.java:861)位于org.apache.jena.riot.RDFDataMgr.parse(RDFDataMgr.java:667)位于org.apache.jena org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:145)上的.riot.RDFDataMgr.read(RDFDataMgr.java:212)在org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:134) )于org.apache.jena.util的org.apache.jena.util.FileManager.loadModelWorker(FileManager.java:305)的org.apache.jena.riot.adapters.AdapterFileManager.readModelWorker(AdapterFileManager.java:282)处。 Main.main(Main.java:51)上的FileManager.loadModel(FileManager.java:292)

My code is: 我的代码是:

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

    }

}

As per AKSW suggestion I have tried it with the construct query: 根据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();
        }

    }

}

The printed results are weird and doesn't provide the subjects, objects and predicates properly. 打印的结果很奇怪,不能正确提供主题,宾语和谓词。 Below is the response printed in the console: result 以下是控制台中打印的响应: 结果

The problem is that you're running a SELECT query which gives you back a resultset. 问题是您正在运行SELECT查询,该查询将为您提供结果集。 This resultset does not represent the RDF triples as you probably expect. 该结果集并不代表您可能期望的RDF三元组。 Especially since you only select p and o , thus, the subject would be missing. 特别是由于仅选择po ,因此将丢失主题。 Then you save that resultset as JSON, see https://www.w3.org/TR/rdf-sparql-json-res/ for a description of the format. 然后将该结果集另存为JSON,有关格式的说明,请参见https://www.w3.org/TR/rdf-sparql-json-res/ As a consequence, there are no triples in the JSON file that you can load into a JENA model back. 因此,JSON文件中没有三元组,您可以将其加载回JENA模型。

The solution would be to use SPARQL CONSTRUCT instead. 解决方案是改用SPARQL CONSTRUCT The query could be as follows: 查询可以如下:

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

In addition, you have to adapt the method call in the Jena API: 另外,您必须调整Jena API中的方法调用:

Model model = qexec.execConstruct(); // instead of execSelect();

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

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