diff --git a/neptune-export/pom.xml b/neptune-export/pom.xml index 60c2e885e4..11bb71f378 100644 --- a/neptune-export/pom.xml +++ b/neptune-export/pom.xml @@ -74,9 +74,36 @@ org.eclipse.rdf4j rdf4j-runtime - 2.2 + [2.4.0,) + + org.eclipse.rdf4j + rdf4j-rio-turtle + 2.4.3 + + + + org.slf4j + slf4j-api + 1.8.0-beta2 + + + + org.slf4j + jcl-over-slf4j + 1.8.0-beta2 + + + + org.slf4j + slf4j-simple + 1.8.0-beta2 + + + + + diff --git a/neptune-export/src/main/java/com/amazonaws/services/neptune/NeptuneExportCli.java b/neptune-export/src/main/java/com/amazonaws/services/neptune/NeptuneExportCli.java index e16cb4ab8e..0f5db2570b 100644 --- a/neptune-export/src/main/java/com/amazonaws/services/neptune/NeptuneExportCli.java +++ b/neptune-export/src/main/java/com/amazonaws/services/neptune/NeptuneExportCli.java @@ -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 cli = new com.github.rvesse.airline.Cli<>(NeptuneExportCli.class); diff --git a/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/NeptuneSparqlClient.java b/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/NeptuneSparqlClient.java index 9b97b46d3c..a437728742 100644 --- a/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/NeptuneSparqlClient.java +++ b/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/NeptuneSparqlClient.java @@ -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; @@ -42,11 +48,17 @@ private NeptuneSparqlClient(List 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() { diff --git a/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/Prefixes.java b/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/Prefixes.java index 64dc01f9b1..a95774b5cb 100644 --- a/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/Prefixes.java +++ b/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/Prefixes.java @@ -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; @@ -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); diff --git a/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/StatementHandler.java b/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/StatementHandler.java deleted file mode 100644 index 721b7bb500..0000000000 --- a/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/StatementHandler.java +++ /dev/null @@ -1,94 +0,0 @@ -/* -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; - -import com.amazonaws.services.neptune.rdf.io.StatementsPrinter; -import org.eclipse.rdf4j.model.Statement; -import org.eclipse.rdf4j.model.Value; - -public class StatementHandler { - - public static StatementHandler create(Prefixes prefixes){ - return new StatementHandler(prefixes, null, null, null, null, null); - } - - private final Prefixes prefixes; - private final String prevSubject; - private final String prevPredicate; - private final String subject; - private final String predicate; - private final String object; - - private StatementHandler(Prefixes prefixes, - String prevSubject, - String prevPredicate, - String subject, - String predicate, - String object) { - this.prefixes = prefixes; - this.prevSubject = prevSubject; - this.prevPredicate = prevPredicate; - this.subject = subject; - this.predicate = predicate; - this.object = object; - } - - public StatementHandler handle(Statement statement){ - return new StatementHandler( - prefixes, subject, - predicate, - prefixes.formatIRI(statement.getSubject().stringValue()), - prefixes.formatIRI(statement.getPredicate().toString()), - formatObject(statement.getObject())); - } - - public void printTo(StatementsPrinter printer){ - if (subject == null || predicate == null || object == null){ - throw new IllegalStateException("Statement handler has not been initialized with a statement"); - } - - if (prevSubject == null) { - printer.printFirstLine(subject, predicate); - } else if (prevSubject.equals(subject)) { - if (prevPredicate.equals(predicate)) { - printer.printObjectList(); - } else { - printer.printPredicateList(subject, predicate); - } - } else { - printer.printTriple(subject, predicate); - } - - printer.printObject(object); - } - - private String formatObject(Value v) { - - String s1 = v.toString(); - String s2 = v.stringValue(); - - if (s1.equals(s2)) { - return prefixes.formatIRI(s1); - } else { - return formatValue(s1); - } - } - - private String formatValue(String v) { - if (v.contains("^^ "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); } } diff --git a/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/io/StatementsPrinter.java b/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/io/StatementsPrinter.java deleted file mode 100644 index de98c483fb..0000000000 --- a/neptune-export/src/main/java/com/amazonaws/services/neptune/rdf/io/StatementsPrinter.java +++ /dev/null @@ -1,67 +0,0 @@ -/* -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 org.apache.commons.lang.StringUtils; - -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; -import java.nio.file.Path; - -public class StatementsPrinter implements AutoCloseable { - private final PrintWriter writer; - - public StatementsPrinter(Path filePath) throws IOException { - this.writer = new PrintWriter(new FileWriter(filePath.toFile())); - } - - public void printObject(String o) { - writer.print(o); - } - - public void printTriple(String subject, String predicate) { - writer.print(" ."); - writer.print(System.lineSeparator()); - writer.print(subject); - writer.print(" "); - writer.print(predicate); - writer.print(" "); - } - - public void printFirstLine(String subject, String predicate) { - writer.print(subject); - writer.print(" "); - writer.print(predicate); - writer.print(" "); - } - - public void printObjectList() { - writer.print(", "); - } - - public void printPredicateList(String subject, String predicate) { - writer.print(" ;"); - writer.print(System.lineSeparator()); - writer.print(StringUtils.repeat(" ", subject.length() + 1)); - writer.print(predicate); - writer.print(" "); - } - - @Override - public void close() throws Exception { - writer.print(" ."); - writer.print(System.lineSeparator()); - writer.close(); - } -}