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

Introduction

A sample Java application demonstrates using RDF* and SPARQL* with Blazegraph™. See Reification Done Right for details.

Download a sample application

You can download the sample-rdr application here.

Code listing

import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.openrdf.model.Statement;
import org.openrdf.query.BindingSet;
import org.openrdf.query.GraphQueryResult;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.rio.RDFFormat;

import com.bigdata.rdf.sail.BigdataSail.Options;
import com.bigdata.rdf.sail.webapp.SD;
import com.bigdata.rdf.sail.webapp.client.IPreparedTupleQuery;
import com.bigdata.rdf.sail.webapp.client.RemoteRepository;
import com.bigdata.rdf.sail.webapp.client.RemoteRepository.AddOp;
import com.bigdata.rdf.sail.webapp.client.RemoteRepositoryManager;

public class SampleBlazegraphRDR {

    protected static final Logger log = Logger.getLogger(SampleBlazegraphRDR.class);
    private static final String sparqlEndPoint = "http://localhost:9999/bigdata";

    public static void main(String[] args) throws Exception  {

        final RemoteRepositoryManager repositoryManager = new RemoteRepositoryManager(sparqlEndPoint, true /*useLBS*/);

        try{

            final String namespace = "namespaceRDR";
            final Properties properties = new Properties();
            properties.setProperty(Options.NAMESPACE, namespace);
            properties.setProperty(Options.STATEMENT_IDENTIFIERS, "true");

            if(!namespaceExists(namespace, repositoryManager)){
                log.info(String.format("Create namespace %s...", namespace));
                repositoryManager.createRepository(namespace, properties);
                log.info(String.format("Create namespace %s done", namespace));
            }

            InputStream is = SampleBlazegraphRDR.class.getResourceAsStream("/rdr_test.ttl");
            repositoryManager.getRepositoryForNamespace(namespace).add(new AddOp(is, RDFFormat.forMIMEType("application/x-turtle-RDR")));

            //execute query
            RemoteRepository r = repositoryManager.getRepositoryForNamespace(namespace);
            IPreparedTupleQuery query = r.prepareTupleQuery("SELECT ?age ?src WHERE {?bob foaf:name \"Bob\" . <<?bob foaf:age ?age>> dc:source ?src .}");
            TupleQueryResult result = query.evaluate();
            try {
                while (result.hasNext()) {
                    BindingSet bs = result.next();
                    log.info(bs);
                }
            } finally {
                result.close();
            }
        } finally {
            repositoryManager.close();
        }
    }

    private static boolean namespaceExists(String namespace, RemoteRepositoryManager repo) throws Exception{
        GraphQueryResult res = repo.getRepositoryDescriptions();
        try{
            while(res.hasNext()){
                Statement stmt = res.next();
                if (stmt.getPredicate().toString().equals(SD.KB_NAMESPACE.stringValue())) {
                    if(namespace.equals(stmt.getObject().stringValue())){
                        log.info(String.format("Namespace %s already exists", namespace));
                        return true;
                    }
                }
            }
        } finally {
            res.close();
        }
        return false;
    }

}

Comments

Create a remote repository

    final RemoteRepositoryManager repositoryManager = new RemoteRepositoryManager(sparqlEndPoint, true /*useLBS*/);

Create a namespace

                        final String namespace = "namespaceRDR";
            final Properties properties = new Properties();
            properties.setProperty(Options.NAMESPACE, namespace);
            properties.setProperty(Options.STATEMENT_IDENTIFIERS, "true");

            if(!namespaceExists(namespace, repositoryManager)){
                log.info(String.format("Create namespace %s...", namespace));
                repositoryManager.createRepository(namespace, properties);
                log.info(String.format("Create namespace %s done", namespace));
            }

Check a namespace existence:

private static boolean namespaceExists(String namespace, RemoteRepositoryManager repo) throws Exception{
        GraphQueryResult res = repo.getRepositoryDescriptions();
        try{
            while(res.hasNext()){
                Statement stmt = res.next();
                if (stmt.getPredicate().toString().equals(SD.KB_NAMESPACE.stringValue())) {
                    if(namespace.equals(stmt.getObject().stringValue())){
                        log.info(String.format("Namespace %s already exists", namespace));
                        return true;
                    }
                }
            }
        } finally {
            res.close();
        }
        return false;
    }

Load data

File src/main/resources/rdr_test.ttl

@prefix : <http://bigdata.com> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix dc:  <http://purl.org/dc/elements/1.1/> .

:bob foaf:name "Bob" .
<<:bob foaf:age 23>> dc:creator <http://example.com/crawlers#c1> ;
                     dc:source <http://example.net/homepage-listing.html> .

Load file to a repository:

InputStream is = SampleBlazegraphRDR.class.getResourceAsStream("/rdr_test.ttl");
repositoryManager.getRepositoryForNamespace(namespace).add(new AddOp(is, RDFFormat.forMIMEType("application/x-turtle-RDR")));

Query data

RemoteRepository r = repositoryManager.getRepositoryForNamespace(namespace);
            IPreparedTupleQuery query = r.prepareTupleQuery("SELECT ?age ?src WHERE {?bob foaf:name \"Bob\" . <<?bob foaf:age ?age>> dc:source ?src .}");
            TupleQueryResult result = query.evaluate();
            try {
                while (result.hasNext()) {
                    BindingSet bs = result.next();
                    log.info(bs);
                }
            } finally {
                result.close();
            }
        } finally {
            repositoryManager.close();
        }

Program output

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
Create namespace namespaceRDR...
Create namespace namespaceRDR done
[src=http://example.net/homepage-listing.html;age="23"^^<http://www.w3.org/2001/XMLSchema#integer>]
Clone this wiki locally