SPARQL入门(二)使用Java操作ARQ

  在文章SPARQL入门(一)SPARQL简介与简单使用中,我们了解了RDF、SPARQL以及基于Java编写的SPARQL处理器ARQ。在本文中,笔者将会如何使用Java来操作ARQ。
  注意到在Jena的官网http://jena.apache.org/download/index.cgi 说明中,有关于Maven的使用方法介绍:

因此可以使用Maven,然后再用Java来操作ARQ,Javadoc的网址为:http://jena.apache.org/documentation/javadoc/arq/

  在本文中我们使用另一种项目构建工具——Gradle,项目工程截图如下:

  我们需要修改的文件为build.gradle, ex002.ttl以及ARQ_TEST.java。其中build.gradle可以加载jena的jar,具体内容如下:

plugins {
    id 'java'
}

group 'com.kg.learn'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    // https://mvnrepository.com/artifact/org.apache.jena/jena-arq
    compile group: 'org.apache.jena', name: 'jena-arq', version: '3.11.0'
    // https://mvnrepository.com/artifact/org.slf4j/slf4j-api
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.26'
    // https://mvnrepository.com/artifact/org.slf4j/slf4j-simple
    compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.26'
}

  ex002.ttl为RDF文档,储存了我们的示例三元组,内容如下:

# filename: ex002.ttl

@prefix ab: <http://learningsparql.com/ns/addressbook#> .

ab:richard ab:homeTel "(229) 276-5135" . 
ab:richard ab:email   "richard49@hotmail.com" . 

ab:cindy ab:homeTel "(245) 646-5488" . 
ab:cindy ab:email   "cindym@gmail.com" . 

ab:craig ab:homeTel "(194) 966-1505" . 
ab:craig ab:email   "craigellis@yahoo.com" . 
ab:craig ab:email   "c.ellis@usairwaysgroup.com" .

  接着就是我们的项目主角ARQ_TETS.java,代码如下:

import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;

public class ARQ_TEST {
    public static void main(String[] args) {
        Model model = ModelFactory.createDefaultModel();
        //ttl文件路径
        model.read("/Users/kg/IdeaProjects/arq_test/src/main/java/ex002.ttl");
        //查询语句, 查询craig的电子邮件地址
        String queryString = "PREFIX ab: <http://learningsparql.com/ns/addressbook#> \n" +
                "\n" +
                "SELECT ?craig_email\n" +
                "WHERE\n" +
                "{ ab:craig ab:email ?craig_email . }";
        Query query = QueryFactory.create(queryString);
        QueryExecution qe = QueryExecutionFactory.create(query, model);
        ResultSet results = qe.execSelect();
        // 输出结果
        ResultSetFormatter.out(System.out, results, query);
    }
}

输出结果如下:

--------------------------------
| craig_email                  |
================================
| "c.ellis@usairwaysgroup.com" |
| "craigellis@yahoo.com"       |
--------------------------------

以上是查询“craig的电子邮件地址” 的代码。如果我们想要知道cindy的全部属性及属性值,可以使用以下Java代码:

import org.apache.jena.query.*;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;

public class ARQ_TEST {
    public static void main(String[] args) {
        Model model = ModelFactory.createDefaultModel();
        //ttl文件路径
        model.read("/Users/kg/IdeaProjects/arq_test/src/main/java/ex002.ttl");
        //查询语句, 查询cindy的全部属性及属性值
        String queryString = "PREFIX ab: <http://learningsparql.com/ns/addressbook#> \n" +
                "\n" +
                "SELECT ?propertyName ?propertyValue\n" +
                "WHERE\n" +
                "{ ab:cindy ?propertyName ?propertyValue . }";
        Query query = QueryFactory.create(queryString);
        QueryExecution qe = QueryExecutionFactory.create(query, model);
        ResultSet results = qe.execSelect();
        // 输出结果
        ResultSetFormatter.out(System.out, results, query);
    }
}

输出结果如下:

-------------------------------------
| propertyName | propertyValue      |
=====================================
| ab:email     | "cindym@gmail.com" |
| ab:homeTel   | "(245) 646-5488"   |
-------------------------------------

  本次分享到此结束,有机会我们再来深入地学习ARQ的Javadoc 。

注意:不妨了解下笔者的微信公众号: Python爬虫与算法(微信号为:easy_web_scrape), 欢迎大家关注~

posted @ 2019-05-15 14:05  山阴少年  阅读(1694)  评论(0编辑  收藏  举报