Table of Contents
1. 建立数据库链接
1.1. 初始化数据库
2. 添加单条数据
2.1. 生成URI
3、批量导入数据
Home Database Mysql Tutorial 【Sesame】TripleStore添加三元数据

【Sesame】TripleStore添加三元数据

Jun 07, 2016 pm 03:28 PM
data Add to

打算写一个sesame数据库的使用系列文章。这是第二篇,第一篇详见这里,讲解sesame数据库的搭建。 Sesame数据库添加triple三元组的方法有很多种,这里讲解两种,即单条添加与批量添加。 1. 建立数据库链接 Sesame数据库提供了几种数据存储办法,有本地数据库N

打算写一个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;
Copy after login

基于内存MemoryStore:
	/**
	 * To get the repository within memory.
	 */
	public RepoUtil() {
		repoFile = new File(Const.repoPath);
		memStore = new MemoryStore();
		repo = new SailRepository(memStore);
	}
Copy after login

基于本地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);
	}
Copy after login

基于网络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);
	}
Copy after login

基于关系型数据库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();
		}
Copy after login


2. 添加单条数据

2.1. 生成URI

此处提供函数用于生成URI,不需要如此麻烦,领会URI生成方法要领即可。先需要初始化URI、Literal生成器ValueFactory:
ValueFactory valueFactory = new ValueFactoryImpl();
Copy after login

接下来即可生成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;
		}
	}
Copy after login

此处附上判断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;
	}
Copy after login

生成URI与Literal方法的简化版(会忽略某些问题,建议采用以上函数):
URI creativeWork = vf.createURI(namespace+"CreativeWork");
Literal about = vf.createLiteral(namespace+"about#"+"SomeString");
Copy after login

在构建好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();
		}
	}
Copy after login

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();
				} 
Copy after login

注意Java Heap的内存大小限制。可以查看这里修改Java虚拟机内存限制。
至此完成了Sesame数据写入的几种方法。下回介绍数据导出与数据修改。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Use ddrescue to recover data on Linux Use ddrescue to recover data on Linux Mar 20, 2024 pm 01:37 PM

Use ddrescue to recover data on Linux

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation! Apr 03, 2024 pm 12:04 PM

Open source! Beyond ZoeDepth! DepthFM: Fast and accurate monocular depth estimation!

How to use Excel filter function with multiple conditions How to use Excel filter function with multiple conditions Feb 26, 2024 am 10:19 AM

How to use Excel filter function with multiple conditions

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks Apr 29, 2024 pm 06:55 PM

The vitality of super intelligence awakens! But with the arrival of self-updating AI, mothers no longer have to worry about data bottlenecks

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training Apr 01, 2024 pm 07:46 PM

Google is ecstatic: JAX performance surpasses Pytorch and TensorFlow! It may become the fastest choice for GPU inference training

How to add a TV to Mijia How to add a TV to Mijia Mar 25, 2024 pm 05:00 PM

How to add a TV to Mijia

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Slow Cellular Data Internet Speeds on iPhone: Fixes

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times.

See all articles