/
JsonStructObjectInspector.java
132 lines (113 loc) · 4.53 KB
/
JsonStructObjectInspector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*======================================================================*
* 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.StandardStructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.StructField;
import org.openx.data.jsonserde.json.JSONException;
import org.openx.data.jsonserde.json.JSONObject;
/**
* This Object Inspector is used to look into a JSonObject object.
* We couldn't use StandardStructObjectInspector since that expects
* something that can be cast to an Array<Object>.
* @author rcongiu
*/
public class JsonStructObjectInspector extends StandardStructObjectInspector {
JsonStructOIOptions options = null;
/*
public JsonStructObjectInspector(List<String> structFieldNames,
List<ObjectInspector> structFieldObjectInspectors) {
super(structFieldNames, structFieldObjectInspectors);
} */
public JsonStructObjectInspector(List<String> structFieldNames,
List<ObjectInspector> structFieldObjectInspectors,JsonStructOIOptions opts) {
super(structFieldNames, structFieldObjectInspectors);
options = opts;
}
/**
* Extract the data from the requested field.
*
* @param data
* @param fieldRef
* @return
*/
@Override
public Object getStructFieldData(Object data, StructField fieldRef) {
if (data == null) {
return null;
}
JSONObject obj = null ;
if( data instanceof JSONObject) {
return getStructFieldDataFromJsonObject((JSONObject) data, fieldRef );
} if (data instanceof List) {
// somehow we have the object parsed already
return getStructFieldDataFromList((List) data, fieldRef );
} else {
throw new Error("Data is not JSONObject but " + data.getClass().getCanonicalName() +
" with value " + data.toString()) ;
}
}
public Object getStructFieldDataFromList(List data, StructField fieldRef ) {
int idx = fields.indexOf(fieldRef);
if(idx <0 || idx >= data.size()) {
return null;
} else {
return data.get(idx);
}
}
public Object getStructFieldDataFromJsonObject(JSONObject data, StructField fieldRef ) {
if (data == null) {
return null;
}
MyField f = (MyField) fieldRef;
int fieldID = f.getFieldID();
assert (fieldID >= 0 && fieldID < fields.size());
try {
if (data.has(getJsonField(fieldRef))) {
return data.get(getJsonField(fieldRef));
} else {
return null;
}
} catch (JSONException ex) {
// if key does not exist
return null;
}
}
/**
* called to map from hive to json
* @param fr
* @return
*/
protected String getJsonField(StructField fr) {
if(options.getMappings() != null && options.getMappings().containsKey(fr.getFieldName())) {
return options.getMappings().get(fr.getFieldName());
} else {
return fr.getFieldName();
}
}
List<Object> values = new ArrayList<Object>();
@Override
public List<Object> getStructFieldsDataAsList(Object o) {
JSONObject jObj = (JSONObject) o;
values.clear();
for (int i = 0; i < fields.size(); i++) {
if (jObj.has(getJsonField(fields.get(i)))){
values.add(getStructFieldData(o, fields.get(i)));
} else {
values.add(null);
}
}
return values;
}
}