D2RQ的下载和使用实例教程

D2RQ的下载和使用

1. d2rq的简介

d2rq平台一个能够把关系数据库当作虚拟的,只读的rdf图来访问的一个平台.它提供了基于rdf的访问模式,但不需要把数据以rdf的形式存储.
以虚拟RDF图的方式访问关系数据库是其最主要的一个特性。它的机理就是通过mapping文件,把对RDF的查询等操作翻译成SQL语句,最终在RDB上实现对应操作。在做知识图谱项目的时候,我们可以灵活地选择数据访问方式。当对外提供服务,查询操作比较频繁的情况下,最好是将RDB的数据直接转为RDF,会节省很多SPARQL到SQL的转换时间。
使用d2rq,你可以

  • 使用saprql查询非rdf数据库
  • 把数据库的内容当作链接来访问
  • 把数据库的内容以rdf的形式转化出来
  • 使用apache jena api 访问非rdf数据库

2. d2rq的下载
查看如下官方网站
D2RQ的下载和使用实例教程_第1张图片
下载到喜欢的地方,解压
我的是下载到了D:\d2rq
3. 下载安装数据库
我的是windows版本的mysql数据库下载
数据库基本操作简单入门
我数据库是建好了两个表一个是person表,一个是user表
D2RQ的下载和使用实例教程_第2张图片
4. 下载jdbc驱动包
d2rq需要与java、mysql进行联合应用,因此需要JDBC driver,将mysql-connector-java-5.1.44.jar 点击下载 文件放入d2rq解压后的/lib文件夹中。
个人建议用上面这个版本,因为其他或者最新的,都会报一个错误

5. 创建映射文件
用generate-mapping工具为数据库创建mapping file(映射文件)
切换到D:\d2rq\d2rq-0.8.1 目录下,用命令行执行如下指令

.\generate-mapping -u root -p 123456 -o lsy.ttl jdbc:mysql:///lsy?useSSL=false
//-u root 是数据库的登录名
//-p 123456 是登录密码
//-o name.ttl 是映射文件的名称
//jdbc:mysql:///lsy 指定数据库的名称 
//?useSSL=false 避免提示警告

6. 启动d2r server
执行命令: .\d2r-server lsy.ttl
打开网址 http://localhost:2020/,出现如下界面
D2RQ的下载和使用实例教程_第3张图片
最上面的home可以查看所有关联的表,下面第三点sparql endpoint 可以使用sparql语言查询数据,如果不懂语法的,请先学习

7. 转存为rdf文件
运行dump-rdf工具,执行如下的指令:

.\dump-rdf -o lsy.nt lsy.ttl
//-o lsy.nt 这是rdf的一种保存格式
//一般有 有“TURTLE”, “RDF/XML”, “RDF/XML-ABBREV”, “N3”, 和“N-TRIPLE”。“N-TRIPLE”是默认的输出格式

8. 映射文件的理解
详见官方文档 d2rq-language
它的映射主体框架如下
D2RQ的下载和使用实例教程_第4张图片
先是数据库的映射,然后是类型的映射(也就是资源类型),接下来就是在资源和属性值之间搭一个桥梁propertyBridge

@prefix map: <#> .
@prefix db: <> .
@prefix vocab: <http://solicucu/vocab/> .
//上面的http://solicucu是自己最随便写的,可以不写,那么他就会生成以文件存储的实际位置开头
//下面这些是常用的命名空间,自动生成的
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix d2rq: <http://www.wiwiss.fu-berlin.de/suhl/bizer/D2RQ/0.1#> .
@prefix jdbc: <http://d2rq.org/terms/jdbc/> .

//整个映射的主要模式:
//把每个表映射成一个类
//每一行是一个资源
//每一列就是资源的属性
map:database a d2rq:Database; //把数据库名映射为database
	d2rq:jdbcDriver "com.mysql.jdbc.Driver";
	d2rq:jdbcDSN "jdbc:mysql:///lsy?useSSL=false";
	d2rq:username "root";
	d2rq:password "123456";
	jdbc:autoReconnect "true";  
	jdbc:zeroDateTimeBehavior "convertToNull";
	.

