Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/main/java/com/bizo/hive/serde/csv/CSVSerde.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;

import au.com.bytecode.opencsv.CSVParser;
import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.CSVWriter;

Expand All @@ -45,7 +46,7 @@ public final class CSVSerde implements SerDe {
private char separatorChar;
private char quoteChar;
private char escapeChar;

private CSVParser csvParser;

@Override
public void initialize(final Configuration conf, final Properties tbl) throws SerDeException {
Expand All @@ -71,6 +72,11 @@ public void initialize(final Configuration conf, final Properties tbl) throws Se
separatorChar = getProperty(tbl, "separatorChar", CSVWriter.DEFAULT_SEPARATOR);
quoteChar = getProperty(tbl, "quoteChar", CSVWriter.DEFAULT_QUOTE_CHARACTER);
escapeChar = getProperty(tbl, "escapeChar", CSVWriter.DEFAULT_ESCAPE_CHARACTER);
if (CSVWriter.DEFAULT_ESCAPE_CHARACTER == escapeChar) {
this.csvParser = new CSVParser(separatorChar, quoteChar);
} else {
this.csvParser = new CSVParser(separatorChar, quoteChar, escapeChar);
}
}

private final char getProperty(final Properties tbl, final String property, final char def) {
Expand Down Expand Up @@ -122,15 +128,13 @@ public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDe
@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();
final String[] strings = this.csvParser.parseLine(rowText.toString());

for (int i=0; i< numCols; i++) {
if (read != null && i < read.length) {
row.set(i, read[i]);
for (int i=0; i < numCols; i++) {
if (strings != null && i < strings.length) {
row.set(i, strings[i]);
} else {
row.set(i, null);
}
Expand All @@ -139,14 +143,6 @@ public Object deserialize(final Writable blob) throws SerDeException {
return row;
} catch (final Exception e) {
throw new SerDeException(e);
} finally {
if (csv != null) {
try {
csv.close();
} catch (final Exception e) {
// ignore
}
}
}
}

Expand Down