Search in sources :

Example 1 with Field

use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.

the class ThriftToDynamicProto method doSchemaMapping.

private int doSchemaMapping(DescriptorProtos.DescriptorProto.Builder desBuilder, TStructDescriptor fieldDesc) throws DescriptorValidationException {
    int maxThriftId = 0;
    for (Field tField : fieldDesc.getFields()) {
        maxThriftId = Math.max(tField.getFieldId(), maxThriftId);
        if (supportNestedObjects && tField.isMap()) {
            String typeName = mapProtoMessageType(fieldDesc, tField);
            if (descriptorBuilderMap.get(typeName) == null) {
                DescriptorProtos.DescriptorProto.Builder mapBuilder = mapDescriptorProtoBuilder(tField, typeName);
                descriptorBuilderMap.put(typeName, mapBuilder);
                addProtoField(desBuilder, tField.getName(), tField.getFieldId() + 1, typeName, true);
            }
        } else {
            Field field = resolveField(tField);
            Type protoType = thriftTypeToProtoType(field);
            boolean isContainer = isContainer(tField);
            if (supportNestedObjects && protoType == Type.TYPE_MESSAGE) {
                String typeName = resolveMessageTypeName(field.gettStructDescriptor());
                // Protobuf field ids start at 1. Thrift starts at 0.
                addProtoField(desBuilder, tField.getName(), tField.getFieldId() + 1, typeName, isContainer);
            } else if (protoType != null) {
                if (supportNestedObjects || (!supportNestedObjects && !hasNestedObject(tField))) {
                    addProtoField(desBuilder, tField.getName(), tField.getFieldId() + 1, protoType, isContainer);
                }
            }
        }
    }
    return maxThriftId;
}
Also used : Field(com.twitter.elephantbird.thrift.TStructDescriptor.Field) Type(com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Type) TType(org.apache.thrift.protocol.TType) ByteString(com.google.protobuf.ByteString)

Example 2 with Field

use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.

the class ThriftToDynamicProto method doConvert.

/**
 * conver TBase object to Message object
 * @param thriftObj
 */
@SuppressWarnings("unchecked")
public <F extends TFieldIdEnum> Message doConvert(TBase<?, F> thriftObj) {
    if (thriftObj == null) {
        return null;
    }
    Class<TBase<?, F>> clazz = (Class<TBase<?, F>>) thriftObj.getClass();
    checkState(clazz);
    Message.Builder builder = getBuilder(clazz);
    TStructDescriptor fieldDesc = TStructDescriptor.getInstance(clazz);
    int fieldId = 0;
    for (Field tField : fieldDesc.getFields()) {
        // don't want to carry over default values from unset fields
        if (!thriftObj.isSet((F) tField.getFieldIdEnum()) || (!supportNestedObjects && hasNestedObject(tField))) {
            fieldId++;
            continue;
        }
        // recurse into the object if it's a struct, otherwise just add the field
        if (supportNestedObjects && tField.getType() == TType.STRUCT) {
            TBase<?, ?> fieldValue = (TBase<?, ?>) fieldDesc.getFieldValue(fieldId++, thriftObj);
            Message message = doConvert(fieldValue);
            if (message != null) {
                FieldDescriptor protoFieldDesc = builder.getDescriptorForType().findFieldByName(tField.getName());
                builder.setField(protoFieldDesc, message);
            }
        } else {
            fieldId = convertField(thriftObj, builder, fieldDesc, fieldId, tField);
        }
    }
    return builder.build();
}
Also used : Field(com.twitter.elephantbird.thrift.TStructDescriptor.Field) DynamicMessage(com.google.protobuf.DynamicMessage) Message(com.google.protobuf.Message) TBase(org.apache.thrift.TBase) TStructDescriptor(com.twitter.elephantbird.thrift.TStructDescriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 3 with Field

use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.

the class ThriftUtils method readFieldNoTag.

/**
 * Deserializes a thrift field that was serilized with
 * {@link #writeFieldNoTag(TProtocol, Field, Object)}.
 *
 * @throws TException in case of any Thrift errors.
 */
public static Object readFieldNoTag(TProtocol proto, Field field) throws TException {
    Collection<Object> coll = null;
    Field innerField = null;
    switch(field.getType()) {
        case TType.LIST:
            innerField = field.getListElemField();
            coll = Lists.newArrayList();
            break;
        case TType.SET:
            innerField = field.getSetElemField();
            coll = Sets.newHashSet();
            break;
        case TType.MAP:
            innerField = field.getMapKeyField();
            break;
        default:
            return readSingleFieldNoTag(proto, field);
    }
    if (field.getType() == TType.MAP) {
        proto.readByte();
        proto.readByte();
        int nEntries = proto.readI32();
        Map<Object, Object> map = Maps.newHashMap();
        Field valueField = field.getMapValueField();
        for (int i = 0; i < nEntries; i++) {
            map.put(readFieldNoTag(proto, innerField), readFieldNoTag(proto, valueField));
        }
        return map;
    } else {
        // SET or LIST
        proto.readByte();
        int nEntries = proto.readI32();
        for (int i = 0; i < nEntries; i++) {
            coll.add(readFieldNoTag(proto, innerField));
        }
        return coll;
    }
}
Also used : Field(com.twitter.elephantbird.thrift.TStructDescriptor.Field)

Example 4 with Field

use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.

the class PigToThrift method toThrift.

/**
 * Construct a Thrift object from the tuple.
 */
@SuppressWarnings("unchecked")
private static TBase<?, ?> toThrift(TStructDescriptor tDesc, Tuple tuple) {
    int size = tDesc.getFields().size();
    int tupleSize = tuple.size();
    @SuppressWarnings("rawtypes") TBase tObj = newTInstance(tDesc.getThriftClass());
    for (int i = 0; i < size && i < tupleSize; i++) {
        Object pObj;
        try {
            pObj = tuple.get(i);
        } catch (ExecException e) {
            throw new RuntimeException(e);
        }
        if (pObj != null) {
            Field field = tDesc.getFieldAt(i);
            try {
                tObj.setFieldValue(field.getFieldIdEnum(), toThriftValue(field, pObj));
            } catch (Exception e) {
                String value = String.valueOf(tObj);
                final int max_length = 100;
                if (max_length < value.length()) {
                    value = value.substring(0, max_length - 3) + "...";
                }
                String type = tObj == null ? "unknown" : tObj.getClass().getName();
                throw new RuntimeException(String.format("Failed to set field '%s' using tuple value '%s' of type '%s' at index %d", field.getName(), value, type, i), e);
            }
        }
    // if tDesc is a union, at least one field needs to be non-null.
    // user is responsible for ensuring that.
    }
    return tObj;
}
Also used : Field(com.twitter.elephantbird.thrift.TStructDescriptor.Field) ExecException(org.apache.pig.backend.executionengine.ExecException) TBase(org.apache.thrift.TBase) ExecException(org.apache.pig.backend.executionengine.ExecException)

Example 5 with Field

use of com.twitter.elephantbird.thrift.TStructDescriptor.Field in project elephant-bird by twitter.

the class ProjectedThriftTupleFactory method newTuple.

public Tuple newTuple(T tObject) throws ExecException {
    int size = requiredFields.length;
    List<Field> tFields = tStructDesc.getFields();
    Tuple tuple = tf.newTuple(size);
    for (int i = 0; i < size; i++) {
        int idx = requiredFields[i];
        Object value = tStructDesc.getFieldValue(idx, tObject);
        tuple.set(i, ThriftToPig.toPigObject(tFields.get(idx), value, true));
    }
    return tuple;
}
Also used : Field(com.twitter.elephantbird.thrift.TStructDescriptor.Field) RequiredField(org.apache.pig.LoadPushDown.RequiredField) Tuple(org.apache.pig.data.Tuple)

Aggregations

Field (com.twitter.elephantbird.thrift.TStructDescriptor.Field)14 ExecException (org.apache.pig.backend.executionengine.ExecException)3 Tuple (org.apache.pig.data.Tuple)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ThriftField (org.apache.parquet.thrift.struct.ThriftField)2 RequiredField (org.apache.pig.LoadPushDown.RequiredField)2 TBase (org.apache.thrift.TBase)2 ByteString (com.google.protobuf.ByteString)1 Type (com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Type)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 DynamicMessage (com.google.protobuf.DynamicMessage)1 Message (com.google.protobuf.Message)1 ThriftBytesToTuple (com.twitter.elephantbird.pig.piggybank.ThriftBytesToTuple)1 TStructDescriptor (com.twitter.elephantbird.thrift.TStructDescriptor)1 TypeRef (com.twitter.elephantbird.util.TypeRef)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Requirement (org.apache.parquet.thrift.struct.ThriftField.Requirement)1 ThriftType (org.apache.parquet.thrift.struct.ThriftType)1