//"建立person表的映射"
# Table person
map:person a d2rq:ClassMap;  //每个表都是一个ClassMap
	d2rq:dataStorage map:database;  //指明来自哪个数据库
	d2rq:uriPattern "http://solicucu/person/#@@person.username|urlify@@";
	//uriPattern 这个是对资源的标识前面一段可以自己修改,否则默认为文件存储位置
	//值得一说的是@@ 之间是表示取标识串里面的值,也就是数据库里面对应的值
	//urlify 表示自动采用urlencode编码,而且会把空格转化为_
	d2rq:class vocab:person;
	//指明资源的类型,对应rdf:type
	d2rq:classDefinitionLabel "person";
	//指明类型定义的标签,对应与rdfs:label
	
//定义每一个资源标签的属性	.
map:person__label a d2rq:PropertyBridge;
	d2rq:belongsToClassMap map:person;
	d2rq:property rdfs:label;
	d2rq:pattern "person #@@person.username@@"; //标签的模式
	.
//属性值的映射	
map:person_username a d2rq:PropertyBridge; //指明是一个属性
	d2rq:belongsToClassMap map:person;	//指明属于哪个类别的
	d2rq:property vocab:person_username; //指明属性的标识名字
	d2rq:propertyDefinitionLabel "person username"; //属性标签
	d2rq:column "person.username"; //指定属性的值,注意一定要用双引号表示取其中的值
	.
map:person_name a d2rq:PropertyBridge;
	d2rq:belongsToClassMap map:person;
	d2rq:property vocab:person_name;
	d2rq:propertyDefinitionLabel "person name";
	d2rq:column "person.name";
	.
map:person_age a d2rq:PropertyBridge;
	d2rq:belongsToClassMap map:person;
	d2rq:property vocab:person_age;
	d2rq:propertyDefinitionLabel "person age";
	d2rq:column "person.age";
	d2rq:datatype xsd:integer; //注明数据类型
	.
map:person_phone a d2rq:PropertyBridge;
	d2rq:belongsToClassMap map:person;
	d2rq:property vocab:person_phone;
	d2rq:propertyDefinitionLabel "person phone";
	d2rq:column "person.phone";
	.

# Table user
map:user a d2rq:ClassMap;
	d2rq:dataStorage map:database;
	d2rq:uriPattern "http://solicucu/user/#@@user.username|urlify@@";
	d2rq:class vocab:user;
	d2rq:classDefinitionLabel "user";
	.
map:user__label a d2rq:PropertyBridge;
	d2rq:belongsToClassMap map:user;
	d2rq:property rdfs:label;
	d2rq:pattern "user #@@user.username@@";
	.
map:user_username a d2rq:PropertyBridge;
	d2rq:belongsToClassMap map:user;
	d2rq:property vocab:user_username;
	d2rq:propertyDefinitionLabel "user username";
	d2rq:column "user.username";
	.
map:user_password a d2rq:PropertyBridge;
	d2rq:belongsToClassMap map:user;
	d2rq:property vocab:user_password;
	d2rq:propertyDefinitionLabel "user password";
	d2rq:column "user.password";
	.

9. 对应生成的rdf文件

