[英]Develop a web application based on triplestore database
I've recently developed a "classic" 3-tier web applications using Java EE. 我最近使用Java EE开发了一个“经典的”三层Web应用程序。
I've used GlassFish as application server, MS SQL Server as DBMS and xhtml pages with primefaces components for the front end. 我已经将GlassFish用作应用程序服务器,将MS SQL Server用作DBMS,并使用了带有Primefaces组件的xhtml页面作为前端。
Now, for educational purposes, I want to substitute the relational db with a pure triplestore database but I'm not sure about the procedure to follow. 现在,出于教育目的,我想用纯三元数据库替换关系数据库,但是我不确定要遵循的过程。
I've searched a lot on google and on this site but I didn't find what I was looking for, because every answer I found was more theoretical than practical. 我在Google和此网站上进行了很多搜索,但未找到所需的内容,因为我发现的每个答案都比理论上的要实用。
If possible, I need a sort of tutorial or some practical tips. 如果可能,我需要某种教程或一些实用技巧。 I've read the documentation about Apache Jena but I'm not able to find a solid starting point. 我已经阅读了有关Apache Jena的文档,但是找不到可靠的起点。
In particoular: 特别是:
- In order to use MS SQL Server with GlassFish I've used a JDBC Driver, created a datasource and a connection pool. -为了将MS SQL Server与GlassFish一起使用,我使用了JDBC驱动程序,创建了数据源和连接池。 Does it exist an equivalent procedure to set up a triple store database? 建立三重存储数据库是否存在等效的过程?
- To handle users authentication, I've used a Realm. -为了处理用户身份验证,我使用了领域。 What should I do now? 我现在应该怎么办?
For the moment I've created "by hand" a RDF schema and using Jena Schemagen I've translated it into a Java Class. 目前,我已经“手工”创建了RDF模式,并使用Jena Schemagen将其转换为Java类。 What should I do now? 我现在应该怎么办?
After several attempts and other research on the net I finally achieved my goal. 经过几次尝试和其他网上研究,我终于实现了我的目标。
I decided to develop a hybrid solution in which I manage users login and their navigation permits via MS SQL Server and JDBCRealm, while I use Jena TDB to save all the other data. 我决定开发一种混合解决方案,在该解决方案中,我通过MS SQL Server和JDBCRealm管理用户登录和导航许可,同时使用Jena TDB保存所有其他数据。
Starting with an RDF schema, I created a Java class that contains resources and properties to easily create my statements via code. 从RDF模式开始,我创建了一个Java类,该类包含资源和属性,可通过代码轻松地创建我的语句。 Here's an example: 这是一个例子:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns="http://www.stackoverflow.com/example#"
xml:base="http://www.stackoverflow.com/example">
<rdfs:Class rdf:ID="User"></rdfs:Class>
<rdfs:Class rdf:ID="Project"></rdfs:Class>
<rdf:Property rdf:ID="email"></rdf:Property>
<rdf:Property rdf:ID="name"></rdf:Property>
<rdf:Property rdf:ID="surname"></rdf:Property>
<rdf:Property rdf:ID="description"></rdf:Property>
<rdf:Property rdf:ID="customer"></rdf:Property>
<rdf:Property rdf:ID="insertProject">
<rdfs:domain rdf:resource="http://www.stackoverflow.com/example#User"/>
<rdfs:range rdf:resource="http://www.stackoverflow.com/example#Project"/>
</rdf:Property>
</rdf:RDF>
And this is the Java class: 这是Java类:
public class MY_ONTOLOGY {
private static final OntModel M = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM);
private static final String NS = "http://www.stackoverflow.com/example#";
private static final String BASE_URI = "http://www.stackoverflow.com/example/";
public static final OntClass USER = M.createClass(NS + "User");
public static final OntClass PROJECT = M.createClass(NS + "Project");
public static final OntProperty EMAIL = M.createOntProperty(NS + "hasEmail");
public static final OntProperty NAME = M.createOntProperty(NS + "hasName");
public static final OntProperty SURNAME = M.createOntProperty(NS + "hasSurname");
public static final OntProperty DESCRIPTION = M.createOntProperty(NS + "hasDescription");
public static final OntProperty CUSTOMER = M.createOntProperty(NS + "hasCustomer");
public static final OntProperty INSERTS_PROJECT = M.createOntProperty(NS + "insertsProject");
public static final String getBaseURI() {
return BASE_URI;
}
}
Then I've created a directory on my PC where I want to store the data, like C:\\MyTDBdataset
. 然后,在PC上创建了一个目录,用于存储数据,例如C:\\MyTDBdataset
。
To store data inside it, I use the following code: 要在其中存储数据,我使用以下代码:
String directory = "C:\\MyTDBdataset";
Dataset dataset = TDBFactory.createDataset(directory);
dataset.begin(ReadWrite.WRITE);
try {
Model m = dataset.getDefaultModel();
Resource user = m.createResource(MY_ONTOLOGY.getBaseURI() + "Ronnie", MY_ONTOLOGY.USER);
user.addProperty(MY_ONTOLOGY.NAME, "Ronald");
user.addProperty(MY_ONTOLOGY.SURNNAME, "Red");
user.addProperty(MY_ONTOLOGY.EMAIL, "ronnie@myemail.com");
Resource project = m.createResource(MY_ONTOLOGY.getBaseURI() + "MyProject", MY_ONTOLOGY.PROJECT);
project.addProperty(MY_ONTOLOGY.DESCRIPTION, "This project is fantastic");
project.addProperty(MY_ONTOLOGY.CUSTOMER, "Customer & Co");
m.add(user, MY_ONTOLOGY.INSERTS_PROJECT, project);
dataset.commit();
} finally {
dataset.end();
}
If I want to read statements in my TDB, I can use something like this: 如果我想在我的TDB中读取语句,则可以使用以下内容:
dataset.begin(ReadWrite.READ);
try {
Model m = dataset.getDefaultModel();
StmtIterator iter = m.listStatements();
while (iter.hasNext()) {
Statement stmt = iter.nextStatement();
Resource subject = stmt.getSubject();
Property predicate = stmt.getPredicate();
RDFNode object = stmt.getObject();
System.out.println(subject);
System.out.println("\t" + predicate);
System.out.println("\t\t" + object);
System.out.println("");
}
m.write(System.out, "RDF/XML"); //IF YOU WANT TO SEE AT CONSOLE YOUR DATA AS RDF/XML
} finally {
dataset.end();
}
If you want to navigate your model in different ways, look at this tutorial provided by Apache. 如果要以不同的方式浏览模型,请查看Apache提供的本教程 。
If you want to remove specific statements in your model, you can write something like this: 如果要删除模型中的特定语句,可以编写如下内容:
dataset.begin(ReadWrite.WRITE);
try {
Model m = dataset.getDefaultModel();
m.remove(m.createResource("http://http://www.stackoverflow.com/example/Ronnie"), MY_ONTOLOGY.NAME, m.createLiteral("Ronald"));
dataset.commit();
} finally {
dataset.end();
}
That's all! 就这样! Bye! 再见!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.