java - 从 jena QuerySolution 到 Model tdb rdf

标签 java rdf jena dbpedia

我使用 dbpedia 获取一些特定的结果,我想将它们存储在本地 tdb 中。但我找不到将结果添加到本地 tdb 的方法。

package festifolk.tdb;

import com.hp.hpl.jena.query.*;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.tdb.TDBFactory;
import com.hp.hpl.jena.update.*;
import org.apache.jena.atlas.lib.StrUtils;

public class Remote {

    public static ResultSet[] Query() {
        /* Remote location of the Data set used in building our tdb */
        String service = "http://dbpedia.org/sparql";

        /* Declaration of the SPARQL prefixes used */
        String owl = "PREFIX owl: <http://www.w3.org/2002/07/owl#>";
        String xsd = "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>";
        String rdfs = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>";
        String rdf = "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>";
        String foaf = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>";
        String dc = "PREFIX dc: <http://purl.org/dc/elements/1.1/>";
        String res = "PREFIX res: <http://dbpedia.org/resource/>";
        String ont = "PREFIX ont: <http://dbpedia.org/ontology/>";
        String dbpedia2 = "PREFIX dbpedia2: <http://dbpedia.org/property/>";
        String dbpedia = "PREFIX dbpedia: <http://dbpedia.org/>";
        String skos = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#>";

        String prefixes = owl + xsd + rdfs + rdf + foaf + dc + res + ont + dbpedia2
                + dbpedia + skos;

        String[] URI = {
            "<http://dbpedia.org/class/yago/MusicFestivalsInTheNetherlands>",
            "<http://dbpedia.org/class/yago/MusicFestivalsInBelgium>",
            "<http://dbpedia.org/class/yago/MusicFestivalsInFrance>",
            "<http://dbpedia.org/class/yago/MusicFestivalsInGermany>",
            "<http://dbpedia.org/class/yago/MusicFestivalsInEngland>"
        };

        String query;
        QueryExecution queryExec;
        ResultSet[] results = new ResultSet[URI.length];

        for (int i = 0; i < URI.length; i++) {
            query = prefixes
                    + "SELECT * WHERE { "
                    + "?festival rdf:type " + URI[i] + ". "
                    + "?festival rdfs:label ?label. "
                    + "?festival ont:abstract ?abstract. "
                    + "OPTIONAL{?festival foaf:homepage ?homepage}. "
                    + "} ";

            /* execute the query and save the result */
            queryExec = QueryExecutionFactory.sparqlService(service, query);
            try {
                results[i] = queryExec.execSelect();
            } catch (Exception e) {
                System.out.println(e);
            } finally {
                queryExec.close();
            }
        }

        return results;
    }

    public static void storeLocally(ResultSet[] results, Dataset dataset) {
        /* Open the provided dataset and begin the writing procedure */
        dataset.begin(ReadWrite.WRITE);

        try {

            /* API Calls to a model in the dataset */
            Model model = dataset.getDefaultModel();

            /* ADD all the results to the database */
            for (ResultSet resultSet: results) {
                while (resultSet.hasNext()) {
                    model.add(resultSet.nextSolution())
                }
            }

            /* ... perform a SPARQL Update */
            GraphStore graphStore = GraphStoreFactory.create(dataset);
            String sparqlUpdateString = StrUtils.strjoinNL(
                    "PREFIX . <http://example/>",
                    "INSERT { :s :p ?now } WHERE { BIND(now() AS ?now) }");

            UpdateRequest request = UpdateFactory.create(sparqlUpdateString);
            UpdateProcessor proc = UpdateExecutionFactory.create(request, graphStore);
            proc.execute();

            /* Finally, commit the transaction */
            dataset.commit();
        } catch (Exception ex) {
            System.out.println(ex);
        } finally {
            dataset.end();
        }
    }

    public static void main(String[] args)
    {
        /* Direct way: Make a TDB-backed dataset */
        String directory = "tdb";
        Dataset dataset = TDBFactory.createDataset(directory);
        storeLocally(Query(), dataset);
    }
}

上面的代码显示了我想做的事情,但显然它不起作用,因为不能将 QuerySolution 添加到模型中。

谁能帮我解决这个问题?

编辑:添加了完整的类(class)。问题是关于 storeLocally void 部分。

最佳答案

与其使用 SELECT 查询,不如使用 CONSTRUCT查询,CONSTRUCT 查询返回一个 RDF 图,因此在 Jena 中是 Model

的一个实例

然后要将其存储在本地,您可以根据需要简单地调用 dataset.setDefaultModel()dataset.addNamedModel() 将返回的模型添加到本地 TDB 数据集。

关于java - 从 jena QuerySolution 到 Model tdb rdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15704905/

相关文章:

java - 将 RDF .ttl 文件合并到一个文件数据库中 - 过滤并仅保留所需的数据/三元组

rdf - 导入 RDF : are referenced blank nodes gonna be merged or handled separately?

java - 写入嵌套rdf :Description elements in RDF/XML with Jena

java - 关于范围的一般编程问题

java - 我的 Simple ListView 应用程序正在泄漏内存。我做错了什么?

java - 将 rdfxml 转换为海龟三元组

java - 如何使用Java代码启动Fuseki服务器并使用Java代码将OWL文件上传到它?

java - Java Applet 的基本布局/结构

java - 解析 SVG - java.net.MalformedURLException : no protocol: <? xml 版本 ="1.0"编码 ="UTF-8"独立 ="no"?>

java - 内容协商 : How to serve other than the highest ranking type from accept header