您好,欢迎访问一九零五行业门户网

【Sesame】TripleStore添加三元数据

打算写一个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;
基于内存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数据写入的几种方法。下回介绍数据导出与数据修改。
其它类似信息

推荐信息