Search in sources :

Example 1 with GoraRecordWriter

use of org.apache.gora.mapreduce.GoraRecordWriter in project gora by apache.

the class GoraStorage method putNext.

@SuppressWarnings("unchecked")
@Override
public void putNext(Tuple pigTuple) throws IOException {
    PersistentBase persistentObj = this.dataStore.newPersistent();
    if (LOG.isTraceEnabled())
        LOG.trace("key: {}", pigTuple.get(pigFieldKeyIndex));
    for (String fieldName : this.loadQueryFields) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("  Put fieldname: {}", fieldName);
            LOG.trace("      resourcefield schema: {}", this.writeResourceFieldSchemaMap.get(fieldName).getResourceFieldSchema());
            LOG.trace("      value: {} - {}", this.writeResourceFieldSchemaMap.get(fieldName).getIndex(), pigTuple.get(this.writeResourceFieldSchemaMap.get(fieldName).getIndex()));
        }
        ResourceFieldSchemaWithIndex writeResourceFieldSchemaWithIndex = this.writeResourceFieldSchemaMap.get(fieldName);
        if (writeResourceFieldSchemaWithIndex == null) {
            if (LOG.isTraceEnabled())
                LOG.trace("Field {} defined in constructor not found in the tuple to persist, skipping field", fieldName);
            continue;
        }
        Field persistentField = persistentSchema.getField(fieldName);
        if (persistentField == null) {
            throw new IOException("Field " + fieldName + " does not exist in the Gora's Avro schema.");
        }
        ResourceFieldSchema pigFieldSchema = writeResourceFieldSchemaWithIndex.getResourceFieldSchema();
        if (pigFieldSchema == null) {
            throw new IOException("The field " + fieldName + " does not have a Pig schema when writing.");
        }
        // TODO Move this put to PersistentUtils
        // TODO Here is used the resourceFieldSchema and the index. Think about optimize if possible
        // TODO Find a better name to this.writeField, like 'tupleToPersistent'
        int persistentFieldIndex = persistentObj.getSchema().getField(fieldName).pos();
        persistentObj.put(persistentFieldIndex, this.writeField(persistentField.schema(), pigFieldSchema, pigTuple.get(writeResourceFieldSchemaWithIndex.getIndex())));
        persistentObj.setDirty(persistentFieldIndex);
    }
    try {
        ((GoraRecordWriter<Object, PersistentBase>) this.writer).write(pigTuple.get(pigFieldKeyIndex), (PersistentBase) persistentObj);
    } catch (InterruptedException e) {
        throw new IOException("Error writing the tuple.", e);
    }
}
Also used : Field(org.apache.avro.Schema.Field) PersistentBase(org.apache.gora.persistency.impl.PersistentBase) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) IOException(java.io.IOException) GoraRecordWriter(org.apache.gora.mapreduce.GoraRecordWriter)

Aggregations

IOException (java.io.IOException)1 Field (org.apache.avro.Schema.Field)1 GoraRecordWriter (org.apache.gora.mapreduce.GoraRecordWriter)1 PersistentBase (org.apache.gora.persistency.impl.PersistentBase)1 ResourceFieldSchema (org.apache.pig.ResourceSchema.ResourceFieldSchema)1