【Sesame】Triple Store 添加三中繼資料

來源:互聯網
上載者:User

打算寫一個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資料寫入的幾種方法。下回介紹資料匯出與資料修改。






相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.