Skip to content

Commit

Permalink
Merge pull request awslabs#18 from iansrobinson/master
Browse files Browse the repository at this point in the history
Update rdf4j version
  • Loading branch information
beebs-systap authored Jan 23, 2019
2 parents 47080ae + 6c064cd commit f9a451a
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 186 deletions.
29 changes: 28 additions & 1 deletion neptune-export/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,36 @@
<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-runtime</artifactId>
<version>2.2</version>
<version>[2.4.0,)</version>
</dependency>

<dependency>
<groupId>org.eclipse.rdf4j</groupId>
<artifactId>rdf4j-rio-turtle</artifactId>
<version>2.4.3</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.8.0-beta2</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.8.0-beta2</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.8.0-beta2</version>
</dependency>




</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
}))
public class NeptuneExportCli {

static {
System.setProperty("org.apache.commons.logging.Log",
"org.apache.commons.logging.impl.NoOpLog");
}

public static void main(String[] args) {
com.github.rvesse.airline.Cli<Runnable> cli = new com.github.rvesse.airline.Cli<>(NeptuneExportCli.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@

package com.amazonaws.services.neptune.rdf;

import com.amazonaws.services.neptune.rdf.io.EnhancedTurtleWriter;
import org.eclipse.rdf4j.query.GraphQuery;
import org.eclipse.rdf4j.query.GraphQueryResult;
import org.eclipse.rdf4j.repository.RepositoryConnection;
import org.eclipse.rdf4j.repository.base.AbstractRepository;
import org.eclipse.rdf4j.repository.sparql.SPARQLRepository;
import org.eclipse.rdf4j.rio.WriterConfig;
import org.joda.time.DateTime;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
import java.util.Random;
Expand All @@ -42,11 +48,17 @@ private NeptuneSparqlClient(List<SPARQLRepository> repositories) {
this.repositories = repositories;
}

public GraphQueryResult executeQuery(String sparql) {
try (RepositoryConnection connection = chooseRepository().getConnection()) {
GraphQuery query = connection.prepareGraphQuery(sparql);
return query.evaluate();
public void executeQuery(String sparql, Path file) throws IOException {
Prefixes prefixes = new Prefixes();

try (RepositoryConnection connection = chooseRepository().getConnection();
Writer fileWriter = new FileWriter(file.toFile())) {

EnhancedTurtleWriter writer = new EnhancedTurtleWriter(fileWriter, prefixes);
connection.prepareGraphQuery(sparql).evaluate(writer);
}

prefixes.addTo(file);
}

private SPARQLRepository chooseRepository() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.LineIterator;
import org.eclipse.rdf4j.rio.RDFWriter;

import java.io.BufferedWriter;
import java.io.File;
Expand Down Expand Up @@ -55,6 +56,21 @@ public String formatIRI(String s) {
}
}

public void parse(String s, RDFWriter writer) {

int i = s.indexOf("#");

if (i > 0 && i < (s.length() - 1)) {
String uri = s.substring(0, i + 1);

if (!prefixes.containsKey(uri)) {
String prefix = "s" + (prefixes.size() - offset);
prefixes.put(uri, prefix);
writer.handleNamespace(prefix, uri);
}
}
}

public void addTo(Path filePath) throws IOException {
File source = filePath.toFile();
LineIterator lineIterator = FileUtils.lineIterator(source);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License.
A copy of the License is located at
http://www.apache.org/licenses/LICENSE-2.0
or in the "license" file accompanying this file. This file 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.amazonaws.services.neptune.rdf.io;

import com.amazonaws.services.neptune.io.Status;
import com.amazonaws.services.neptune.rdf.Prefixes;
import org.eclipse.rdf4j.model.Statement;
import org.eclipse.rdf4j.rio.RDFHandlerException;
import org.eclipse.rdf4j.rio.turtle.TurtleWriter;

import java.io.IOException;
import java.io.Writer;

public class EnhancedTurtleWriter extends TurtleWriter {

private final Prefixes prefixes;
private final Status status = new Status();

public EnhancedTurtleWriter(Writer writer, Prefixes prefixes) {
super(writer);
this.prefixes = prefixes;
}

@Override
public void handleStatement(Statement statement) throws RDFHandlerException {

prefixes.parse(statement.getSubject().stringValue(), this);
prefixes.parse(statement.getPredicate().toString(), this);
prefixes.parse(statement.getObject().stringValue(), this);

super.handleStatement(statement);

status.update();
}

@Override
protected void writeNamespace(String prefix, String name)
throws IOException {
// Do nothing
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
package com.amazonaws.services.neptune.rdf.io;

import com.amazonaws.services.neptune.io.Directories;
import com.amazonaws.services.neptune.io.Status;
import com.amazonaws.services.neptune.rdf.NeptuneSparqlClient;
import com.amazonaws.services.neptune.rdf.Prefixes;
import com.amazonaws.services.neptune.rdf.StatementHandler;
import org.eclipse.rdf4j.query.GraphQueryResult;

public class ExportRdfGraphJob {

Expand All @@ -37,22 +34,6 @@ public void execute() throws Exception {
java.nio.file.Path filePath = directories.createFilePath(
directories.statementsDirectory(), "statements", 0, () -> "ttl");

try (StatementsPrinter printer = new StatementsPrinter(filePath)) {

Status status = new Status();
StatementHandler handler = StatementHandler.create(prefixes);

GraphQueryResult result = client.executeQuery("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }");

while (result.hasNext()) {

handler = handler.handle(result.next());
handler.printTo(printer);

status.update();
}
}

prefixes.addTo(filePath);
client.executeQuery("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }", filePath);
}
}

This file was deleted.

0 comments on commit f9a451a

Please sign in to comment.