Skip to content

Commit

Permalink
refactoring, almost got it to work
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberto Congiu committed May 29, 2014
1 parent 5c303d8 commit bc10f2a
Show file tree
Hide file tree
Showing 112 changed files with 1,685 additions and 1 deletion.
57 changes: 57 additions & 0 deletions json-serde-cdh4-shim/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>org.openx.data</groupId>
<artifactId>json-serde</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>org.openx.data</groupId>
<artifactId>json-serde-cdh4-shim</artifactId>
<packaging>jar</packaging>

<name>json-cdh4-shim</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.openx.data</groupId>
<artifactId>json</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-serde</artifactId>
<version>${cdh.hive.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>${cdh.hive.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${cdh.hadoop.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>



Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions json-serde-cdh4-shim/target/maven-archiver/pom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#Generated by Maven
#Wed May 28 18:04:55 PDT 2014
version=1.2-SNAPSHOT
groupId=org.openx.data
artifactId=json-serde-cdh4-shim
Empty file.
59 changes: 59 additions & 0 deletions json-serde-cdh5-shim/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<parent>
<groupId>org.openx.data</groupId>
<artifactId>json-serde</artifactId>
<relativePath>../pom.xml</relativePath>
<version>1.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<groupId>org.openx.data</groupId>
<artifactId>json-serde-cdh5-shim</artifactId>
<packaging>jar</packaging>

<name>json-cdh5-shim</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.openx.data</groupId>
<artifactId>json</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>


<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-serde</artifactId>
<version>${cdh.hive.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-exec</artifactId>
<version>${cdh.hive.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${cdh.hadoop.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>



Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*======================================================================*
* 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.objectinspector;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.openx.data.jsonserde.json.JSONException;
import org.openx.data.jsonserde.json.JSONObject;

/**
* JSONObject is technically different from a map, since a json object
* has string keys and values can be of any kind, while hive could
* define a column as map<string,string>. This consistency is left
* to the user to satisfy.
*
* Since we want to present a map, and we don't want to extract keys
* and values every time, we keep a cache
*
* @author rcongiu
*/
/* TODO: having an initialize() that copies everything into a map is expensive
and won't work well for large maps. Good enough for now since
we seldom - if ever - use maps. */
public class JSONObjectMapAdapter implements Map {
HashMap cache;
JSONObject jsonObject;

public JSONObjectMapAdapter(JSONObject obj) {
jsonObject = obj;
initialize();
}

public JSONObjectMapAdapter() {

}

public JSONObject getJSONObject() {
return jsonObject;
}

public void setJSONObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
initialize();
}



@Override
public int size() {
return jsonObject.length();
}

@Override
public boolean isEmpty() {
return cache.isEmpty();
}

protected final void initialize() {
if(cache==null) cache = new HashMap();

for(Iterator<String> i = jsonObject.keys(); i.hasNext(); ) {
String o = i.next();
try {
cache.put(o, safeGet(o));
} catch (JSONException ex) {
// if key does not exist - should not happen
throw new RuntimeException("Non existent key - should never happen!");
}
}
}

/**
* sanitize all the JSONObject.NULL converting them to nulls.
* @param s
* @return
* @throws JSONException
*/
private Object safeGet(String s) throws JSONException {
if(s == null) return null;
Object obj = jsonObject.get(s);
if(JSONObject.NULL.equals(obj)) {
return null;
} else {
return obj;
}
}

@Override
public boolean containsKey(Object key) {
return cache.containsKey(key);
}

@Override
public boolean containsValue(Object value) {
return cache.containsValue(value);
}

@Override
public Object get(Object key) {
return cache.get(key);
}

@Override
public Object put(Object key, Object value) {
return cache.put(key,value);
}

@Override
public Object remove(Object key) {
return cache.remove(key);
}

@Override
public void putAll(Map m) {
cache.putAll(m);
}

@Override
public void clear() {
cache.clear();
}

@Override
public Set keySet() {
return cache.keySet();
}

@Override
public Collection values() {
return cache.values();
}

@Override
public Set entrySet() {
return cache.entrySet();
}

public Map<?, ?> getMap() {
return cache;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*======================================================================*
* 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.objectinspector;

import java.util.ArrayList;
import java.util.List;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector;
import org.openx.data.jsonserde.json.JSONArray;
import org.openx.data.jsonserde.json.JSONException;
import org.openx.data.jsonserde.json.JSONObject;

/**
*
* @author rcongiu
*/
public class JsonListObjectInspector extends StandardListObjectInspector {

JsonListObjectInspector(ObjectInspector listElementObjectInspector) {
super(listElementObjectInspector);
}

@Override
public List<?> getList(Object data) {
if (data == null || JSONObject.NULL.equals(data)) {
return null;
}
JSONArray array = (JSONArray) data;
List al = new ArrayList(array.length());
for(int i =0; i< array.length(); i++) {
al.add(getListElement(data,i));
}
return al;
}

@Override
public Object getListElement(Object data, int index) {
if (data == null) {
return null;
}
JSONArray array = (JSONArray) data;
try {
Object obj = array.get(index);
if(JSONObject.NULL == obj) {
return null;
} else {
return obj;
}
} catch(JSONException ex) {
return null;
}
}

@Override
public int getListLength(Object data) {
if (data == null) {
return -1;
}
JSONArray array = (JSONArray) data;
return array.length();
}

}
Loading

0 comments on commit bc10f2a

Please sign in to comment.