Skip to content

Instantly share code, notes, and snippets.

@schwarzmx
Created February 18, 2014 19:58
Show Gist options
  • Save schwarzmx/9078743 to your computer and use it in GitHub Desktop.
Save schwarzmx/9078743 to your computer and use it in GitHub Desktop.
// Copyright (c) 2010 - 2014, Clark & Parsia, LLC. <http://www.clarkparsia.com>
// For more information about licensing and copyright of this software, please contact
// inquiries@clarkparsia.com or visit http://stardog.com
package com.complexible.stardog.examples.api;
import java.io.FileInputStream;
import com.complexible.common.protocols.server.Server;
import com.complexible.common.rdf.query.resultio.TextTableQueryResultWriter;
import com.complexible.stardog.Stardog;
import com.complexible.stardog.api.ConnectionConfiguration;
import com.complexible.stardog.api.admin.AdminConnection;
import com.complexible.stardog.api.admin.AdminConnectionConfiguration;
import com.complexible.stardog.protocols.snarl.SNARLProtocolConstants;
import com.complexible.stardog.sesame.StardogRepository;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.query.resultio.QueryResultIO;
import org.openrdf.repository.Repository;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.rio.RDFFormat;
/**
* @author Fernando Hernandez
*/
public class JsonLdAdd {
public static void main(String[] args) throws Exception {
Server aServer = Stardog
.buildServer()
.bind(SNARLProtocolConstants.EMBEDDED_ADDRESS)
.start();
try {
AdminConnection aAdminConnection = AdminConnectionConfiguration.toEmbeddedServer()
.credentials("admin", "admin")
.connect();
if (aAdminConnection.list().contains("testJsonLd")) {
aAdminConnection.drop("testJsonLd");
}
aAdminConnection.createMemory("testJsonLd");
aAdminConnection.close();
Repository aRepo = new StardogRepository(ConnectionConfiguration
.to("testJsonLd")
.credentials("admin", "admin"));
// init the repo
aRepo.initialize();
// now you can use it like a normal Sesame Repository
RepositoryConnection aRepoConn = aRepo.getConnection();
// always best to turn off auto commit
aRepoConn.setAutoCommit(false);
aRepoConn.add(new FileInputStream("/Users/fernando/Desktop/data.json"), "http://sesame.stardog.com/", RDFFormat.JSONLD);
aRepoConn.commit();
TupleQuery aQuery = aRepoConn.prepareTupleQuery(QueryLanguage.SPARQL, "select * where { ?s ?p ?o .}");
// run the query
TupleQueryResult aResults = aQuery.evaluate();
// print the results in tabular format
QueryResultIO.write(aResults, TextTableQueryResultWriter.FORMAT, System.out);
aResults.close();
aRepoConn.close();
}
finally {
aServer.stop();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment