前言

正如其承诺的那样

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. php : 工厂类演示

    工厂类的目的: 通过类名, 动态创建该类的对象实例 <?php /* * 工厂类演示 */ class A{} class B{} // 工厂类: 有一个静态方法,通过该方法,能够获得指定类的对 ...

  2. LTE Module User Documentation(翻译7)——无线环境地图(REM)、AMC 模型 和 CQI 计算

    LTE用户文档 (如有不当的地方,欢迎指正!) 12 Radio Environment Maps   通过使用类 RadioEnvironmentMapHelper  是可能输出文件 Radio E ...

  3. Linux变量

    变量:(大的分为环境变量与本的变量) 本地变量: 本地变量在用户现在的shell生命期的脚本中使用.例如,本地变量file-name="loop.doc",这个值只在用户当前she ...

  4. 自定义datagridview列,却提示DataGridView 控件中至少有一列没有单元格模板

    哈哈,一个小误区,你看看设计窗体生成的代码,DataGridView的列不是GridViewColumn 而是DataGridViewTextBoxColumn你只要添加这个类型的对象就可以了,我也是 ...

  5. centos 安装redis自启动要点

    1.redis.conf a.daemonize yes b.pidfile /var/run/xxx.pid 2./etc/init.d/redis //加了下面三个注释部分,才支持设置开机自启动 ...

  6. List怎么遍历删除元素

    public static void main(String[] args) {  List<String> list = new ArrayList<String>();   ...

  7. The import javax.servlet.http.HttpServletRequest cannot be resolved

    Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServletRequest ...

  8. 便携式文件夹加密器 lockdir 5.74

    便携式文件夹加密器 lockdir 5.74下载地址 http://www.hoposoft.com/lock/ 注册码两枚: 注册名:Long 注册码:6088805000000E7E25F09A6 ...

  9. 【转载】JSP常用跳转方式

    转自:http://blog.csdn.net/wanghuan203/article/details/8836326 (1)href超链接标记,属于客户端跳转 (2)使用javascript完成,属 ...

  10. 鼠标滚轮控制侧边div上下翻动效果

    css部分: <style> * { margin: 0; padding: 0;} .wrap { width: 1000px; margin: 0 auto; overflow: hi ...