use of org.apache.gora.persistency.impl.BeanFactoryImpl in project gora by apache.
the class HiveResultParser method parseRecord.
/**
* Parse a json object to a persistent record.
*
* @param value json object string
* @param schema record schema to be used for parsing the object
* @return persistent object
* @throws GoraException throw if parsing record value is failed
*/
private Object parseRecord(Object value, Schema schema) throws GoraException {
if (LOG.isDebugEnabled()) {
LOG.debug("Parsing json object:{} as a record", value);
}
Class<?> clazz;
try {
clazz = ClassLoadingUtils.loadClass(schema.getFullName());
} catch (ClassNotFoundException e) {
throw new GoraException(e);
}
@SuppressWarnings("unchecked") final PersistentBase record = (PersistentBase) new BeanFactoryImpl(hiveStore.getKeyClass(), clazz).newPersistent();
JSONObject recordObject = new JSONObject(String.valueOf(value));
for (Field recField : schema.getFields()) {
Schema innerSchema = recField.schema();
if (recordObject.has(recField.name())) {
record.put(recField.pos(), parseSchema(innerSchema, recordObject.get(recField.name())));
}
}
return record;
}
use of org.apache.gora.persistency.impl.BeanFactoryImpl in project gora by apache.
the class HiveQueryBuilder method getNullValue.
/**
* Generate the null value for a given schema type
*
* @param parameterList carries the list of parameters to be injected into sql
* @param schema schema to get null type
* @return null value for the schema.type
* @throws GoraException throw if the null value generation is failed
*/
private Object getNullValue(List<Object> parameterList, Schema schema) throws GoraException {
final Type type = schema.getType();
switch(type) {
case BYTES:
return "binary(null)";
case MAP:
return "map(null," + getNullValue(parameterList, schema.getValueType()) + CLOSE_BRACKET_SYMBOL;
case ARRAY:
return "array(" + getNullValue(parameterList, schema.getElementType()) + CLOSE_BRACKET_SYMBOL;
case UNION:
return serializeUnion(parameterList, schema, null);
case RECORD:
Class<?> clazz;
try {
clazz = ClassLoadingUtils.loadClass(schema.getFullName());
} catch (ClassNotFoundException e) {
throw new GoraException(e);
}
@SuppressWarnings("unchecked") final PersistentBase emptyRecord = (PersistentBase) new BeanFactoryImpl(hiveStore.getKeyClass(), clazz).newPersistent();
return serializeRecord(parameterList, schema, emptyRecord);
default:
return null;
}
}
Aggregations