Search in sources :

Example 56 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class BiocacheLegendObject method loadFromCsv.

private void loadFromCsv(String legend) {
    List<String[]> csv = null;
    try {
        CSVReader csvReader = new CSVReader(new StringReader(legend));
        csv = csvReader.readAll();
        csvReader.close();
    } catch (IOException ex) {
        LOGGER.error("error reading legend: ", ex);
    }
    boolean isDecade = colourMode.startsWith(StringConstants.OCCURRENCE_YEAR_DECADE) || StringConstants.DECADE.equals(colourMode);
    boolean isYear = colourMode.contains(StringConstants.OCCURRENCE_YEAR) && !isDecade;
    int count = 0;
    int sum = 0;
    String colour = null;
    String line = null;
    StringBuilder sb = new StringBuilder();
    sb.append("name,red,green,blue,count");
    categoryNameOrder = new String[csv.size() - 1];
    for (int i = 1; i < csv.size(); i++) {
        String[] c = csv.get(i);
        String[] p = (i > 1) ? csv.get(i - 1) : null;
        if (isYear) {
            c[0] = c[0].replace(StringConstants.DATE_TIME_BEGINNING_OF_YEAR, "");
            c[0] = c[0].replace(StringConstants.DATE_TIME_END_OF_YEAR, "");
        } else if (isDecade) {
            for (int j = 0; j <= 9; j++) {
                c[0] = c[0].replace(j + StringConstants.DATE_TIME_BEGINNING_OF_YEAR, "0");
                c[0] = c[0].replace(j + StringConstants.DATE_TIME_END_OF_YEAR, "0");
            }
        }
        int rc = Integer.parseInt(c[4]);
        if (rc == 0) {
            continue;
        }
        int[] value = { readColour(c[1], c[2], c[3]), rc };
        categories.put(c[0], value);
        categoryNameOrder[i - 1] = c[0];
        double d = Double.NaN;
        try {
            d = Double.parseDouble(c[0]);
        } catch (Exception e) {
        // will use default Double.NaN if parse fails
        }
        categoriesNumeric.put((float) d, value);
        // check for endpoint (repitition of colour)
        if (p != null && c.length > 4 && p.length > 4 && p[1].equals(c[1]) && p[2].equals(c[2]) && p[3].equals(c[3]) && !isDecade && !isYear) {
            if (count == 0) {
                count = 1;
                sum = Integer.parseInt(p[4]);
            }
            count++;
            sum += Integer.parseInt(c[4]);
        } else {
            sb.append("\n");
            colour = c[1] + "," + c[2] + "," + c[3];
            line = "\"" + c[0].replace("\"", "\"\"") + "\"," + colour + "," + c[4];
            sb.append(line);
        }
    }
    // replace last line
    if (count > 0) {
        csvLegend = sb.toString().replace(line, count + " more" + "," + colour + "," + sum);
    } else {
        csvLegend = sb.toString();
    }
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) StringReader(java.io.StringReader) IOException(java.io.IOException) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException)

Example 57 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class CommonData method getSpeciesListCounts.

public static Map getSpeciesListCounts(boolean refresh) {
    if (speciesListCounts == null || refresh) {
        Map m = new HashMap();
        HttpClient client = new HttpClient();
        String url = biocacheServer + "/occurrences/facets/download?facets=species_guid&count=true" + "&q=geospatial_kosher:*";
        LOGGER.debug(url);
        GetMethod get = new GetMethod(url);
        try {
            client.executeMethod(get);
            CSVReader csv = new CSVReader(new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream())));
            String[] row;
            // skip header
            csv.readNext();
            while ((row = csv.readNext()) != null) {
                try {
                    m.put(row[0], Long.parseLong(row[1]));
                } catch (Exception e) {
                    LOGGER.error("error getting species_guid,count: " + url, e);
                }
            }
        } catch (Exception e) {
            LOGGER.error("error getting species list from: " + url);
        }
        speciesListCountsUpdated = System.currentTimeMillis();
        speciesListCounts = m;
    }
    return speciesListCounts;
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 58 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project spatial-portal by AtlasOfLivingAustralia.

the class CommonData method getSpeciesListCountsKosher.

public static Map getSpeciesListCountsKosher(boolean refresh) {
    if (speciesListCountsKosher == null || refresh) {
        Map m = new HashMap();
        HttpClient client = new HttpClient();
        String url = biocacheServer + "/occurrences/facets/download?facets=species_guid&count=true" + "&q=geospatial_kosher:true";
        LOGGER.debug(url);
        GetMethod get = new GetMethod(url);
        try {
            client.executeMethod(get);
            CSVReader csv = new CSVReader(new BufferedReader(new InputStreamReader(get.getResponseBodyAsStream())));
            String[] row;
            // skip header
            csv.readNext();
            while ((row = csv.readNext()) != null) {
                try {
                    m.put(row[0], Long.parseLong(row[1]));
                } catch (Exception e) {
                    LOGGER.error("error getting species_guid,count (kosher): " + url, e);
                }
            }
        } catch (Exception e) {
            LOGGER.error("error getting species list from: " + url);
        }
        speciesListCountsUpdatedKosher = System.currentTimeMillis();
        speciesListCountsKosher = m;
    }
    return speciesListCountsKosher;
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 59 with CSVReader

use of au.com.bytecode.opencsv.CSVReader 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 60 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project dbeaver by dbeaver.

the class ConfigImportWizardPageCustomConnections method importCSV.

private void importCSV(ImportData importData, ImportDriverInfo driver, Reader reader) throws IOException {
    final CSVReader csvReader = new CSVReader(reader);
    final String[] header = csvReader.readNext();
    if (ArrayUtils.isEmpty(header)) {
        setErrorMessage("No connection found");
        return;
    }
    for (; ; ) {
        final String[] line = csvReader.readNext();
        if (ArrayUtils.isEmpty(line)) {
            break;
        }
        Map<String, String> conProps = new HashMap<>();
        for (int i = 0; i < line.length; i++) {
            if (i >= header.length) {
                break;
            }
            conProps.put(header[i], line[i]);
        }
        makeConnectionFromProps(importData, driver, conProps);
    }
}
Also used : CSVReader(au.com.bytecode.opencsv.CSVReader) HashMap(java.util.HashMap)

Aggregations

CSVReader (au.com.bytecode.opencsv.CSVReader)82 IOException (java.io.IOException)29 InputStreamReader (java.io.InputStreamReader)27 ArrayList (java.util.ArrayList)16 FileReader (java.io.FileReader)11 StringReader (java.io.StringReader)11 HashMap (java.util.HashMap)9 BufferedReader (java.io.BufferedReader)8 InputStream (java.io.InputStream)6 File (java.io.File)5 Reader (java.io.Reader)5 HttpClient (org.apache.commons.httpclient.HttpClient)5 GetMethod (org.apache.commons.httpclient.methods.GetMethod)5 Test (org.junit.Test)5 DBException (org.jkiss.dbeaver.DBException)4 Query (au.org.ala.spatial.util.Query)3 TransformationExample (eu.esdihumboldt.cst.test.TransformationExample)3 Date (java.util.Date)3 LinkedHashMap (java.util.LinkedHashMap)3 JSONArray (org.json.simple.JSONArray)3