Jena Fuseki 101

时间:2023-03-09 05:51:11
Jena Fuseki 101

前言

正如其承诺的那样

Expose your triples as a SPARQL end-point accessible over HTTP. Fuseki provides REST-style interaction with your RDF data.

Fuseki提供了可通过HTTP访问SPARQL终端,那么它应该实现RDF存储、CRUD等事务性操作、同时兼容W3C SPARQL 1.1建议(http://www.w3.org/TR/2012/PR-sparql11-overview-20121108/)。

本文记录Fuseki的启动、如何导入数据、在Fuseki中执行SPARQL查询和更新,以及如何以编程方式实现Fuseki中SPARQL查询和更新。

内容

1 运行Fuseki

2 准备数据

3 执行SPARQL查询

4 执行SPARQL更新

5 编程方式实现SPARQL查询和更新

6 待考察问题

1 运行Fuseki

版本jena-fuseki-1.0.1

解压到目录${FUSEKI},在该目录下运行

java -jar fuseki-server.jar –config=config-tdb.ttl

运行输出:

Jena Fuseki 101

2 准备数据

数据仍用Jena TDB API without Assembler(http://www.cnblogs.com/zhoujiagen/p/3647783.html)中的graph.owl

Fuseki Web管理界面登录(http://localhost:3030)

Jena Fuseki 101

点击Control Panel进入Fuseki服务管理,

选择数据集/data,进入Fuseki Query界面:

Jena Fuseki 101

选择文件(graph.owl)上传。

3 执行SPARQL查询

在SPARQL Qeury中输入查询语句

PREFIX rdf: -rdf-syntax-ns#>
PREFIX owl: /owl#>
PREFIX xsd: /XMLSchema#>
PREFIX rdfs: /rdf-schema#>
PREFIX graph: <http://www.nosql.com/graph#>

SELECT ?friend
    WHERE { graph:Martin graph:friend ?friend }

点击Get Results,

Jena Fuseki 101

4 执行SPARQL更新

在SPARQL Qeury中输入更新语句

PREFIX graph: <http://www.nosql.com/graph#>
INSERT DATA
{
    graph:Martin graph:friend graph:ZhouJiaGen .
}

点击Perform update后,再次执行查询:

Jena Fuseki 101

5 编程方式实现SPARQL查询和更新

下面以自说明的代码说明如何通过编程方式执行Fuseki中RDF数据的查询和更新

package arq;

import static util.Constants.NEWLINE;

import org.apache.jena.fuseki.http.UpdateRemote;

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.RDFNode;
import com.hp.hpl.jena.update.UpdateFactory;
import com.hp.hpl.jena.update.UpdateRequest;

public class SPARQLUsingFuseki {
    static final String PREFIX = "graph";
    static final String NS = "http://www.nosql.com/graph#";

    public static void main(String[] args) {
        sparqlUpdate();
    }

    @SuppressWarnings("deprecation")
    static void sparqlUpdate() {
        String update = "";
        StringBuilder sb = new StringBuilder();
        sb.append("PREFIX graph: <http://www.nosql.com/graph#>").append(NEWLINE).append("    INSERT DATA").append(NEWLINE).append("{").append(NEWLINE)
                .append("graph:Martin graph:friend  graph:ZhouJiaGen2 .").append(NEWLINE).append("}");
        update = sb.toString();
        UpdateRequest request = UpdateFactory.create(update);
        UpdateRemote.execute(request, "http://localhost:3030/data/update");
    }

    // / output:
    // http://www.nosql.com/graph#Pramod
    static void sparqlQuery() {
        // 准备SPARQL查询
        StringBuilder sb = new StringBuilder();
        sb.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>").append(NEWLINE).append("PREFIX owl: <http://www.w3.org/2002/07/owl#>")
                .append(NEWLINE).append("PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>").append(NEWLINE)
                .append("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>").append(NEWLINE).append("PREFIX foaf: <http://xmlns.com/foaf/0.1/>")
                .append(NEWLINE).append("PREFIX myfoaf: <http://blog.sina.com.cn/zhoujiagenontology/helloworld.owl#>").append(NEWLINE)
                .append("PREFIX " + PREFIX + ": " + "<" + NS + ">").append(NEWLINE);
        sb.append("SELECT ?friend WHERE { graph:Martin graph:friend ?friend }");
        String service = "http://localhost:3030/data/query";// http://localhost:3030/<<dataset>>/query
        String query = sb.toString();
        QueryExecution queryExecution = QueryExecutionFactory.sparqlService(service, query);
        sparqlHelp(queryExecution, "?friend");
    }

    static void sparqlHelp(QueryExecution queryExecution, String valueLabel) {
        ResultSet rs = queryExecution.execSelect();
        while (rs.hasNext()) {
            QuerySolution qs = rs.nextSolution();
            RDFNode name = qs.get(valueLabel);
            if (name != null) {
                System.out.println(name.toString());
            } else {
                System.out.println("Not found!");
            }
        }
        queryExecution.close();
    }
}

6 待考察问题

(1) 服务器和数据集的配置方法

(2) 如何将查询出的RDF数据与推理机结合

一种简单的思路是直接将推理后的结果存入Fuseki数据集,但这种方式无法区分断言数据和推理后数据;

另一种方法还是将Schema与数据分开,推理只对这部分查询出来的数据执行。

(3) SPARQL查询语言的语法和用例