<http://solicucu/person/#chacha> <http://solicucu/vocab/person_phone> "15200823423" .
<http://solicucu/person/#chacha> <http://solicucu/vocab/person_age> "19"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://solicucu/person/#chacha> <http://solicucu/vocab/person_name> "scc" .
<http://solicucu/person/#chacha> <http://solicucu/vocab/person_username> "chacha" .
<http://solicucu/person/#chacha> <http://www.w3.org/2000/01/rdf-schema#label> "person #chacha" .
<http://solicucu/person/#chacha> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://solicucu/vocab/person> .
<http://solicucu/person/#littlejun> <http://solicucu/vocab/person_phone> "15200823421" .
<http://solicucu/person/#littlejun> <http://solicucu/vocab/person_age> "19"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://solicucu/person/#littlejun> <http://solicucu/vocab/person_name> "lhj" .
<http://solicucu/person/#littlejun> <http://solicucu/vocab/person_username> "littlejun" .
<http://solicucu/person/#littlejun> <http://www.w3.org/2000/01/rdf-schema#label> "person #littlejun" .
<http://solicucu/person/#littlejun> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://solicucu/vocab/person> .
<http://solicucu/person/#yuqin> <http://solicucu/vocab/person_phone> "15768169184" .
<http://solicucu/person/#yuqin> <http://solicucu/vocab/person_age> "19"^^<http://www.w3.org/2001/XMLSchema#integer> .
<http://solicucu/person/#yuqin> <http://solicucu/vocab/person_name> "yuqing" .
<http://solicucu/person/#yuqin> <http://solicucu/vocab/person_username> "yuqin" .
<http://solicucu/person/#yuqin> <http://www.w3.org/2000/01/rdf-schema#label> "person #yuqin" .
<http://solicucu/person/#yuqin> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://solicucu/vocab/person> .
<http://solicucu/user/#chacha> <http://solicucu/vocab/user_password> "888888" .
<http://solicucu/user/#chacha> <http://solicucu/vocab/user_username> "chacha" .
<http://solicucu/user/#chacha> <http://www.w3.org/2000/01/rdf-schema#label> "user #chacha" .
<http://solicucu/user/#chacha> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://solicucu/vocab/user> .
<http://solicucu/user/#littlejun> <http://solicucu/vocab/user_password> "888888" .
<http://solicucu/user/#littlejun> <http://solicucu/vocab/user_username> "littlejun" .
<http://solicucu/user/#littlejun> <http://www.w3.org/2000/01/rdf-schema#label> "user #littlejun" .
<http://solicucu/user/#littlejun> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://solicucu/vocab/user> .
<http://solicucu/user/#yuqin> <http://solicucu/vocab/user_password> "888888" .
<http://solicucu/user/#yuqin> <http://solicucu/vocab/user_username> "yuqin" .
<http://solicucu/user/#yuqin> <http://www.w3.org/2000/01/rdf-schema#label> "user #yuqin" .
<http://solicucu/user/#yuqin> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://solicucu/vocab/user> .
<http://solicucu/vocab/user_password> <http://www.w3.org/2000/01/rdf-schema#label> "user password" .
<http://solicucu/vocab/user_password> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://solicucu/vocab/person_age> <http://www.w3.org/2000/01/rdf-schema#label> "person age" .
<http://solicucu/vocab/person_age> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://solicucu/vocab/person> <http://www.w3.org/2000/01/rdf-schema#label> "person" .
<http://solicucu/vocab/person> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://solicucu/vocab/user> <http://www.w3.org/2000/01/rdf-schema#label> "user" .
<http://solicucu/vocab/user> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2000/01/rdf-schema#Class> .
<http://solicucu/vocab/person_username> <http://www.w3.org/2000/01/rdf-schema#label> "person username" .
<http://solicucu/vocab/person_username> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://solicucu/vocab/person_phone> <http://www.w3.org/2000/01/rdf-schema#label> "person phone" .
<http://solicucu/vocab/person_phone> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://solicucu/vocab/person_name> <http://www.w3.org/2000/01/rdf-schema#label> "person name" .
<http://solicucu/vocab/person_name> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://solicucu/vocab/user_username> <http://www.w3.org/2000/01/rdf-schema#label> "user username" .
<http://solicucu/vocab/user_username> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .
<http://www.w3.org/2000/01/rdf-schema#label> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Property> .

你可能感兴趣的:(自然语言处理)