use of apoc.export.util.CountingReader in project neo4j-apoc-procedures by neo4j-contrib.
the class LoadCsv method csv.
@Procedure
@Description("apoc.load.csv('url',{config}) YIELD lineNo, list, map - load CSV fom URL as stream of values,\n config contains any of: {skip:1,limit:5,header:false,sep:'TAB',ignore:['tmp'],arraySep:';',mapping:{years:{type:'int',arraySep:'-',array:false,name:'age',ignore:false}}")
public Stream<CSVResult> csv(@Name("url") String url, @Name("config") Map<String, Object> config) {
boolean failOnError = booleanValue(config, "failOnError", true);
try {
CountingReader reader = FileUtils.readerFor(url);
char separator = separator(config, "sep", DEFAULT_SEP);
char arraySep = separator(config, "arraySep", DEFAULT_ARRAY_SEP);
long skip = longValue(config, "skip", 0L);
boolean hasHeader = booleanValue(config, "header", true);
long limit = longValue(config, "limit", Long.MAX_VALUE);
EnumSet<Results> results = EnumSet.noneOf(Results.class);
for (String result : value(config, "results", asList("map", "list"))) {
results.add(Results.valueOf(result));
}
List<String> ignore = value(config, "ignore", emptyList());
List<String> nullValues = value(config, "nullValues", emptyList());
Map<String, Map<String, Object>> mapping = value(config, "mapping", Collections.emptyMap());
Map<String, Mapping> mappings = createMapping(mapping, arraySep, ignore);
CSVReader csv = new CSVReader(reader, separator);
String[] header = getHeader(hasHeader, csv, ignore, mappings);
boolean checkIgnore = !ignore.isEmpty() || mappings.values().stream().anyMatch(m -> m.ignore);
return StreamSupport.stream(new CSVSpliterator(csv, header, url, skip, limit, checkIgnore, mappings, nullValues, results), false);
} catch (IOException e) {
if (!failOnError)
return Stream.of(new CSVResult(new String[0], new String[0], 0, true, Collections.emptyMap(), emptyList(), EnumSet.noneOf(Results.class)));
else
throw new RuntimeException("Can't read CSV from URL " + cleanUrl(url), e);
}
}
use of apoc.export.util.CountingReader in project neo4j-apoc-procedures by neo4j-contrib.
the class FileUtils method readHdfs.
private static CountingReader readHdfs(String fileName) {
try {
StreamConnection streamConnection = HDFSUtils.readFile(fileName);
Reader reader = new BufferedReader(new InputStreamReader(streamConnection.getInputStream(), "UTF-8"));
return new CountingReader(reader, streamConnection.getLength());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations