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];
}
}
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;
}
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;
}
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);
}
}
}
}
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);
}
}
Aggregations