【Sesame】Triple Store 添加三元数据

来源:互联网 发布:淘宝图片自己拍 编辑:程序博客网 时间:2024/05/02 00:18

打算写一个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数据写入的几种方法。
下回介绍数据导出与数据修改。






0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 很想上专升本但是专科挂科怎么办 英国学校申请的gpa太低了怎么办 博士面试通过后导师不签名怎么办 资助出国留学协议书写错了怎么办 人生最低谷运气最差的时候怎么办 孩子出国留学一年后想回国怎么办 领导给我娃的钱我怎么办 在西浦大三成绩不好影响申研怎么办 日本国想在中国开分公司要怎么办 美国生完孩子做飞机婴儿怎么办 会计专硕复试英语面试不好怎么办 被打过狂犬疫苗的狗咬了怎么办 保姆和雇主带孩子玩受伤了怎么办 银行流水造假拒签10年怎么办 老师进修被学校扣工资该怎么办 老婆性格太倔结婚一年想离婚怎么办 天正打图窗户线条太粗怎么办 孩子在幼儿园不敢跟老师说话怎么办 温州教育准考证号密码忘了怎么办 高等继续教育网打不开课程怎么办 安运继续教育的登录密码忘了怎么办 金蝶k3账套管理打不开了怎么办 仁和会计课堂app不能用怎么办 光大银行已经下卡了终审被拒怎么办 过了上诉期和申诉期该怎么办 北外大四学生要实习半年课程怎么办 电脑发给手机的文件过期了怎么办 农民给土地卖了30年后怎么办 家长发家长群作业太多老师怎么办 在考试中心补不了四级成绩怎么办 微信登录密码不记得了怎么办 欠农民工工资不给怎么办老板说没钱 国外期刊催问稿件不理睬怎么办 老公离不开老婆也离不开小三怎么办 出轨被老婆发现还和小三联系怎么办 老公出轨后回家老婆不想原谅怎么办 小三和原配打架都住院了怎么办 毕业太多年查不到学历认证怎么办 没有做税种核定开了票怎么办 在学信网上查不到学历信息怎么办 学信网手机号换了密码忘了怎么办