Search in sources :

Example 1 with CountingReader

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);
    }
}
Also used : Collections.emptyMap(java.util.Collections.emptyMap) FileUtils(apoc.util.FileUtils) java.util(java.util) Meta(apoc.meta.Meta) Context(org.neo4j.procedure.Context) Collections.emptyList(java.util.Collections.emptyList) IOException(java.io.IOException) Description(org.neo4j.procedure.Description) CountingReader(apoc.export.util.CountingReader) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) Stream(java.util.stream.Stream) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) CSVReader(au.com.bytecode.opencsv.CSVReader) Util.cleanUrl(apoc.util.Util.cleanUrl) Arrays.asList(java.util.Arrays.asList) Name(org.neo4j.procedure.Name) StreamSupport(java.util.stream.StreamSupport) Pattern(java.util.regex.Pattern) Util(apoc.util.Util) Procedure(org.neo4j.procedure.Procedure) CountingReader(apoc.export.util.CountingReader) CSVReader(au.com.bytecode.opencsv.CSVReader) IOException(java.io.IOException) Collections.emptyMap(java.util.Collections.emptyMap) Description(org.neo4j.procedure.Description) Procedure(org.neo4j.procedure.Procedure)

Example 2 with CountingReader

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);
    }
}
Also used : CountingReader(apoc.export.util.CountingReader) CountingReader(apoc.export.util.CountingReader)

Aggregations

CountingReader (apoc.export.util.CountingReader)2 Meta (apoc.meta.Meta)1 FileUtils (apoc.util.FileUtils)1 Util (apoc.util.Util)1 Util.cleanUrl (apoc.util.Util.cleanUrl)1 CSVReader (au.com.bytecode.opencsv.CSVReader)1 IOException (java.io.IOException)1 java.util (java.util)1 Arrays.asList (java.util.Arrays.asList)1 Collections.emptyList (java.util.Collections.emptyList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Consumer (java.util.function.Consumer)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)1 Context (org.neo4j.procedure.Context)1 Description (org.neo4j.procedure.Description)1 Name (org.neo4j.procedure.Name)1