java 使用Apache Jena的API(不是RESTful API)从RDF数据构建数据库并回答Java中的SPARQL查询

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 使用Apache Jena的API(不是RESTful API)从RDF数据构建数据库并回答Java中的SPARQL查询相关的知识,希望对你有一定的参考价值。

//REFERENCE:
//http://www.xuebuyuan.com/2022237.html

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.TreeMap;

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.VCARD;
import org.apache.jena.tdb.TDBFactory;
import org.apache.jena.util.FileManager;
import org.apache.jena.query.Dataset;

public class Load {
	public static void main(String[] args) {

		String tdbDirectory = "D:/RDFDatabase/lubm.db";
		Dataset ds = TDBFactory.createDataset(tdbDirectory);
		Model tdbModel = ds.getDefaultModel();

		String dbdump1 = "D:/RDFData/LUBM_10.n3";
		System.out.println(dbdump1);
		System.out.println(tdbDirectory);
		try {
			Date nowTime = new Date();
			System.out.println(nowTime);
			
			//build the database according to the dataset type
			//use file manager's readModel is better than using Model.read
			if (dbdump1.endsWith("rdf") || dbdump1.endsWith("owl"))
				FileManager.get().readModel(tdbModel, dbdump1, "RDF/XML");
			else if (dbdump1.endsWith("ttl"))
				FileManager.get().readModel(tdbModel, dbdump1, "Turtle");
			else if (dbdump1.endsWith("nt"))
				FileManager.get().readModel(tdbModel, dbdump1, "N-Triples");
			else if (dbdump1.endsWith("n3"))
				FileManager.get().readModel(tdbModel, dbdump1, "N3");

			//tdbModel.write(System.out);
			//not supported here
			//tdbModel.commit();  //like transaction commit in database
			tdbModel.close();
			ds.close();
			
			//count the time used
			Date nowTime1 = new Date();
			System.out.println(nowTime1);
		} catch (Exception e) {
			System.out.println(dbdump1);
			e.printStackTrace();
		}
		System.out.println("Build ends!");
	}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintStream;

import org.apache.jena.query.Dataset;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.RDFNode;
import org.apache.jena.tdb.TDBFactory;
import org.apache.jena.query.Dataset;
import org.apache.jena.update.UpdateAction;

public class Query {
	 /*
     * 查询的方式有多种,查询的对象也有多种,可以对一个Dataset进行查询
     * 也可以对一个Model进行查询,还可以对一个Graph进行查询,总之Jena
     * 能提供很强大的功能
     */
    public static ResultSet SelectQuery(Dataset ds, String queryString)
    {
 
        QueryExecution qexec = QueryExecutionFactory.create(queryString, ds);
        ResultSet resultSet = qexec.execSelect();
        return resultSet;
    }
 
    /**
     * @param model
     * @param queryString
     * @return 返回的是查询的结果,类型为ResultSet类型
     *
     */
    public static ResultSet SelectQuery(Model model,String queryString)
    {
        QueryExecution qexec = QueryExecutionFactory.create(queryString, model);
        ResultSet resultSet = qexec.execSelect();
        return resultSet;
    }
 
    public static void updateQuery(Dataset ds,String updateFile)
    {
         UpdateAction.readExecute(updateFile, ds);     //updateFile是含有更新操作的sparql文件,就是把sparql保存到文件里面去
    }
    
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			String qiry = "";
			File tmpFile = new File("D:/RDFData/ex2.sql");
			InputStream in7 = new FileInputStream(tmpFile);
			Long filelength = tmpFile.length();
			byte[] filecontent = new byte[filelength.intValue()];
			in7.read(filecontent);

			qiry = new String(filecontent);
			/*
			qiry = "PREFIX dbp: <http://dbpedia.org/property/> "
					+ "PREFIX res: <http://dbpedia.org/resource/> "
					+ "PREFIX yago: <http://dbpedia.org/class/yago/> "
					+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
					+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
					+ "SELECT DISTINCT ?uri ?string  "
					+ "WHERE { "
					//+ "?uri rdf:type yago:StatesOfGermany . "
					+ "{ ?uri dbp:rulingParty 'SPD'@en . } UNION { ?uri dbp:rulingParty res:Social_Democratic_Party_of_Germany . }  "				
					+ "}";
			*/
			in7.close();

			String tdbDirectory = "";
			tdbDirectory = "D:/RDFDatabase/lubm.db";
			System.out.println(qiry);
			PrintStream out = new PrintStream(new File(
					"D:/RDFDatabase/ans.txt"));

			//load the database
			Dataset dataset = TDBFactory.createDataset(tdbDirectory);
			Model tdb = dataset.getDefaultModel();

			long startTime = System.currentTimeMillis();
			//Query query = QueryFactory.create(qiry);
			//QueryExecution qexec = QueryExecutionFactory.create(query, tdb);
			ResultSet results = SelectQuery(tdb, qiry);
			/* Execute the Query */
			// System.out.println(qexec.execAsk());
			//ResultSet results = qexec.execSelect();
			//System.out.println(qiry);
			String valStr = "", subjectStr = "", predicateStr = "", objectStr = "";
			int res_count = 0;
			while (results.hasNext()) {
				QuerySolution cur_res = results.next();
				// if (res_count % 100 == 0) {
				System.out.println(cur_res.toString());
				// break;
				// }
				res_count++;

				valStr = cur_res.toString();
				// System.out.println(res_count + "\t" + cur_res.toString());
				out.println(cur_res.toString());

			}
			//qexec.close();
			long endTime = System.currentTimeMillis();
			System.out.println("evaluation time = " + (endTime - startTime) + "ms");
			tdb.close();

			out.flush();
			out.close();

			System.out.println("End to query!");
			System.out.println("number of results : " + res_count);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
//REFERENCE: 
//http://blog.csdn.net/javafreely/article/details/8432522

//NOTICE:
//jena2 is developed by HP Lab, so the library path is com.hp.hpl.jena
//from jena3 it is maintained by Apache Software Foundation, so the library path is changed to 
//org.apache.jena

import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.VCARD;

public class Test {
	static String personURI    = "http://somewhere/JohnSmith";
	static String givenName    = "John";
	static String familyName   = "Smith";
	static String fullName     = givenName + " " + familyName;
	
	public static void main(String[] args){
		// create an empty Model
		Model model = ModelFactory.createDefaultModel();

		// create the resource
		Resource johnSmith = model.createResource(personURI);

		// add the property
		johnSmith.addProperty(VCARD.FN, fullName);
		johnSmith.addProperty(VCARD.N, 
				model.createResource()
				.addProperty(VCARD.Given, givenName)
				.addProperty(VCARD.Family, familyName));
		/*
		model.write(System.out);  
        System.out.println();  
        model.write(System.out, "RDF/XML-ABBREV");  
        System.out.println();  
        model.write(System.out, "N-TRIPLE");  
		*/
		System.out.println("Test ends!");
	}
}

以上是关于java 使用Apache Jena的API(不是RESTful API)从RDF数据构建数据库并回答Java中的SPARQL查询的主要内容,如果未能解决你的问题,请参考以下文章

如何使用Apache Jena获得“选择计数(*)”查询的结果?

搭建Jena Fuseki并执行SPARQL查询

在 Apache Jena Fuseki 中压缩数据集

Jena Fuseki 服务器命令未找到

Jena+fuseki

PySpark 是不是调用 java api,然后 java api 在 Apache Spark 中调用 scala api?