Skip to content

Commit

Permalink
fixed #82, can handle maps with empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Congiu committed Aug 25, 2014
1 parent d367e3a commit a387336
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StandardMapObjectInspector;
import org.apache.hadoop.io.Text;
import org.openx.data.jsonserde.json.JSONException;
import org.openx.data.jsonserde.json.JSONObject;

Expand All @@ -32,7 +33,7 @@ public JsonMapObjectInspector(ObjectInspector mapKeyObjectInspector,

@Override
public Map<?, ?> getMap(Object data) {
if (data == null) {
if (checkObject(data) == null) {
return null;
}

Expand All @@ -41,9 +42,22 @@ public JsonMapObjectInspector(ObjectInspector mapKeyObjectInspector,
return new JSONObjectMapAdapter(jObj);
}

protected Object checkObject(Object data) {
// just check for null first thing
if(data == null) return data;

if(data instanceof String ||
data instanceof Text) {
String str = (data instanceof String ? (String)data : ((Text)data).toString() );
if(str.trim().isEmpty())
return null;
}
return data;
}

@Override
public int getMapSize(Object data) {
if (data == null) {
if (checkObject(data) == null) {
return -1;
}
JSONObject jObj = (JSONObject) data;
Expand All @@ -52,7 +66,7 @@ public int getMapSize(Object data) {

@Override
public Object getMapValueElement(Object data, Object key) {
if (data == null) {
if (checkObject(data) == null) {
return -1;
}

Expand Down
75 changes: 75 additions & 0 deletions json-serde/src/test/java/org/openx/data/jsonserde/JsonMapTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*======================================================================*
* Copyright (c) 2011, OpenX Technologies, Inc. All rights reserved. *
* *
* Licensed under the New BSD License (the "License"); you may not use *
* this file except in compliance with the License. Unless required *
* by applicable law or agreed to in writing, software distributed *
* under the License is distributed on an "AS IS" BASIS, WITHOUT *
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. See accompanying LICENSE file. *
*======================================================================*/


package org.openx.data.jsonserde;

import java.util.Properties;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.serde.Constants;
import org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StructField;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import static org.openx.data.jsonserde.NestedStructureTest.instance;
import org.openx.data.jsonserde.json.JSONObject;

/**
*
* @author rcongiu
*/
public class JsonMapTest {
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,languages,religions");
tbl.setProperty(Constants.LIST_COLUMN_TYPES, "string,string,map<string,string>".toLowerCase());

instance.initialize(conf, tbl);
}

@Test
public void testDeSerialize() throws Exception {
// Test that timestamp object can be deserialized
Writable w = new Text("{\"country\":\"Switzerland\",\"languages\":\"Italian\",\"religions\":\"\"}");

JSONObject result = (JSONObject) instance.deserialize(w);

StructObjectInspector soi = (StructObjectInspector) instance.getObjectInspector();

StructField sfr = soi.getStructFieldRef("religions");

assertEquals(sfr.getFieldObjectInspector().getCategory(),ObjectInspector.Category.MAP);

MapObjectInspector moi = (MapObjectInspector) sfr.getFieldObjectInspector();

Object val = soi.getStructFieldData(result, sfr) ;

assertEquals(-1, moi.getMapSize(val));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,10 @@
import java.util.Properties;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.serde.Constants;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category;
import org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StructField;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaIntObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.JavaLongObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableIntObjectInspector;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
Expand All @@ -31,11 +28,7 @@
import org.junit.Test;
import org.openx.data.jsonserde.json.JSONObject;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/


/**
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
/*======================================================================*
* Copyright (c) 2011, OpenX Technologies, Inc. All rights reserved. *
* *
* Licensed under the New BSD License (the "License"); you may not use *
* this file except in compliance with the License. Unless required *
* by applicable law or agreed to in writing, software distributed *
* under the License is distributed on an "AS IS" BASIS, WITHOUT *
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. See accompanying LICENSE file. *
*======================================================================*/

package org.openx.data.jsonserde;

import org.apache.hadoop.conf.Configuration;
Expand Down

0 comments on commit a387336

Please sign in to comment.