Skip to content

Commit

Permalink
wMerge branch 'guyrt-timestamp-support' into feature/25-timestamp-sup…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
rcongiu committed May 6, 2013
2 parents d1cc04b + 3968202 commit 8407fe6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 12 deletions.
29 changes: 19 additions & 10 deletions src/main/java/org/openx/data/jsonserde/JsonSerDe.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package org.openx.data.jsonserde;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -53,6 +54,8 @@
import org.openx.data.jsonserde.objectinspector.JsonObjectInspectorFactory;
import org.openx.data.jsonserde.objectinspector.JsonStructOIOptions;

import javax.print.attribute.standard.DateTimeAtCompleted;

/**
* Properties:
* ignore.malformed.json = true/false : malformed json will be ignored
Expand Down Expand Up @@ -111,8 +114,8 @@ public void initialize(Configuration conf, Properties tbl) throws SerDeException
}
assert (columnNames.size() == columnTypes.size());

stats = new SerDeStats();
stats = new SerDeStats();

// Create row related objects
rowTypeInfo = (StructTypeInfo) TypeInfoFactory
.getStructTypeInfo(columnNames, columnTypes);
Expand Down Expand Up @@ -154,6 +157,7 @@ public Object deserialize(Writable w) throws SerDeException {

// Try parsing row into JSON object
JSONObject jObj = null;


try {
jObj = new JSONObject(rowText.toString()) {
Expand All @@ -166,8 +170,16 @@ public Object deserialize(Writable w) throws SerDeException {
* java.lang.Object)
*/
@Override
public JSONObject put(String key, Object value)
throws JSONException {
public JSONObject put(String key, Object value) throws JSONException {

try {
if (columnNames.contains(key) && rowTypeInfo.getStructFieldTypeInfo(key).getTypeName().equalsIgnoreCase("timestamp")) {
value = Timestamp.valueOf((String)value);
}
} catch (IllegalArgumentException e) {
throw new JSONException("Timestamp " + value + "improperly formatted.");
}

return super.put(key.toLowerCase(), value);
}
};
Expand All @@ -192,7 +204,7 @@ public ObjectInspector getObjectInspector() throws SerDeException {

/**
* We serialize to Text
* @return
* @return
*
* @see org.apache.hadoop.io.Text
*/
Expand Down Expand Up @@ -225,7 +237,7 @@ public Writable serialize(Object obj, ObjectInspector objInspector) throws SerDe

Text t = new Text(serializer.toString());

serializedDataSize = t.getBytes().length;
serializedDataSize = t.getBytes().length;
return t;
}

Expand All @@ -243,9 +255,6 @@ private String getSerializedFieldName( List<String> columnNames, int pos, Struct
* Serializing means getting every field, and setting the appropriate
* JSONObject field. Actual serialization is done at the end when
* the whole JSON object is built
* @param serializer
* @param obj
* @param structObjectInspector
*/
private JSONObject serializeStruct( Object obj,
StructObjectInspector soi, List<String> columnNames) {
Expand Down Expand Up @@ -405,7 +414,7 @@ public void onMalformedJson(String msg) throws SerDeException {

@Override
public SerDeStats getSerDeStats() {
if(lastOperationSerialize) {
if(lastOperationSerialize) {
stats.setRawDataSize(serializedDataSize);
} else {
stats.setRawDataSize(deserializedDataSize);
Expand Down
23 changes: 21 additions & 2 deletions src/test/java/org/openx/data/jsonserde/JsonSerDeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,15 @@ static public void initialize() throws Exception {
public void testDeserialize() throws Exception {
System.out.println("deserialize");
Writable w = new Text("{\"one\":true,\"three\":[\"red\",\"yellow\",\"orange\"],\"two\":19.5,\"four\":\"poop\"}");
Object expResult = null;

JSONObject result = (JSONObject) instance.deserialize(w);
assertEquals(result.get("four"), "poop");
assertEquals(result.get("four"),"poop");

assertTrue(result.get("three") instanceof JSONArray);

assertTrue( ((JSONArray)result.get("three")).get(0) instanceof String );
assertEquals( ((JSONArray)result.get("three")).get(0),"red");
}

assertTrue(result.get("three") instanceof JSONArray);
Expand All @@ -110,6 +116,19 @@ public void testDeserialize2() throws Exception {
assertEquals(((JSONArray) result.get("three")).get(0), "red");
}

@Test
public void testDeserializePartialFieldSet() throws Exception {
Writable w = new Text("{\"missing\":\"whocares\",\"one\":true,\"three\":[\"red\",\"yellow\",[\"blue\",\"azure\",\"cobalt\",\"teal\"],\"orange\"],\"two\":19.5,\"four\":\"poop\"}");
Object expResult = null;
JSONObject result = (JSONObject) instance.deserialize(w);
assertEquals(result.get("four"),"poop");

assertTrue( result.get("three") instanceof JSONArray);

assertTrue( ((JSONArray)result.get("three")).get(0) instanceof String );
assertEquals( ((JSONArray)result.get("three")).get(0),"red");
}

/**
* Test of getSerializedClass method, of class JsonSerDe.
*/
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/org/openx/data/jsonserde/JsonSerDeTimeStampTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.openx.data.jsonserde;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.serde.Constants;
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.sql.Timestamp;
import java.util.Properties;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* User: guyrt
*/
public class JsonSerDeTimeStampTest {

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();
tbl.setProperty(Constants.LIST_COLUMNS, "one,two,three,four,five");
tbl.setProperty(Constants.LIST_COLUMN_TYPES, "boolean,float,array<string>,string,timestamp");

instance.initialize(conf, tbl);
}

@Test
public void testTimestampDeSerialize() throws Exception {
// Test that timestamp object can be deserialized
Writable w = new Text("{\"one\":true,\"five\":\"2013-03-27 23:18:40\"}");

JSONObject result = (JSONObject) instance.deserialize(w);
assertEquals(result.get("five"), Timestamp.valueOf("2013-03-27 23:18:40.0"));
}

@Test
public void testTimestampDeSerializeWithNanoseconds() throws Exception {
// Test that timestamp object can be deserialized
Writable w = new Text("{\"one\":true,\"five\":\"2013-03-27 23:18:40.123456\"}");

JSONObject result = (JSONObject) instance.deserialize(w);
assertEquals(result.get("five"), Timestamp.valueOf("2013-03-27 23:18:40.123456"));
}


}

0 comments on commit 8407fe6

Please sign in to comment.