-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added UNIONTYPE support ( fixes #53)
- Loading branch information
Showing
11 changed files
with
265 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...erde/src/main/java/org/openx/data/jsonserde/objectinspector/JsonUnionObjectInspector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.openx.data.jsonserde.objectinspector; | ||
|
||
import org.apache.hadoop.hive.serde2.SerDeException; | ||
import org.apache.hadoop.hive.serde2.objectinspector.*; | ||
import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo; | ||
import org.openx.data.jsonserde.json.JSONArray; | ||
import org.openx.data.jsonserde.json.JSONObject; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by rcongiu on 8/29/15. | ||
*/ | ||
public class JsonUnionObjectInspector implements UnionObjectInspector { | ||
JsonStructOIOptions options; | ||
private List<ObjectInspector> ois; | ||
|
||
|
||
public JsonUnionObjectInspector(List<ObjectInspector> ois,JsonStructOIOptions opts) { | ||
this.ois = ois; | ||
options = opts; | ||
} | ||
|
||
|
||
@Override | ||
public List<ObjectInspector> getObjectInspectors() { | ||
return ois; | ||
} | ||
|
||
|
||
/* | ||
* This method looks at the object and finds which object inspector should be used. | ||
*/ | ||
@Override | ||
public byte getTag(Object o) { | ||
if(o==null) return 0; | ||
for(byte i =0; i< ois.size(); i ++) { | ||
ObjectInspector oi = ois.get(i); | ||
|
||
switch(oi.getCategory()) { | ||
case LIST: if(o instanceof JSONArray) return i; else break; | ||
case STRUCT: if(o instanceof JSONObject) return i; else break; | ||
case MAP: if(o instanceof JSONObject) return i; else break; | ||
case UNION: return i; | ||
|
||
case PRIMITIVE: { | ||
PrimitiveObjectInspector poi = (PrimitiveObjectInspector) oi; | ||
try { | ||
// try to parse it, return if able to | ||
poi.getPrimitiveJavaObject(o); | ||
return i; | ||
} catch (Exception ex) { continue;} | ||
} | ||
default :throw new Error("Object Inspector " + oi.toString() + " Not supported for object " + o.toString()); | ||
} | ||
} | ||
throw new Error("No suitable Object Inspector found for object " + o.toString() + " of class " + o.getClass().getCanonicalName()); | ||
} | ||
|
||
@Override | ||
public Object getField(Object o) { | ||
return o; | ||
} | ||
|
||
@Override | ||
public String getTypeName() { | ||
return ObjectInspectorUtils.getStandardUnionTypeName(this); | ||
|
||
} | ||
|
||
@Override | ||
public Category getCategory() { | ||
return Category.UNION; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
json-serde/src/test/java/org/openx/data/jsonserde/JsonUnionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.openx.data.jsonserde; | ||
|
||
import org.apache.hadoop.conf.Configuration; | ||
import org.apache.hadoop.hive.serde.Constants; | ||
import org.apache.hadoop.hive.serde2.objectinspector.*; | ||
import org.apache.hadoop.io.Text; | ||
import org.apache.hadoop.io.Writable; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openx.data.jsonserde.json.JSONArray; | ||
import org.openx.data.jsonserde.json.JSONObject; | ||
|
||
import java.util.Properties; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
/** | ||
* Created by rcongiu on 8/30/15. | ||
*/ | ||
public class JsonUnionTest { | ||
static JsonSerDe instance; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
initialize(); | ||
} | ||
|
||
static public void initialize() throws Exception { | ||
instance = new JsonSerDe(); | ||
Configuration conf = null; | ||
Properties tbl = new Properties(); | ||
// from google video API | ||
tbl.setProperty(Constants.LIST_COLUMNS, "country,stuff"); | ||
tbl.setProperty(Constants.LIST_COLUMN_TYPES, "string,uniontype<int,double,array<string>,struct<a:int,b:string>,string>".toLowerCase()); | ||
|
||
instance.initialize(conf, tbl); | ||
} | ||
|
||
@Test | ||
public void testDeSerialize() throws Exception { | ||
// Test that timestamp object can be deserialized | ||
|
||
|
||
StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector(); | ||
|
||
StructField sfr = soi.getStructFieldRef("stuff"); | ||
|
||
assertEquals(sfr.getFieldObjectInspector().getCategory(), ObjectInspector.Category.UNION); | ||
|
||
UnionObjectInspector uoi = (UnionObjectInspector) sfr.getFieldObjectInspector(); | ||
|
||
// first, string | ||
Writable w = new Text("{\"country\":\"Switzerland\",\"stuff\":\"Italian\"}"); | ||
JSONObject result = (JSONObject) instance.deserialize(w); | ||
Object val = soi.getStructFieldData(result, sfr) ; | ||
assertEquals("Italian", uoi.getField(val)); | ||
|
||
uoi.getTypeName(); | ||
|
||
// now, int | ||
w = new Text("{\"country\":\"Switzerland\",\"stuff\":2}"); | ||
result = (JSONObject) instance.deserialize(w); | ||
val = soi.getStructFieldData(result, sfr) ; | ||
assertEquals("2", val); | ||
assertEquals(0, uoi.getTag(val)); | ||
|
||
// now, struct | ||
w = new Text("{\"country\":\"Switzerland\",\"stuff\": { \"a\": \"OK\" } }"); | ||
result = (JSONObject) instance.deserialize(w); | ||
val = soi.getStructFieldData(result, sfr) ; | ||
assertTrue(val instanceof JSONObject); | ||
assertEquals(3, uoi.getTag(val)); | ||
|
||
// now, array | ||
w = new Text("{\"country\":\"Switzerland\",\"stuff\": [ 1, 2 ] }"); | ||
result = (JSONObject) instance.deserialize(w); | ||
val = soi.getStructFieldData(result, sfr) ; | ||
assertTrue(val instanceof JSONArray); | ||
assertEquals(2, uoi.getTag(val)); | ||
} | ||
} |
Oops, something went wrong.