Added line end delimiter and null string support

master
Abhinav Sarkar 2014-06-07 17:07:38 +05:30
parent f315c1ae4b
commit 8daf918fc8
1 changed files with 50 additions and 49 deletions

View File

@ -1,15 +1,7 @@
package com.bizo.hive.serde.csv; package com.bizo.hive.serde.csv;
import java.io.CharArrayReader; import au.com.bytecode.opencsv.CSVReader;
import java.io.IOException; import au.com.bytecode.opencsv.CSVWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.serde.Constants; import org.apache.hadoop.hive.serde.Constants;
import org.apache.hadoop.hive.serde2.SerDe; import org.apache.hadoop.hive.serde2.SerDe;
@ -26,68 +18,74 @@ import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
import org.apache.hadoop.io.Text; import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable; import org.apache.hadoop.io.Writable;
import au.com.bytecode.opencsv.CSVReader; import java.io.*;
import au.com.bytecode.opencsv.CSVWriter; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
/** /**
* CSVSerde uses opencsv (http://opencsv.sourceforge.net/) to serialize/deserialize columns as CSV. * CSVSerde uses opencsv (http://opencsv.sourceforge.net/) to serialize/deserialize columns as CSV.
* *
* @author Larry Ogrodnek <ogrodnek@gmail.com> * @author Larry Ogrodnek <ogrodnek@gmail.com>
*/ */
public final class CSVSerde implements SerDe { public final class CSVSerde implements SerDe {
private ObjectInspector inspector; private ObjectInspector inspector;
private String[] outputFields; private String[] outputFields;
private int numCols; private int numCols;
private List<String> row; private List<String> row;
private char separatorChar; private char separatorChar;
private char quoteChar; private char quoteChar;
private char escapeChar; private char escapeChar;
private String lineEnd;
private String nullDefinedAs;
@Override @Override
public void initialize(final Configuration conf, final Properties tbl) throws SerDeException { public void initialize(final Configuration conf, final Properties tbl) throws SerDeException {
final List<String> columnNames = Arrays.asList(tbl.getProperty(Constants.LIST_COLUMNS).split(",")); final List<String> columnNames = Arrays.asList(tbl.getProperty(Constants.LIST_COLUMNS).split(","));
final List<TypeInfo> columnTypes = TypeInfoUtils.getTypeInfosFromTypeString(tbl.getProperty(Constants.LIST_COLUMN_TYPES)); final List<TypeInfo> columnTypes = TypeInfoUtils.getTypeInfosFromTypeString(tbl.getProperty(Constants.LIST_COLUMN_TYPES));
numCols = columnNames.size(); numCols = columnNames.size();
final List<ObjectInspector> columnOIs = new ArrayList<ObjectInspector>(numCols); final List<ObjectInspector> columnOIs = new ArrayList<ObjectInspector>(numCols);
for (int i=0; i< numCols; i++) { for (int i=0; i< numCols; i++) {
columnOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector); columnOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
} }
this.inspector = ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, columnOIs); this.inspector = ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, columnOIs);
this.outputFields = new String[numCols]; this.outputFields = new String[numCols];
row = new ArrayList<String>(numCols); row = new ArrayList<String>(numCols);
for (int i=0; i< numCols; i++) { for (int i=0; i< numCols; i++) {
row.add(null); row.add(null);
} }
separatorChar = getProperty(tbl, "separatorChar", CSVWriter.DEFAULT_SEPARATOR); separatorChar = getProperty(tbl, "separatorChar", CSVWriter.DEFAULT_SEPARATOR);
quoteChar = getProperty(tbl, "quoteChar", CSVWriter.DEFAULT_QUOTE_CHARACTER); quoteChar = getProperty(tbl, "quoteChar", CSVWriter.DEFAULT_QUOTE_CHARACTER);
escapeChar = getProperty(tbl, "escapeChar", CSVWriter.DEFAULT_ESCAPE_CHARACTER); escapeChar = getProperty(tbl, "escapeChar", CSVWriter.DEFAULT_ESCAPE_CHARACTER);
lineEnd = tbl.getProperty("lineEnd", CSVWriter.DEFAULT_LINE_END);
nullDefinedAs = tbl.getProperty("nullDefinedAs", "\\N");
} }
private final char getProperty(final Properties tbl, final String property, final char def) { private final char getProperty(final Properties tbl, final String property, final char def) {
final String val = tbl.getProperty(property); final String val = tbl.getProperty(property);
if (val != null) { if (val != null) {
return val.charAt(0); return val.charAt(0);
} }
return def; return def;
} }
@Override @Override
public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDeException { public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDeException {
final StructObjectInspector outputRowOI = (StructObjectInspector) objInspector; final StructObjectInspector outputRowOI = (StructObjectInspector) objInspector;
final List<? extends StructField> outputFieldRefs = outputRowOI.getAllStructFieldRefs(); final List<? extends StructField> outputFieldRefs = outputRowOI.getAllStructFieldRefs();
if (outputFieldRefs.size() != numCols) { if (outputFieldRefs.size() != numCols) {
throw new SerDeException("Cannot serialize the object because there are " throw new SerDeException("Cannot serialize the object because there are "
+ outputFieldRefs.size() + " fields but the table has " + numCols + " columns."); + outputFieldRefs.size() + " fields but the table has " + numCols + " columns.");
@ -97,45 +95,48 @@ public final class CSVSerde implements SerDe {
for (int c = 0; c < numCols; c++) { for (int c = 0; c < numCols; c++) {
final Object field = outputRowOI.getStructFieldData(obj, outputFieldRefs.get(c)); final Object field = outputRowOI.getStructFieldData(obj, outputFieldRefs.get(c));
final ObjectInspector fieldOI = outputFieldRefs.get(c).getFieldObjectInspector(); final ObjectInspector fieldOI = outputFieldRefs.get(c).getFieldObjectInspector();
// The data must be of type String // The data must be of type String
final StringObjectInspector fieldStringOI = (StringObjectInspector) fieldOI; final StringObjectInspector fieldStringOI = (StringObjectInspector) fieldOI;
// Convert the field to Java class String, because objects of String type // Convert the field to Java class String, because objects of String type
// can be stored in String, Text, or some other classes. // can be stored in String, Text, or some other classes.
outputFields[c] = fieldStringOI.getPrimitiveJavaObject(field); outputFields[c] = fieldStringOI.getPrimitiveJavaObject(field);
if (outputFields[c] == null) {
outputFields[c] = nullDefinedAs;
}
} }
final StringWriter writer = new StringWriter(); final StringWriter writer = new StringWriter();
final CSVWriter csv = newWriter(writer, separatorChar, quoteChar, escapeChar); final CSVWriter csv = newWriter(writer, separatorChar, quoteChar, escapeChar, lineEnd);
try { try {
csv.writeNext(outputFields); csv.writeNext(outputFields);
csv.close(); csv.close();
return new Text(writer.toString()); return new Text(writer.toString());
} catch (final IOException ioe) { } catch (final IOException ioe) {
throw new SerDeException(ioe); throw new SerDeException(ioe);
} }
} }
@Override @Override
public Object deserialize(final Writable blob) throws SerDeException { public Object deserialize(final Writable blob) throws SerDeException {
Text rowText = (Text) blob; Text rowText = (Text) blob;
CSVReader csv = null; CSVReader csv = null;
try { try {
csv = newReader(new CharArrayReader(rowText.toString().toCharArray()), separatorChar, quoteChar, escapeChar); csv = newReader(new CharArrayReader(rowText.toString().toCharArray()), separatorChar, quoteChar, escapeChar);
final String[] read = csv.readNext(); final String[] read = csv.readNext();
for (int i=0; i< numCols; i++) { for (int i=0; i< numCols; i++) {
if (read != null && i < read.length) { if (read != null && i < read.length) {
row.set(i, read[i]); row.set(i, read[i] == nullDefinedAs ? null : read[i]);
} else { } else {
row.set(i, null); row.set(i, null);
} }
} }
return row; return row;
} catch (final Exception e) { } catch (final Exception e) {
throw new SerDeException(e); throw new SerDeException(e);
@ -149,22 +150,22 @@ public final class CSVSerde implements SerDe {
} }
} }
} }
private CSVReader newReader(final Reader reader, char separator, char quote, char escape) { private static CSVReader newReader(final Reader reader, char separator, char quote, char escape) {
// CSVReader will throw an exception if any of separator, quote, or escape is the same, but // CSVReader will throw an exception if any of separator, quote, or escape is the same, but
// the CSV format specifies that the escape character and quote char are the same... very weird // the CSV format specifies that the escape character and quote char are the same... very weird
if (CSVWriter.DEFAULT_ESCAPE_CHARACTER == escape) { if (CSVWriter.DEFAULT_ESCAPE_CHARACTER == escape) {
return new CSVReader(reader, separator, quote); return new CSVReader(reader, separator, quote);
} else { } else {
return new CSVReader(reader, separator, quote, escape); return new CSVReader(reader, separator, quote, escape);
} }
} }
private CSVWriter newWriter(final Writer writer, char separator, char quote, char escape) { private static CSVWriter newWriter(final Writer writer, char separator, char quote, char escape, String lineEnd) {
if (CSVWriter.DEFAULT_ESCAPE_CHARACTER == escape) { if (CSVWriter.DEFAULT_ESCAPE_CHARACTER == escape) {
return new CSVWriter(writer, separator, quote, ""); return new CSVWriter(writer, separator, quote, lineEnd);
} else { } else {
return new CSVWriter(writer, separator, quote, escape, ""); return new CSVWriter(writer, separator, quote, escape, lineEnd);
} }
} }
@ -177,7 +178,7 @@ public final class CSVSerde implements SerDe {
public Class<? extends Writable> getSerializedClass() { public Class<? extends Writable> getSerializedClass() {
return Text.class; return Text.class;
} }
public SerDeStats getSerDeStats() { public SerDeStats getSerDeStats() {
return null; return null;
} }