欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

jena sparql rdf java

程序员文章站 2022-03-04 13:09:09
...

RDF文件内容:


<rdf:RDF
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:uni="http://www.mydomain.org/uni-ns#"
	xmlns:id="http://www.mydomain.org/id-ns#"
	>
	<rdf:Description rdf:about="id:949352">
		<uni:name>Grigoris Antoniou</uni:name>
		<uni:title>教授</uni:title>
	</rdf:Description>
	<rdf:Description rdf:about="id:949318">
		<uni:name>David Billington</uni:name>
		<uni:title>副教授</uni:title>
	</rdf:Description>
	<rdf:Description rdf:about="id:949111">
		<uni:name>Michael Maher</uni:name>
		<uni:title>教授</uni:title>
	</rdf:Description>
	<rdf:Description rdf:about="id:CIT1111">
		<uni:courseName>离散数学</uni:courseName>
		<uni:isTaughtBy>David Billington</uni:isTaughtBy>
	</rdf:Description>
	<rdf:Description rdf:about="id:CIT1112">
		<uni:courseName>具体数学</uni:courseName>
		<uni:isTaughtBy>Grigoris Antoniou</uni:isTaughtBy>
	</rdf:Description>
	<rdf:Description rdf:about="id:CIT2112">
		<uni:courseName>程序设计</uni:courseName>
		<uni:isTaughtBy>Michael Maher</uni:isTaughtBy>
	</rdf:Description>
	<rdf:Description rdf:about="id:CIT3112">
		<uni:courseName>计算理论</uni:courseName>
		<uni:isTaughtBy>David Billington</uni:isTaughtBy>
	</rdf:Description>
	<rdf:Description rdf:about="id:CIT3116">
		<uni:courseName>知识表示</uni:courseName>
		<uni:isTaughtBy>Grigoris Antoniou</uni:isTaughtBy>
	</rdf:Description>
</rdf:RDF>


在官网下载jena包,导入到程序中即可,jena下载地址:http://jena.apache.org/

java源程序:(记得改包名和文件路径名)
package rdf;

import java.io.InputStream;

import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.QuerySolution;
import com.hp.hpl.jena.query.ResultSet;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.util.FileManager;

public class jena_sparql_rdf {
	public static void main(String[] args) {
		//创建模型并读取rdf文件
		String inputfileName = "/semanticWeb/src/rdf/files.rdf";
		Model model = ModelFactory.createDefaultModel();
		InputStream open = FileManager.get().open(inputfileName);
		
		model.read(open, "","RDF/XML");
		//编写查询语句
//		String select ="SELECT  ?sub ?pre WHERE {?sub ?pre \"教授\"}";
//		String select ="SELECT  ?teacher WHERE {?sub uni:title \"教授\".?sub uni:name ?teacher}";
		String select ="SELECT * WHERE {?sub ?pre ?obj}";
		String queryString = 
			    "PREFIX uni:<http://www.mydomain.org/uni-ns#>"
			    + "PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>"
			    + "PREFIX id:<http://www.mydomain.org/id-ns#>"+select;

		//创建查询
		Query query = QueryFactory.create(queryString);
		//执行查询
		QueryExecution qe = QueryExecutionFactory.create(query, model);
		//返回查询结果
		ResultSet results = qe.execSelect();
		//输出查询结果
		System.out.println("遍历结果集依次输出结果:");
		while(results.hasNext()){
			QuerySolution next = results.next();
			Resource resource = next.getResource("sub");
			RDFNode rdfNode = next.get("pre");
			Literal literal = next.getLiteral("obj");
			
			System.out.println(resource+"\t"+rdfNode+"\t"+literal);
		}
		
		
//		System.out.println("按格式输出结果:");
//		ResultSetFormatter.out(System.out, results, query);
		
		//关闭QueryExecution链接
		qe.close();

	}
}