Skip to content

Sesame_API_embedded_mode

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

Introduction

A sample Java application demonstrates using Blazegraph™ in embedded mode with Sesame API. It uploads data from a document located in resources into a repository and executes sparql query. It was created in Eclipse IDE with Maven. If you are not familiar with Eclipse and Maven you may take a look at our step by step tutorial on how to create Java project using Blazegraph™ in Eclipse.

Download a sample application

You can download sample-sesame-embedded application here.

Code listing

package sample.sesame.embedded;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.openrdf.OpenRDFException;
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 org.openrdf.rio.RDFFormat;

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


public class SampleBlazegraphSesameEmbedded {

    protected static final Logger log = Logger.getLogger(SampleBlazegraphSesameEmbedded.class);

    public static void main(String[] args) throws IOException, OpenRDFException {

        // load journal properties from resources
        Properties props = loadProperties("/blazegraph.properties");

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

        try{
            repo.initialize();

            loadData(repo, "/data.n3", "");

            String query = "select * {<http://blazegraph.com/blazegraph> ?p ?o}";
            TupleQueryResult result = executeSelectQuery(repo, query, QueryLanguage.SPARQL);

            try {
                while(result.hasNext()){

                    BindingSet bs = result.next();
                    log.info(bs);

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

    /*
     * Load a Properties object from a file.
     */
    public static Properties loadProperties(String resource) throws IOException {
        Properties p = new Properties();
        InputStream is = SampleBlazegraphSesameEmbedded.class
                .getResourceAsStream(resource);
        p.load(new InputStreamReader(new BufferedInputStream(is)));
        return p;
    }

    /*
     * Load data from resources into a repository.
     */
    public static void loadData(Repository repo, String resource, String baseURL)
            throws OpenRDFException, IOException {

        RepositoryConnection cxn = repo.getConnection();

        try {
            cxn.begin();
            try {
                InputStream is = SampleBlazegraphSesameEmbedded.class.
                                       getResourceAsStream(resource);
                if (is == null) {
                    throw new IOException("Could not locate resource: " + resource);
                }
                Reader reader = new InputStreamReader(new BufferedInputStream(is));
                try {
                    cxn.add(reader, baseURL, RDFFormat.N3);
                } finally {
                    reader.close();
                }
                cxn.commit();
            } catch (OpenRDFException ex) {
                cxn.rollback();
                throw ex;
            }
        } finally {
            // close the repository connection
            cxn.close();
        }
    }

    /*
     * Execute sparql select query.
     */
    public static TupleQueryResult executeSelectQuery(Repository repo, String query,
            QueryLanguage ql) throws OpenRDFException  {

        RepositoryConnection cxn;
        if (repo instanceof BigdataSailRepository) {
            cxn = ((BigdataSailRepository) repo).getReadOnlyConnection();
        } else {
            cxn = repo.getConnection();
        }

        try {

            final TupleQuery tupleQuery = cxn.prepareTupleQuery(ql, query);
            tupleQuery.setIncludeInferred(true /* includeInferred */);
            return tupleQuery.evaluate();

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

Comments

Journal property file

/stc/main/resource/blazegraph.properies - is a Blazegraph™ journal property file.

com.bigdata.journal.AbstractJournal.bufferMode=DiskRW
com.bigdata.journal.AbstractJournal.file=/tmp/blazegraph/test.jnl

com.bigdata.journal.AbstractJournal.bufferMode=DiskRW -sets a journal mode. 'DiskRW' - journal will be persistent located on path specified by com.bigdata.journal.AbstractJournal.file property. You can use 'MemStore' for a journal located in memory. For details see Blazegraph javadocs com.bigdata.journal.BufferMode. com.bigdata.journal.AbstractJournal.file=/tmp/blazegraph/test.jnl - sets journal location.

Load property file from resources:

    public static Properties loadProperties(String resource) throws IOException {
        Properties p = new Properties();
        InputStream is = SampleBlazegraphSesameEmbedded.class
                .getResourceAsStream(resource);
        p.load(new InputStreamReader(new BufferedInputStream(is)));
        return p;
    }

Create a repository

See also Sesame API tutorial for more details on using Sesame with Blazegraph™

                // load journal properties from resources
        Properties props = loadProperties("/blazegraph.properties");

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

        repo.initialize();

Load data

The application uploads data from /src/main/resources/data.n3 using following procedure:

public static void loadData(Repository repo, String resource, String baseURL)
            throws OpenRDFException, IOException {

        RepositoryConnection cxn = repo.getConnection();

        try {
            cxn.begin();
            try {
                InputStream is = SampleBlazegraphSesameEmbedded.class
                        .getResourceAsStream(resource);
                if (is == null) {
                    throw new IOException("Could not locate resource: " + resource);
                }
                Reader reader = new InputStreamReader(new BufferedInputStream(is));
                try {
                    cxn.add(reader, baseURL, RDFFormat.N3);
                } finally {
                    reader.close();
                }
                cxn.commit();
            } catch (OpenRDFException ex) {
                cxn.rollback();
                throw ex;
            }
        } finally {
            // close the repository connection
            cxn.close();
        }
    }

File data.n3 contains some RDF statements in notation3:

PREFIX : <http://blazegraph.com/>
PREFIX schema: <http://schema.org/>


:systap a schema:Organization ;
        schema:owns :blazegraph .
:blazegraph a schema:Product ;
            schema:brand :systap;
            :productOf <http://systap.com/>;
            :implements <http://rdf4j.org>, <http://blueprints.tinkerpop.com> .

Query data

Function executes sparql query:

public static TupleQueryResult executeSelectQuery(Repository repo, String query,
            QueryLanguage ql) throws OpenRDFException  {

        RepositoryConnection cxn;
        if (repo instanceof BigdataSailRepository) {
            cxn = ((BigdataSailRepository) repo).getReadOnlyConnection();
        } else {
            cxn = repo.getConnection();
        }

        try {

            final TupleQuery tupleQuery = cxn.prepareTupleQuery(ql, query);
            tupleQuery.setIncludeInferred(true /* includeInferred */);
            return tupleQuery.evaluate();

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

Query result iteration

                TupleQueryResult result = executeSelectQuery(repo, query, QueryLanguage.SPARQL);

        try {
                while(result.hasNext()){
                        BindingSet bs = result.next();
                    log.info(bs);
                }
            } finally {
                result.close();
            }

Program output

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
Wed Apr 22 16:34:36 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=229890224
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-embedded/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/implements;o=http://blueprints.tinkerpop.com]
[p=http://blazegraph.com/implements;o=http://rdf4j.org]
[p=http://blazegraph.com/productOf;o=http://systap.com/]
[p=http://schema.org/brand;o=http://blazegraph.com/systap]
[p=http://www.w3.org/1999/02/22-rdf-syntax-ns#type;o=http://schema.org/Product]
Clone this wiki locally