Skip to content

First_Application_Tutorial

Brad Bebee edited this page Feb 13, 2020 · 1 revision

Create maven project in Eclipse using Blazegraph in embedded mode with Sesame API. You need Eclipse IDE for Java Developer with Maven integration to begin.

Step 1: Create a new project

Select File->New->Project and choose Maven project in a dialog window.

Screen1.png

Click Next and make you sure the option 'Create a simple project' is checked in the dialog box:

Screen2.png

Click 'Next' again. In the next dialog box specify Maven project details:

Screen3.png

Click 'Finish'. Now you have an Eclipse Maven project.

Step 2: Add Blazegraph dependency to the POM

Open the pom.xml file of your project and add the following lines:

    <repositories>
        <repository>
            <id>bigdata.releases</id>
            <url>http://www.systap.com/maven/releases</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.bigdata</groupId>
            <artifactId>bigdata</artifactId>
            <version>1.5.1</version>
        </dependency>
    </dependencies>

Screen4.png

Save pom.xml. All necessary Blazegraph dependencies will be automatically downloaded, updated, and have their sub-dependencies managed by Maven.

Screen5.png

Step 3: Configuring the logger

Create file log4j.properties in /src/main/resources folder. Copy the following code in it.

log4j.rootCategory=WARN, dest1

log4j.logger.com.bigdata=ERROR
log4j.logger.com.blazegraph.sample-sesame-local=INFO

log4j.appender.dest1=org.apache.log4j.ConsoleAppender
log4j.appender.dest1.layout=org.apache.log4j.PatternLayout

Step 4: Programming first application using Blazegraph

We are going to create a simple application which uploads a statement to the Blazegraph local store:

<http://blazegraph.com/Blazegraph> <http://blazegraph.com/says> "hello"^^<http://www.w3.org/2001/XMLSchema#string> .

Then we want to retrieve it by evaluating a SPARQL query:

