Search in sources :

Example 11 with BeanFactoryImpl

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;
}
Also used : Field(org.apache.avro.Schema.Field) GoraException(org.apache.gora.util.GoraException) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) JSONObject(org.json.JSONObject) Schema(org.apache.avro.Schema) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl)

Example 12 with BeanFactoryImpl

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;
    }
}
Also used : Type(org.apache.avro.Schema.Type) GoraException(org.apache.gora.util.GoraException) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) BeanFactoryImpl(org.apache.gora.persistency.impl.BeanFactoryImpl)

Aggregations

BeanFactoryImpl (org.apache.gora.persistency.impl.BeanFactoryImpl)12 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)8 Schema (org.apache.avro.Schema)7 GoraException (org.apache.gora.util.GoraException)6 Field (org.apache.avro.Schema.Field)4 WebPage (org.apache.gora.examples.generated.WebPage)4 Test (org.junit.Test)4 IOException (java.io.IOException)2 DocumentFieldType (org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)2 BSONDecorator (org.apache.gora.mongodb.utils.BSONDecorator)2 MapObject (com.rethinkdb.model.MapObject)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 Type (org.apache.avro.Schema.Type)1 Field (org.apache.gora.elasticsearch.mapping.Field)1 JSONObject (org.json.JSONObject)1