About Apache Jena
Apache Jena is a free and open source Java framework for building Semantic Web and Linked Data applications : link.
How to use Jena Module
Import Jena module into your project
Add the needed dependencies into your pom.xml
org.ddbstoolkit.toolkit.modules.datastore.jena ddbstoolkit-jena 1.0.0-beta2 compile
Create your data model
You need to create your data classes with the following requirements :
- For non distributed data: your classes need to implement IEntity
- For distributed data: your classes need to extend DistributedEntity
Primary keys are indicated with the @Id annotation. It is mainly used if you want to add your entity into a database module such as MySQL or SQLite module.
The remote endpoint URL is indicated with the @Service annotation.
The default URI namespace is indicated with the @DefaultNamespace annotation.
Optional elements are indicated with the @Optional annotation.
Entity URI is indicated with the @URI annotation.
Namespace property is indicated with the @Namespace annotation. It can be used if the namespace of the property is different from the default namespace defined with @DefaultNamespace annotation.
Example for distributed Actor and Film data
@Service(url="http://www.factforge.net/sparql")
@DefaultNamespace(name="fb",url="http://rdf.freebase.com/ns/")
public class Actor extends DistributedEntity {
@Id
@EntityName(name="actor_id")
private Integer actorId;
@URI
private String actor_uri;
@Optional
@EntityName(name="actor_name")
private String actorName;
public String getActorName() {
return actorName;
}
public void setActorName(String actorName) {
this.actorName = actorName;
}
public String getActor_uri() {
return actor_uri;
}
public void setActor_uri(String actor_uri) {
this.actor_uri = actor_uri;
}
public Integer getActorId() {
return actorId;
}
public void setActorId(Integer actorId) {
this.actorId = actorId;
}
}
@Service(url="http://data.linkedmdb.org/sparql")
@DefaultNamespace(name="movie",url="http://data.linkedmdb.org/resource/movie/")
public class Film implements DistributedEntity {
@Id
@Optional
@EntityName(name="film_id")
private Integer filmID;
@URI
private String film_uri;
@Namespace(name="dc",url="http://purl.org/dc/terms/")
private String title;
private int runtime;
private Actor[] actor;
public Integer getFilmID() {
return filmID;
}
public void setFilmID(Integer filmID) {
this.filmID = filmID;
}
public String getFilm_uri() {
return film_uri;
}
public void setFilm_uri(String film_uri) {
this.film_uri = film_uri;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getRuntime() {
return runtime;
}
public void setRuntime(int runtime) {
this.runtime = runtime;
}
public Actor[] getActors() {
return actors;
}
public void setActors(Actor[] actors) {
this.actors = actors;
}
}
Instantiate your Jena module
If you are using a remote endpoint
DistributableEntityManager manager = new DistributedSPARQLManager();
If you are using a local datastore
DistributableEntityManager manager = new DistributedSPARQLManager("/myPath/myJenaDatastore");
Jena Module actions
Add an element (Only for local datastore)
manager.open();
Actor anActor = new Actor();
anActor.setActor_uri("http://ddbstoolkit.org/actor/Steven");
anActor.setActorName("Steven");
manager.add(anActor);
manager.close();
Update an element (Only for local datastore)
manager.open();
Actor anActor = new Actor();
anActor.setActor_uri("http://ddbstoolkit.org/actor/Steven");
anActor.setActorName("Steven");
manager.update(anActor);
manager.close();
Delete an element (Only for local datastore)
manager.open();
Actor anActor = new Actor();
anActor.setActor_uri("http://ddbstoolkit.org/actor/Steven");
manager.delete(anActor);
manager.close();
Read an element
manager.open();
Actor anActor = new Actor();
anActor.setActor_uri("http://ddbstoolkit.org/actor/Steven");
anActor = manager.read(anActor);
manager.close();
List elements
manager.open(); //List all films Listfilms = manager.listAll(new Film(), null, null); //List all films ordered by film_ID films = manager.listAll(new Film(), null, OrderBy.get("filmId", OrderByType.ASC)); List conditions = new ArrayList<>(); conditions.add(DistributedSPARQLManager.getObjectVariable(new Book())+" fb:type.object.name 'The Fellowship of the Ring'@en"); conditions.add("FILTER ( lang(?title) = 'en' )"); conditions.add("FILTER ( lang(?summary) = 'en' )"); //List all films with the english name 'The Fellowship of the Ring' films = manager.listAll(new Film(), conditions, null); //List with Conditions Conditions condition = Conditions.createConditions().add( Conditions.ge("runtime", 20)); List results = manager.listAll(new Film(),condition, null); manager.close();
Load elements linked to an entity
manager.open();
Film aFilm = new Film();
aFilm.setFilmID("http://ddbstoolkit.org/film/hobbit");
aFilm = manager.read(aFilm);
//List actors of a movie ordered by actor_id field
lastFilmAdded = manager.loadArray(aFilm, "actors", OrderBy.get("filmID", OrderByType.ASC));
manager.close();
Execute transactions
manager.open();
//Start a new transaction
DDBSTransaction transactionAddCommit = new DDBSTransaction("myTransaction");
Actor anActor = new Actor();
anActor.setActorName("Steven");
transactionAddCommit.add(filmToAdd);
//Execute the transaction
manager.executeTransaction(transactionAddCommit);
//Commit the transaction
manager.commit(transactionAddCommit);
//Rollback the transaction
manager.rollback(transactionAddCommit);
manager.close();