前言

正如其承诺的那样

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

运行输出:

2 准备数据

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

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

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

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

选择文件(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,

4 执行SPARQL更新

在SPARQL Qeury中输入更新语句

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

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

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查询语言的语法和用例

Jena Fuseki 101的更多相关文章

  1. python使用rdflib创建rdf,在jena fuseki上执行SPARQL查询

    建立并启动jena fuseki服务 参考:https://www.cnblogs.com/bincoding/p/11223372.html 使用rdflib创建rdf文件 import rdfli ...

  2. 搭建Jena Fuseki并执行SPARQL查询

    1. 下载Jena Fuseki:http://jena.apache.org/download/index.cgi 2. 运行服务 windows解压后双击fuseki-server.bat lin ...

  3. Apache Jena Fuseki使用

    下载Apache Jena Fuseki 先从apache官网下载fuseki压缩包.然后解压到目标文件夹. apache官网:http://jena.apache.org/download/ 这里我 ...

  4. Jena Fuseki 102

    Version Fuseki v1 Fuseki v2 since Jena 2.13.0 Both v1 and v2 are active and maintained.[2015/06/29] ...

  5. Jena TDB 101 Java API without Assembler

    Update on 2015/05/12 ongoing tutorials site on https://github.com/zhoujiagen/semanticWebTutorialUsin ...

  6. Jena Fuseki安装完成后不能添加数据库

    问题描述:安装Jena成功后可以进入管理页面,无法通过界面选择和查询数据 解决方案: 进入 apache-jena-fuseki-3.12.0\run 修改 shiro.ini 配置文件 注释 /$/ ...

  7. Jena+fuseki

    1.下载apache-jena-3.1.0.tar.gz,这个可以将ttl三元组文件或者xml文件加载 进入bin目录,执行./tdbloader2 --loc /path/for/database ...

  8. Outline of Apache Jena Notes

    1 description 这篇是语义网应用框架Apache Jena学习记录的索引. 初始动机见Apache Jena - A Bootstrap 2 Content 内容组织基本上遵循Jena首页 ...

  9. Jena TDB 102

    1 Introduction TDB is a RDF storage of Jena. official guarantees and limitations TDB support full ra ...

随机推荐

  1. CSS3的chapter4

    段落样式: 行高——line-height p { line-height:25px | 150% | normal;} 段落缩进——text-indent p { text-indent:2em;} ...

  2. 继续Kanzi

    转眼间,Kanzi已经发展到3.3版本了,之前研究过的东西,今天有空下了个版本跟进更新看看有没有什么变化.新的引擎跟以前2.x版本有很大的差别.新引擎增加了很多新功能(包括局部刷新技术),也跟随大潮加 ...

  3. Java开发中经典的小实例-(字符串倒序输出)

    public class Test12 {    public static void main(String[] args) {        // TODO Auto-generated meth ...

  4. Logistic回归模型和Python实现

    回归分析是研究变量之间定量关系的一种统计学方法,具有广泛的应用. Logistic回归模型 线性回归 先从线性回归模型开始,线性回归是最基本的回归模型,它使用线性函数描述两个变量之间的关系,将连续或离 ...

  5. DuiLib 自定义识别控件

    遇到一个断点无法识别自定义的控件,运气比较好,一搜就搜出来了: 参考地址:http://www.bkjia.com/ASPjc/992050.html 主要是这个函数: CControlUI* CDi ...

  6. services 文件

    Services 文件列出了服务使用的标准端口号.可以向表中添加自己定义的项,来给自己的服务选择.(安装在Windows目录下的一个子目录中,取决于Windows版本) # Copyright (c) ...

  7. 防止SVN冲突,Elipse资源同步介绍

    灰色向右箭头: 本地修改了 灰色向右箭头且中间有白色减号: 本地删除了,服务器未删除 灰色向右且中间有个加号的箭头:本地比SVN上多出的文件 蓝色向左箭头:svn上修改过 蓝色向左且中间有个加号的箭头 ...

  8. 类似qq的浮动窗口 ,随着滚轴的滚动,始终处于屏幕的中间(能看到运动的过程)

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  9. Linux运行级详解

    对于那些在DOS/Win9x/NT平台下的高级用户而言,Linux似乎是一个怪物.没有config.sys,没有 autoexec.bat,具有个人特色的机器配置不知道从何开始. 需要说明的是,很多人 ...

  10. Python--关于连接符+

    连接符 + 连接符 + 实则是创建了新的对象并占用新的内存(dict.set不能使用) String 由于Python必须为每一个使用连接符+的字符串分配新的内存,并产生新的字符串.下面两种方式会更有 ...