initial
commit
f30d2831dd
|
@ -0,0 +1,10 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
|
||||||
|
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
|
<classpathentry kind="lib" path="lib/default/hadoop-core.jar"/>
|
||||||
|
<classpathentry kind="lib" path="lib/default/hive-exec.jar"/>
|
||||||
|
<classpathentry kind="lib" path="lib/default/opencsv.jar"/>
|
||||||
|
<classpathentry kind="output" path="target/classes"/>
|
||||||
|
</classpath>
|
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>@PROJECT_NAME@</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?xml version="1.0"?>
|
||||||
|
|
||||||
|
<project name="csv-serde" default="dist">
|
||||||
|
|
||||||
|
<!-- you may override default properties for the build by using entries
|
||||||
|
in a build.properties file, or by just adding 'property' elements
|
||||||
|
here, before the following properties files are loaded -->
|
||||||
|
<property file="build.properties"/>
|
||||||
|
<property file="project.properties"/>
|
||||||
|
<property file="${common.build.dir}/build.properties"/>
|
||||||
|
<property file="${common.build.dir}/project.properties"/>
|
||||||
|
<property file="${user.home}/build.properties"/>
|
||||||
|
|
||||||
|
<import file="${common.build.dir}/common-targets.xml"/>
|
||||||
|
<import file="${common.build.dir}/repository-targets.xml"/>
|
||||||
|
<import file="${common.build.dir}/cobertura-targets.xml"/>
|
||||||
|
<import file="${common.build.dir}/findbugs-targets.xml"/>
|
||||||
|
<import file="${common.build.dir}/checkstyle-targets.xml"/>
|
||||||
|
<import file="${common.build.dir}/hadoop-targets.xml"/>
|
||||||
|
|
||||||
|
<target name="jar.pre" depends="hadoop.unjar.dependencies" />
|
||||||
|
|
||||||
|
<!-- Hudson target -->
|
||||||
|
<target name="hudson" depends="reallyclean, dist, findbugs, checkstyle, cobertura-xml-report, commit.repo"/>
|
||||||
|
|
||||||
|
</project>
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<ivy-module version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:noNamespaceSchemaLocation="http://ant.apache.org/ivy/schemas/ivy.xsd">
|
||||||
|
|
||||||
|
<info organisation="bizo.com" module="csv-serde" />
|
||||||
|
|
||||||
|
<configurations>
|
||||||
|
<conf name="default" description="core dependencies needed at compile-time and runtime"/>
|
||||||
|
<conf name="buildtime" visibility="private" description="additional dependencies needed only at build time"/>
|
||||||
|
<conf name="test" visibility="private" description="additional dependencies needed only for testing"/>
|
||||||
|
<conf name="sources" description="source artifacts"/>
|
||||||
|
<conf name="hadoop" visibility="private"/>
|
||||||
|
</configurations>
|
||||||
|
|
||||||
|
<publications>
|
||||||
|
<artifact conf="default"/>
|
||||||
|
<artifact name="${ivy.module}-src" type="source" ext="zip" conf="sources"/>
|
||||||
|
</publications>
|
||||||
|
|
||||||
|
<dependencies defaultconf="default->default;hadoop->default;sources->sources()">
|
||||||
|
<!-- default (compile, test, runtime) dependencies -->
|
||||||
|
<dependency org="opencsv" name="opencsv" rev="2.2" />
|
||||||
|
|
||||||
|
<dependency org="apache" name="hive" rev="0.5.0" conf="default->exec" />
|
||||||
|
|
||||||
|
<dependency org="apache" name="hadoop" rev="0.20.0" conf="buildtime->default">
|
||||||
|
<artifact name="hadoop-core" type="jar" />
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- build time only dependencies -->
|
||||||
|
<dependency org="findbugs" name="findbugs" rev="1.3.9" conf="buildtime->default"/>
|
||||||
|
<dependency org="checkstyle" name="checkstyle" rev="all-5.1" conf="buildtime->default"/>
|
||||||
|
<dependency org="svntask" name="svntask" rev="1.0.7" conf="buildtime->default"/>
|
||||||
|
<dependency org="cobertura" name="cobertura" rev="1.9.4.1" conf="buildtime->default"/>
|
||||||
|
|
||||||
|
<!-- test time only dependencies -->
|
||||||
|
<dependency org="junit" name="junit" rev="4.8.2" conf="test->default"/>
|
||||||
|
</dependencies>
|
||||||
|
</ivy-module>
|
|
@ -0,0 +1,7 @@
|
||||||
|
# to override these or other properties with local user settings,
|
||||||
|
# create a unversioned build.properties file
|
||||||
|
|
||||||
|
# The location of the common build system
|
||||||
|
common.build.dir=${basedir}/../common-build
|
||||||
|
|
||||||
|
project.base.version=1.1-dev
|
|
@ -0,0 +1,10 @@
|
||||||
|
*Hive CSV Support*
|
||||||
|
|
||||||
|
add jar path/to/csv-serde.jar;
|
||||||
|
|
||||||
|
create table my_table(a string, b string, ...)
|
||||||
|
row format serde 'com.bizo.hive.serde.csv.CSVSerde'
|
||||||
|
stored as textfile
|
||||||
|
;
|
||||||
|
|
||||||
|
see: ...
|
|
@ -0,0 +1,132 @@
|
||||||
|
package com.bizo.hive.serde.csv;
|
||||||
|
|
||||||
|
import java.io.CharArrayReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
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.hive.serde.Constants;
|
||||||
|
import org.apache.hadoop.hive.serde2.SerDe;
|
||||||
|
import org.apache.hadoop.hive.serde2.SerDeException;
|
||||||
|
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
|
||||||
|
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
|
||||||
|
import org.apache.hadoop.hive.serde2.objectinspector.StructField;
|
||||||
|
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
|
||||||
|
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
|
||||||
|
import org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector;
|
||||||
|
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
|
||||||
|
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
|
||||||
|
import org.apache.hadoop.io.Text;
|
||||||
|
import org.apache.hadoop.io.Writable;
|
||||||
|
|
||||||
|
import au.com.bytecode.opencsv.CSVReader;
|
||||||
|
import au.com.bytecode.opencsv.CSVWriter;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CSVSerde uses opencsv (http://opencsv.sourceforge.net/) to serialize/deserialize columns as CSV.
|
||||||
|
*
|
||||||
|
* @author Larry Ogrodnek <ogrodnek@gmail.com>
|
||||||
|
*/
|
||||||
|
public final class CSVSerde implements SerDe {
|
||||||
|
|
||||||
|
private ObjectInspector inspector;
|
||||||
|
String[] outputFields;
|
||||||
|
int numCols;
|
||||||
|
List<String> row;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
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<TypeInfo> columnTypes = TypeInfoUtils.getTypeInfosFromTypeString(tbl.getProperty(Constants.LIST_COLUMN_TYPES));
|
||||||
|
|
||||||
|
numCols = columnNames.size();
|
||||||
|
|
||||||
|
final List<ObjectInspector> columnOIs = new ArrayList<ObjectInspector>(numCols);
|
||||||
|
|
||||||
|
for (int i=0; i< numCols; i++) {
|
||||||
|
columnOIs.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.inspector = ObjectInspectorFactory.getStandardStructObjectInspector(columnNames, columnOIs);
|
||||||
|
this.outputFields = new String[numCols];
|
||||||
|
row = new ArrayList<String>(numCols);
|
||||||
|
|
||||||
|
for (int i=0; i< numCols; i++) {
|
||||||
|
row.add(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDeException {
|
||||||
|
final StructObjectInspector outputRowOI = (StructObjectInspector) objInspector;
|
||||||
|
final List<? extends StructField> outputFieldRefs = outputRowOI.getAllStructFieldRefs();
|
||||||
|
|
||||||
|
if (outputFieldRefs.size() != numCols) {
|
||||||
|
throw new SerDeException("Cannot serialize the object because there are "
|
||||||
|
+ outputFieldRefs.size() + " fields but the table has " + numCols + " columns.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get all data out.
|
||||||
|
for (int c = 0; c < numCols; c++) {
|
||||||
|
final Object field = outputRowOI.getStructFieldData(obj, outputFieldRefs.get(c));
|
||||||
|
final ObjectInspector fieldOI = outputFieldRefs.get(c).getFieldObjectInspector();
|
||||||
|
|
||||||
|
// The data must be of type String
|
||||||
|
final StringObjectInspector fieldStringOI = (StringObjectInspector) fieldOI;
|
||||||
|
|
||||||
|
// Convert the field to Java class String, because objects of String type
|
||||||
|
// can be stored in String, Text, or some other classes.
|
||||||
|
outputFields[c] = fieldStringOI.getPrimitiveJavaObject(field);
|
||||||
|
}
|
||||||
|
|
||||||
|
final StringWriter writer = new StringWriter();
|
||||||
|
final CSVWriter csv = new CSVWriter(writer, CSVWriter.DEFAULT_SEPARATOR, CSVWriter.DEFAULT_QUOTE_CHARACTER, "");
|
||||||
|
|
||||||
|
try {
|
||||||
|
csv.writeNext(outputFields);
|
||||||
|
csv.close();
|
||||||
|
|
||||||
|
return new Text(writer.toString());
|
||||||
|
} catch (final IOException ioe) {
|
||||||
|
throw new SerDeException(ioe);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object deserialize(final Writable blob) throws SerDeException {
|
||||||
|
Text rowText = (Text) blob;
|
||||||
|
|
||||||
|
try {
|
||||||
|
final CSVReader csv = new CSVReader(new CharArrayReader(rowText.toString().toCharArray()));
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ObjectInspector getObjectInspector() throws SerDeException {
|
||||||
|
return inspector;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<? extends Writable> getSerializedClass() {
|
||||||
|
return Text.class;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue