Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add add.raw.json.column SerDe property #51

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/main/java/org/openx/data/jsonserde/JsonSerDe.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ public class JsonSerDe implements SerDe {
long serializedDataSize;
// if set, will ignore malformed JSON in deserialization
boolean ignoreMalformedJson = false;
// if set, will add column that contains raw json string
String rawJsonColumn = null;
public static final String PROP_IGNORE_MALFORMED_JSON = "ignore.malformed.json";

public static final String PROP_ADD_RAW_JSON_COLUMN = "add.raw.json.column";

JsonStructOIOptions options;

/**
Expand Down Expand Up @@ -115,6 +118,15 @@ public void initialize(Configuration conf, Properties tbl) throws SerDeException
}
assert (columnNames.size() == columnTypes.size());

// add field with raw json string
rawJsonColumn = tbl.getProperty(PROP_ADD_RAW_JSON_COLUMN);
if (rawJsonColumn != null) {
columnNames = new ArrayList<String>(columnNames);
columnTypes = new ArrayList<TypeInfo>(columnTypes);
columnNames.add(rawJsonColumn);
columnTypes.add(TypeInfoFactory.stringTypeInfo);
}

stats = new SerDeStats();

// Create row related objects
Expand Down Expand Up @@ -201,6 +213,9 @@ public JSONObject put(String key, Object value) throws JSONException {
return super.put(key.toLowerCase(), value);
}
};
if (rawJsonColumn != null) {
jObj.put(rawJsonColumn, rowText.toString());
}
} catch (JSONException e) {
// If row is not a JSON object, make the whole row NULL
onMalformedJson("Row is not a valid JSON Object - JSONException: "
Expand Down Expand Up @@ -287,6 +302,9 @@ private JSONObject serializeStruct( Object obj,

for (int i =0; i< fields.size(); i++) {
StructField sf = fields.get(i);
if (sf.getFieldName().equals(rawJsonColumn)) {
continue;
}
Object data = soi.getStructFieldData(obj, sf);

if (null != data) {
Expand Down