Skip to content

Instantly share code, notes, and snippets.

@er1c
Created January 15, 2016 17:09
Show Gist options
  • Save er1c/0d0a5d4851aab1e2f65f to your computer and use it in GitHub Desktop.
Save er1c/0d0a5d4851aab1e2f65f to your computer and use it in GitHub Desktop.
/*
* Decompiled with CFR 0_110.
*
* Could not load the following classes:
* org.apache.commons.logging.Log
* org.apache.commons.logging.LogFactory
* org.apache.hadoop.conf.Configuration
* org.apache.hadoop.hive.serde2.SerDe
* org.apache.hadoop.hive.serde2.SerDeException
* org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector
* org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory
* org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaStringObjectInspector
* org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory
* org.apache.hadoop.io.Text
* org.apache.hadoop.io.Writable
*/
package com.amazon.elasticmapreduce;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
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.primitive.JavaStringObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
public class JsonSerde
implements SerDe {
public static final Log LOG = LogFactory.getLog((String)JsonSerde.class.getName());
List<String> paths;
List<String> columnNames;
List<String> row;
private ObjectInspector rowObjectInspector;
private List<ObjectInspector> columnObjectInspectors;
List<String> getPropertyValueAsList(Properties properties, String string) throws SerDeException {
if (properties.getProperty(string) != null) {
return Arrays.asList(properties.getProperty(string).split(",\\s*"));
}
throw new SerDeException("Expected a property called " + string + " giving a list of paths to extract");
}
public void initialize(Configuration configuration, Properties properties) throws SerDeException {
this.paths = this.getPropertyValueAsList(properties, "paths");
this.columnNames = this.getPropertyValueAsList(properties, "columns");
if (this.paths.size() != this.columnNames.size()) {
throw new SerDeException("Expected a one-one correspondance between paths " + this.paths.size() + " and columns " + this.columnNames.size());
}
this.columnObjectInspectors = new ArrayList<ObjectInspector>();
for (String string : this.columnNames) {
this.columnObjectInspectors.add((ObjectInspector)PrimitiveObjectInspectorFactory.javaStringObjectInspector);
}
this.rowObjectInspector = ObjectInspectorFactory.getStandardStructObjectInspector(this.columnNames, this.columnObjectInspectors);
}
public ObjectInspector getObjectInspector() throws SerDeException {
return this.rowObjectInspector;
}
public Class<? extends Writable> getSerializedClass() {
return Text.class;
}
public Object deserialize(Writable writable) throws SerDeException {
LOG.error((Object)"Deserialize");
Text text = (Text)writable;
JsonElement jsonElement = new JsonParser().parse(text.toString());
this.row = new ArrayList<String>();
for (String string : this.paths) {
this.row.add(this.resolve(jsonElement, string));
}
return this.row;
}
private String resolve(JsonElement jsonElement, String string) {
if (string.equals(Character.valueOf('$'))) {
return jsonElement.toString();
}
String[] arrstring = string.split("\\.");
for (int i = 0; i < arrstring.length; ++i) {
if (jsonElement == null) {
return null;
}
if (!jsonElement.isJsonObject()) {
return null;
}
jsonElement = jsonElement.getAsJsonObject().get(arrstring[i]);
}
if (jsonElement != null) {
if (jsonElement.isJsonPrimitive()) {
return jsonElement.getAsString();
}
return jsonElement.toString();
}
return null;
}
public Writable serialize(Object object, ObjectInspector objectInspector) throws SerDeException {
Text text = new Text();
return text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment