??在文章SPARQL入門(一)SPARQL簡(jiǎn)介與簡(jiǎn)單使用中,我們了解了RDF题画、SPARQL以及基于Java編寫的SPARQL處理器ARQ默辨。在本文中,筆者將??會(huì)如何使用Java來(lái)操作ARQ苍息。
??注意到在Jena的官網(wǎng)http://jena.apache.org/download/index.cgi 說(shuō)明中缩幸,有關(guān)于Maven的使用方法介紹:
因此可以使用Maven,然后再用Java來(lái)操作ARQ竞思,Javadoc的網(wǎng)址為:http://jena.apache.org/documentation/javadoc/arq/ 表谊。
??在本文中我們使用另一種項(xiàng)目構(gòu)建工具——Gradle,項(xiàng)目工程截圖如下:
??我們需要修改的文件為build.gradle, ex002.ttl以及ARQ_TEST.java盖喷。其中build.gradle可以加載jena的jar爆办,具體內(nèi)容如下:
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文檔,儲(chǔ)存了我們的示例三元組课梳,內(nèi)容如下:
# 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" .
??接著就是我們的項(xiàng)目主角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");
//查詢語(yǔ)句, 查詢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();
// 輸出結(jié)果
ResultSetFormatter.out(System.out, results, query);
}
}
輸出結(jié)果如下:
--------------------------------
| 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");
//查詢語(yǔ)句, 查詢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();
// 輸出結(jié)果
ResultSetFormatter.out(System.out, results, query);
}
}
輸出結(jié)果如下:
-------------------------------------
| propertyName | propertyValue |
=====================================
| ab:email | "cindym@gmail.com" |
| ab:homeTel | "(245) 646-5488" |
-------------------------------------
??本次分享到此結(jié)束跨算,有機(jī)會(huì)我們?cè)賮?lái)深入地學(xué)習(xí)ARQ的Javadoc 。
注意:不妨了解下筆者的微信公眾號(hào): Python爬蟲與算法(微信號(hào)為:easy_web_scrape)椭懊, 歡迎大家關(guān)注~