Search in sources :

Example 66 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project hale by halestudio.

the class LookupTablePage method readHeader.

// read line from a file specified by provider of corresponding wizard
private String[] readHeader() {
    LookupTableImport provider = getWizard().getProvider();
    List<String> items = new ArrayList<String>();
    try {
        if (provider instanceof CSVLookupReader) {
            CSVReader reader = CSVUtil.readFirst(getWizard().getProvider());
            return reader.readNext();
        } else {
            Workbook workbook;
            // write xls file
            String file = provider.getSource().getLocation().getPath();
            String fileExtension = file.substring(file.lastIndexOf("."), file.length());
            if (fileExtension.equals(".xls")) {
                workbook = new HSSFWorkbook(provider.getSource().getInput());
            } else // write xlsx file
            if (fileExtension.equals(".xlsx")) {
                workbook = new XSSFWorkbook(provider.getSource().getInput());
            } else
                return new String[0];
            Sheet sheet = workbook.getSheetAt(0);
            Row currentRow = sheet.getRow(0);
            for (int cell = 0; cell < currentRow.getPhysicalNumberOfCells(); cell++) {
                items.add(currentRow.getCell(cell).getStringCellValue());
            }
            return items.toArray(new String[0]);
        }
    } catch (IOException e) {
        return new String[0];
    }
}
Also used : DefaultCSVLookupReader(eu.esdihumboldt.hale.io.csv.reader.DefaultCSVLookupReader) CSVLookupReader(eu.esdihumboldt.hale.io.csv.reader.internal.CSVLookupReader) CSVReader(au.com.bytecode.opencsv.CSVReader) ArrayList(java.util.ArrayList) StyledString(org.eclipse.jface.viewers.StyledString) IOException(java.io.IOException) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) HSSFWorkbook(org.apache.poi.hssf.usermodel.HSSFWorkbook) LookupTableImport(eu.esdihumboldt.hale.common.lookup.LookupTableImport) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) Sheet(org.apache.poi.ss.usermodel.Sheet)

Example 67 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project hale by halestudio.

the class DefaultCSVLookupReader method read.

/**
 * Reads a csv lookup table file. The selected columns specified by
 * parameters keyColumn and valueColumn are mapped together.
 *
 * @param input the inputstream of the csv file
 * @param charset specific charset of the csv file
 * @param separator used separator char in csv file
 * @param quote used quote char in csv file
 * @param escape used escape char in csv file
 * @param skipFirst true, if first line should be skipped
 * @param keyColumn source column of the lookup table
 * @param valueColumn target column of the lookup table
 * @return lookup table as map
 * @throws IOException if inputstream is not readable
 */
public Map<Value, Value> read(InputStream input, Charset charset, char separator, char quote, char escape, boolean skipFirst, int keyColumn, int valueColumn) throws IOException {
    Reader streamReader = new BufferedReader(new InputStreamReader(input, charset));
    CSVReader reader = new CSVReader(streamReader, separator, quote, escape);
    String[] nextLine;
    Map<Value, Value> values = new LinkedHashMap<Value, Value>();
    if (skipFirst)
        reader.readNext();
    while ((nextLine = reader.readNext()) != null) {
        if (nextLine.length >= 2)
            values.put(Value.of(nextLine[keyColumn]), Value.of(nextLine[valueColumn]));
    }
    reader.close();
    return values;
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) BufferedReader(java.io.BufferedReader) Value(eu.esdihumboldt.hale.common.core.io.Value) CSVReader(au.com.bytecode.opencsv.CSVReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) LinkedHashMap(java.util.LinkedHashMap)

Example 68 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project hale by halestudio.

the class CSVUtil method readFirst.

/**
 * This method reads/retrieves just one line, either the first after the
 * header or the first line after skipping "skipLines".
 *
 * @param provider to get the parameters from
 * @param skipLines how many lines have to be skipped
 * @return a reader containing the first line after (no. of lines -
 *         skipLines) of the CSV file
 * @throws IOException
 */
public static CSVReader readFirst(ImportProvider provider, int skipLines) throws IOException {
    Reader streamReader = new BufferedReader(new InputStreamReader(provider.getSource().getInput(), provider.getCharset()));
    CSVReader reader = new CSVReader(streamReader, getSep(provider), getQuote(provider), getEscape(provider), skipLines, CSVParser.DEFAULT_STRICT_QUOTES);
    return reader;
}
Also used : InputStreamReader(java.io.InputStreamReader) CSVReader(au.com.bytecode.opencsv.CSVReader) BufferedReader(java.io.BufferedReader) CSVReader(au.com.bytecode.opencsv.CSVReader) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader)

Example 69 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project hive by apache.

the class OpenCSVSerde method deserialize.

@Override
public Object deserialize(final Writable blob) throws SerDeException {
    Text rowText = (Text) blob;
    CSVReader csv = null;
    try {
        csv = newReader(new CharArrayReader(rowText.toString().toCharArray()), separatorChar, quoteChar, escapeChar);
        final String[] read = csv.readNext();
        for (int i = 0; i < numCols; i++) {
            if (read != null && i < read.length) {
                row.set(i, read[i]);
            } else {
                row.set(i, null);
            }
        }
        return row;
    } catch (final Exception e) {
        throw new SerDeException(e);
    } finally {
        if (csv != null) {
            try {
                csv.close();
            } catch (final Exception e) {
                log.error("fail to close csv writer", e);
            }
        }
    }
}
Also used : CharArrayReader(java.io.CharArrayReader) CSVReader(au.com.bytecode.opencsv.CSVReader) Text(org.apache.hadoop.io.Text) IOException(java.io.IOException)

Example 70 with CSVReader

use of au.com.bytecode.opencsv.CSVReader in project aws-doc-sdk-examples by awsdocs.

the class MovieLensDatasetProvider method uploadMovieLensDatasetToS3.

public static void uploadMovieLensDatasetToS3(S3Client s3Client, String bucket, DatasetType type, String path) throws IOException {
    CSVReader reader = new CSVReader(new BufferedReader(new InputStreamReader(new FileInputStream("./movie-lens-ds/ml-100k/u.data"))), '\t');
    StringWriter sw = new StringWriter();
    CSVWriter pw = new CSVWriter(sw);
    pw.writeNext(new String[] { "USER_ID", "ITEM_ID", "TIMESTAMP" });
    String[] row = null;
    while ((row = reader.readNext()) != null) {
        int rating = Integer.parseInt(row[2]);
        if (rating > 3) {
            pw.writeNext(new String[] { row[0], row[1], row[3] });
        }
    }
    reader.close();
    pw.close();
    String data = sw.toString();
    try {
        PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucket).key(path).build();
        s3Client.putObject(objectRequest, RequestBody.fromBytes(data.getBytes()));
    } catch (S3Exception e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) StringWriter(java.io.StringWriter) CSVReader(au.com.bytecode.opencsv.CSVReader) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) BufferedReader(java.io.BufferedReader) CSVWriter(au.com.bytecode.opencsv.CSVWriter) FileInputStream(java.io.FileInputStream) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest)

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