-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #1257: apoc.load.json file roundtrip from apoc.export.json.all
- Loading branch information
Showing
6 changed files
with
590 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package apoc.export.json; | ||
|
||
import apoc.export.util.CountingReader; | ||
import apoc.export.util.ProgressReporter; | ||
import apoc.result.ProgressInfo; | ||
import apoc.util.FileUtils; | ||
import apoc.util.JsonUtil; | ||
import apoc.util.Util; | ||
import org.neo4j.graphdb.GraphDatabaseService; | ||
import org.neo4j.procedure.Context; | ||
import org.neo4j.procedure.Description; | ||
import org.neo4j.procedure.Mode; | ||
import org.neo4j.procedure.Name; | ||
import org.neo4j.procedure.Procedure; | ||
|
||
import java.util.Map; | ||
import java.util.Scanner; | ||
import java.util.stream.Stream; | ||
|
||
public class ImportJson { | ||
@Context | ||
public GraphDatabaseService db; | ||
|
||
@Procedure(value = "apoc.import.json.all", mode = Mode.WRITE) | ||
@Description("apoc.import.json.all(file,config) - imports the json list to the provided file") | ||
public Stream<ProgressInfo> all(@Name("file") String fileName, @Name(value = "config", defaultValue = "{}") Map<String, Object> config) { | ||
ProgressInfo result = | ||
Util.inThread(() -> { | ||
ImportJsonConfig importJsonConfig = new ImportJsonConfig(config); | ||
ProgressReporter reporter = new ProgressReporter(null, null, new ProgressInfo(fileName, "file", "json")); | ||
|
||
try (final CountingReader reader = FileUtils.readerFor(fileName); | ||
final Scanner scanner = new Scanner(reader).useDelimiter("\n|\r"); | ||
JsonImporter jsonImporter = new JsonImporter(importJsonConfig, db, reporter)) { | ||
while (scanner.hasNext()) { | ||
Map<String, Object> row = JsonUtil.OBJECT_MAPPER.readValue(scanner.nextLine(), Map.class); | ||
jsonImporter.importRow(row); | ||
} | ||
} | ||
|
||
return reporter.getTotal(); | ||
}); | ||
return Stream.of(result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package apoc.export.json; | ||
|
||
import apoc.util.Util; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
public class ImportJsonConfig { | ||
|
||
private final Map<String, Map<String, String>> nodePropertyMappings; | ||
private final Map<String, Map<String, String>> relPropertyMappings; | ||
|
||
private final int unwindBatchSize; | ||
private final int txBatchSize; | ||
|
||
private final String importIdName; | ||
|
||
public ImportJsonConfig(Map<String, Object> config) { | ||
config = config == null ? Collections.emptyMap() : config; | ||
this.nodePropertyMappings = (Map<String, Map<String, String>>) config.getOrDefault("nodePropertyMappings", Collections.emptyMap()); | ||
this.relPropertyMappings = (Map<String, Map<String, String>>) config.getOrDefault("relPropertyMappings", Collections.emptyMap()); | ||
this.unwindBatchSize = Util.toInteger(config.getOrDefault("unwindBatchSize", 5000)); | ||
this.txBatchSize = Util.toInteger(config.getOrDefault("txBatchSize", 5000)); | ||
this.importIdName = (String) config.getOrDefault("importIdName", "neo4jImportId"); | ||
} | ||
|
||
public String typeForNode(Collection<String> labels, String property) { | ||
return labels.stream() | ||
.map(label -> nodePropertyMappings.getOrDefault(label, Collections.emptyMap()).get(property)) | ||
.filter(StringUtils::isNotBlank) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
|
||
public String typeForRel(String type, String property) { | ||
return relPropertyMappings.getOrDefault(type, Collections.emptyMap()).get(property); | ||
} | ||
|
||
public int getUnwindBatchSize() { | ||
return unwindBatchSize; | ||
} | ||
|
||
public int getTxBatchSize() { | ||
return txBatchSize; | ||
} | ||
|
||
public String getImportIdName() { | ||
return importIdName; | ||
} | ||
} |
Oops, something went wrong.