Skip to content

Instantly share code, notes, and snippets.

@mhgrove
Last active February 14, 2019 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhgrove/1045572 to your computer and use it in GitHub Desktop.
Save mhgrove/1045572 to your computer and use it in GitHub Desktop.
Example of how to use Stardog's Jena Bindings
// start a transaction before adding the data. This is not required,
// but it is faster to group the entire add into a single transaction rather
// than rely on the auto commit of the underlying stardog connection.
aModel.begin();
// read data into the model. note, this will add statement at a time.
// Bulk loading needs to be performed directly with the BulkUpdateHandler provided
// by the underlying graph, or by reading in files in RDF/XML format, which uses the
// bulk loader natively. Alternatively, you can load data into the Stardog
// database using SNARL, or via the command line client.
aModel.getReader("N3").read(aModel, new FileInputStream("data/sp2b_10k.n3"), "");
// done!
aModel.commit();
// obtain a Jena model for the specified stardog database connection. Just creating an in-memory
// database; this is roughly equivalent to ModelFactory.createDefaultModel.
Model aModel = SDJenaFactory.createModel(aConn);
/*
* Copyright (c) 2010-2015 Clark & Parsia, LLC. <http://www.clarkparsia.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.complexible.stardog.examples.jena;
import java.io.FileInputStream;
import com.complexible.common.protocols.server.Server;
import com.complexible.stardog.Stardog;
import com.complexible.stardog.api.ConnectionConfiguration;
import com.complexible.stardog.api.Connection;
import com.complexible.stardog.api.admin.AdminConnection;
import com.complexible.stardog.api.admin.AdminConnectionConfiguration;
import com.complexible.stardog.jena.SDJenaFactory;
import com.complexible.stardog.protocols.snarl.SNARLProtocolConstants;
import org.apache.jena.query.Query;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.QueryExecutionFactory;
import org.apache.jena.query.QueryFactory;
import org.apache.jena.query.ResultSetFormatter;
import org.apache.jena.rdf.model.Model;
/**
* <p>Example of how to use the Jena integration with stardog</p>
*
* @author Michael Grove
* @since 0.3.3
* @version 4.0
*/
public class JenaExample {
// Using Stardog with the [Jena](http://jena.apache.org) API
// -------------------
// In this example we'll show how to use the Stardog Jena API bindings.
public static void main(String[] args) throws Exception {
// Creating a Server
// -----------------
// You'll need a server to connect to, obviously. For the example, lets create an embedded server.
Server aServer = Stardog
.buildServer()
.bind(SNARLProtocolConstants.EMBEDDED_ADDRESS)
.start();
try {
// Next we'll establish a admin connection to Stardog so we can create a database to use for the example
try (AdminConnection aAdminConnection = AdminConnectionConfiguration.toEmbeddedServer()
.credentials("admin", "admin")
.connect()) {
// If the database already exists, we'll drop it and create a fresh copy
if (aAdminConnection.list().contains("testJena")) {
aAdminConnection.drop("testJena");
}
aAdminConnection.createMemory("testJena");
}
// Now we open a Connection our new database
try (Connection aConn = ConnectionConfiguration
.to("testJena")
.credentials("admin", "admin")
.connect()) {
// Then we obtain a Jena `Model` for the specified stardog database which is backed by our `Connection`
Model aModel = SDJenaFactory.createModel(aConn);
// Start a transaction before adding the data. This is not required, but it is faster to group the entire add into a single transaction rather
// than rely on the auto commit of the underlying stardog connection.
aModel.begin();
// Read data into the model. note, this will add statement at a time. Bulk loading needs to be performed directly with the BulkUpdateHandler provided
// by the underlying graph, or read in files in RDF/XML format, which uses the bulk loader natively. Alternatively, you can load data into the stardog
// database using it's native API via the command line client.
aModel.getReader("N3").read(aModel, new FileInputStream("data/sp2b_10k.n3"), "");
// When you're done adding, you need to commit the changes
aModel.commit();
// Query that we will run against the data we just loaded
String aQueryString = "select * where { ?s ?p ?o. filter(?s = <http://localhost/publications/articles/Journal1/1940/Article1>).}";
// Create a query...
Query aQuery = QueryFactory.create(aQueryString);
// ... and run it
try (QueryExecution aExec = QueryExecutionFactory.create(aQuery, aModel)) {
// Now print the results
ResultSetFormatter.out(aExec.execSelect(), aModel);
}
}
}
finally {
// You must stop the server when you're done
aServer.stop();
}
}
}
@kgurses
Copy link

kgurses commented Dec 13, 2013

It's running and then terminated with those errors.

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/pc/Documents/stardog-2.0.3/client/jena/slf4j-log4j12-1.6.4.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/pc/Documents/stardog-2.0.3/server/dbms/slf4j-jdk14-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
log4j:WARN No appenders could be found for logger (org.openrdf.rio.RDFParserRegistry).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

@charbull
Copy link

Hi,
Thank you for these examples,

It would be great if we can have a pom.xml for these examples.
I am trying to guess compatible versions with openrdf, jena and sesame to work with the code examples you provided.

Thank you,

@iesnaola
Copy link

Hi,

I agree with @charbull . A pom.xml would be really helpful. I am trying to find dependencies for imports:

import com.complexible.stardog.Stardog;
import com.complexible.stardog.jena.SDJenaFactory;

BR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment