1. 程式人生 > >【Sesame】Triple Store 新增三元資料

【Sesame】Triple Store 新增三元資料

打算寫一個sesame資料庫的使用系列文章。這是第二篇,第一篇詳見這裡,講解sesame資料庫的搭建。

Sesame資料庫新增triple三元組的方法有很多種,這裡講解兩種,即單條新增與批量新增。

1. 建立資料庫連結

Sesame資料庫提供了幾種資料儲存辦法,有本地資料庫NativeStore,有基於記憶體的MemoryStore,有基於遠端資料庫的HTTP方式,還有基於關係型資料庫儲存方式的MySQLStore. 宣告變數:
private Repository repo;
	private MemoryStore memStore;
	private NativeStore natStore;
	private File repoFile;
	private RepositoryConnection repoConn;

基於記憶體MemoryStore:
	/**
	 * To get the repository within memory.
	 */
	public RepoUtil() {
		repoFile = new File(Const.repoPath);
		memStore = new MemoryStore();
		repo = new SailRepository(memStore);
	}


基於本地NativeStore:
	/**
	 * To get the repository on the disk.
	 * @param repoPath the repository file path
	 */
	public RepoUtil(String repoPath) {
		repoFile = new File(repoPath);
		natStore = new NativeStore(repoFile);
		repo = new SailRepository(natStore);
	}

基於網路HTTP Connection:
	/**
	 * To get the repository on the Http server.
	 * @param server the server address
	 * @param repoId the repository ID
	 */
	public RepoUtil(String server, String repoId) {
		repo = new HTTPRepository(server, repoId);
	}

基於關係型資料庫MySQL: 參考我的另一篇文章.

1.1. 初始化資料庫

try {
			repo.initialize();
			repoConn = repo.getConnection();//Get the connection from repository connection pool
//			repoConn.setAutoCommit(false);//why deprecate the setAutoCommit method?
		} catch(RepositoryException e) {
			e.printStackTrace();
		}


2. 新增單條資料

2.1. 生成URI

此處提供函式用於生成URI,不需要如此麻煩,領會URI生成方法要領即可。 先需要初始化URI、Literal生成器ValueFactory:
ValueFactory valueFactory = new ValueFactoryImpl();

接下來即可生成URI:
	/**
	 * To get the URI of the specific string value
	 * 1. if it is already a URI, then return;
	 * 2. else translate it to URI format and return.
	 * @param iden
	 * @return the true URI
	 */
	public URI getUri(String iden) {
		URI rtn = null;
		String url = null;
		StringBuilder strRtn = new StringBuilder(uriBuilder);
		if(isUri(iden)) {
			System.out.println("isUri");
			return valueFactory.createURI(iden);
		} else {
			try {
				url = URLEncoder.encode(iden,"utf-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			strRtn.append(url);
			rtn = valueFactory.createURI(strRtn.toString());
			return rtn;
		}
	}

此處附上判斷URI的函式(摘自網路)
	/**
	 * To justify if the input string is 
	 * in the format of URI.
	 * @param obj
	 * @return
	 */
	public boolean isUri(String obj) {
		return obj.matches("(([a-zA-Z][0-9a-zA-Z+\\\\-\\\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\\\.\\\\-_!~*'()%]+)?(#[0-9a-zA-Z;/?:@&=+$\\\\.\\\\-_!~*'()%]+)?");
//		return false;
	}

生成URI與Literal方法的簡化版(會忽略某些問題,建議採用以上函式):
URI creativeWork = vf.createURI(namespace+"CreativeWork");
Literal about = vf.createLiteral(namespace+"about#"+"SomeString");

在構建好Connection、URI以及Literal以後,即可插入三元組:
	/**
	 * The URI-URI-Literal format SPO record.
	 */
	public void addRecord(URI subj, URI pred, Literal obj) {
		try {
//			repoConn = repo.getConnection();
			repoConn.add(subj, pred, obj);
//			repoConn.close();
		} catch (RepositoryException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * The URI-URI-URI format SPO record.
	 */
	public void addRecord(URI subj, URI pred, URI obj) {
		try {
//			repoConn = repo.getConnection();
			repoConn.add(subj, pred, obj);
//			repoConn.close();
		} catch (RepositoryException e) {
			e.printStackTrace();
		}
	}

3、批量匯入資料

如果有大量資料已經在檔案中儲存,我們不需要人工編寫資料讀取、寫入的程式碼,直接通過Sesame已經提供的批量匯入介面即可。
				File importFile = new File("segment"+j+".ttl");
				String baseURI = "http://rk.com/import/test/";
				RepositoryConnection con;
				try {
					FileReader fileReader = new FileReader(importFile);
					BufferedReader reader = new BufferedReader(fileReader);
					con = repo.getConnection();
					con.add(reader, baseURI, RDFFormat.TURTLE);
					System.out.println("Add "+j+" ends.");

					con.close();
				} catch (RepositoryException e) {
					e.printStackTrace();
				} catch (RDFParseException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} 

注意Java Heap的記憶體大小限制。 可以檢視這裡修改Java虛擬機器記憶體限制。 至此完成了Sesame資料寫入的幾種方法。 下回介紹資料匯出與資料修改。