select ?p ?o where {<http://blazegraph.com/Blazegraph> ?p ?o .}

We will connect to Blazegraph using Sesame API.

Right-click on src/main/java and select New->Class. In the dialog fill in the class name (HelloBlazegraph in example) and make sure public static void main (String[] args) is checked.

Screen6.png

Click 'Finish' and Eclipse will create the new class and automatically open it in an editor.

Screen7.png

Insert the following code to HelloBlazegraph.java.

import java.util.Properties;

import org.openrdf.OpenRDFException;
import org.openrdf.model.Literal;
import org.openrdf.model.Statement;
import org.openrdf.model.impl.LiteralImpl;
import org.openrdf.model.impl.StatementImpl;
import org.openrdf.model.impl.URIImpl;
import org.openrdf.query.BindingSet;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;

import com.bigdata.journal.Options;
import com.bigdata.rdf.sail.BigdataSail;
import com.bigdata.rdf.sail.BigdataSailRepository;



public class HelloBlazegraph {
    public static void main(String[] args) throws OpenRDFException {

        final Properties props = new Properties();
        props.put(Options.BUFFER_MODE, "DiskRW"); // persistent file system located journal
        props.put(Options.FILE, "/tmp/blazegraph/test.jnl"); // journal file location

        final BigdataSail sail = new BigdataSail(props); // instantiate a sail
        final Repository repo = new BigdataSailRepository(sail); // create a Sesame repository

        repo.initialize();

        try {
            // prepare a statement
            URIImpl subject = new URIImpl("http://blazegraph.com/Blazegraph");
            URIImpl predicate = new URIImpl("http://blazegraph.com/says");
            Literal object = new LiteralImpl("hello");
            Statement stmt = new StatementImpl(subject, predicate, object);

            // open repository connection
            RepositoryConnection cxn = repo.getConnection();

            // upload data to repository
            try {
                cxn.begin();
                cxn.add(stmt);
                cxn.commit();
            } catch (OpenRDFException ex) {
                cxn.rollback();
                throw ex;
            } finally {
                // close the repository connection
                cxn.close();
            }

            // open connection
            if (repo instanceof BigdataSailRepository) {
                cxn = ((BigdataSailRepository) repo).getReadOnlyConnection();
            } else {
                cxn = repo.getConnection();
            }

            // evaluate sparql query
            try {

                final TupleQuery tupleQuery = cxn
                        .prepareTupleQuery(QueryLanguage.SPARQL,
                                "select ?p ?o where { <http://blazegraph.com/Blazegraph> ?p ?o . }");
                TupleQueryResult result = tupleQuery.evaluate();
                try {
                    while (result.hasNext()) {
                        BindingSet bindingSet = result.next();
                        System.err.println(bindingSet);
                    }
                } finally {
                    result.close();
                }

            } finally {
                // close the repository connection
                cxn.close();
            }

        } finally {
            repo.shutDown();
        }
    }
}

After running the application, you will see the following in your Eclipse console :

BlazeGraph(TM) Graph Engine

                   Flexible
                   Reliable
                  Affordable
      Web-Scale Computing for the Enterprise

Copyright SYSTAP, LLC 2006-2015.  All rights reserved.

192.168.3.3
Tue Apr 21 17:31:49 YEKT 2015
Linux/3.13.0-49-generic amd64
AMD FX(tm)-6100 Six-Core Processor Family 21 Model 1 Stepping 2, AuthenticAMD #CPU=6
Oracle Corporation 1.8.0_40
freeMemory=237754792
buildVersion=1.5.1

Dependency         License
ICU                http://source.icu-project.org/repos/icu/icu/trunk/license.html
bigdata-ganglia    http://www.apache.org/licenses/LICENSE-2.0.html
blueprints-core    https://github.com/tinkerpop/blueprints/blob/master/LICENSE.txt
colt               http://acs.lbl.gov/software/colt/license.html
commons-codec      http://www.apache.org/licenses/LICENSE-2.0.html
commons-fileupload http://www.apache.org/licenses/LICENSE-2.0.html
commons-io         http://www.apache.org/licenses/LICENSE-2.0.html
commons-logging    http://www.apache.org/licenses/LICENSE-2.0.html
dsiutils           http://www.gnu.org/licenses/lgpl-2.1.html
fastutil           http://www.apache.org/licenses/LICENSE-2.0.html
flot               http://www.opensource.org/licenses/mit-license.php
high-scale-lib     http://creativecommons.org/licenses/publicdomain
httpclient         http://www.apache.org/licenses/LICENSE-2.0.html
httpclient-cache   http://www.apache.org/licenses/LICENSE-2.0.html
httpcore           http://www.apache.org/licenses/LICENSE-2.0.html
httpmime           http://www.apache.org/licenses/LICENSE-2.0.html
jackson-core       http://www.apache.org/licenses/LICENSE-2.0.html
jetty              http://www.apache.org/licenses/LICENSE-2.0.html
jquery             https://github.com/jquery/jquery/blob/master/MIT-LICENSE.txt
log4j              http://www.apache.org/licenses/LICENSE-2.0.html
lucene             http://www.apache.org/licenses/LICENSE-2.0.html
nanohttp           http://elonen.iki.fi/code/nanohttpd/#license
rexster-core       https://github.com/tinkerpop/rexster/blob/master/LICENSE.txt
river              http://www.apache.org/licenses/LICENSE-2.0.html
servlet-api        http://www.apache.org/licenses/LICENSE-2.0.html
sesame             http://www.openrdf.org/download.jsp
slf4j              http://www.slf4j.org/license.html
zookeeper          http://www.apache.org/licenses/LICENSE-2.0.html

INFO: com.bigdata.util.config.LogUtil: Configure: file:/home/maria/workspace_sample/sample-sesame-local/target/classes/log4j.properties
New service class org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONWriterFactory replaces existing service class com.bigdata.rdf.rio.json.BigdataSPARQLResultsJSONWriterFactory
New service class org.openrdf.query.resultio.sparqljson.SPARQLResultsJSONParserFactory replaces existing service class com.bigdata.rdf.rio.json.BigdataSPARQLResultsJSONParserFactory
[p=http://blazegraph.com/says;o="hello"]

Screen8.png

Clone this wiki locally