Search in sources :

Example 11 with Field

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

the class TestThriftToPig method thriftToPig.

static <M extends TBase<?, ?>> Tuple thriftToPig(M obj) throws TException {
    // it is very inefficient to create one ThriftToPig for each Thrift object,
    // but good enough for unit testing.
    TypeRef<M> typeRef = new TypeRef<M>(obj.getClass()) {
    };
    ThriftToPig<M> thriftToPig = ThriftToPig.newInstance(typeRef);
    Tuple t = thriftToPig.getPigTuple(obj);
    // test projected tuple. project a subset of fields based on field name.
    List<Field> tFields = thriftToPig.getTStructDescriptor().getFields();
    List<Integer> idxList = Lists.newArrayList();
    RequiredFieldList reqFieldList = new RequiredFieldList();
    for (int i = 0; i < tFields.size(); i++) {
        String name = tFields.get(i).getName();
        if (name.hashCode() % 2 == 0) {
            RequiredField rf = new RequiredField();
            rf.setAlias(name);
            rf.setIndex(i);
            reqFieldList.add(rf);
            idxList.add(i);
        }
    }
    try {
        Tuple pt = new ProjectedThriftTupleFactory<M>(typeRef, reqFieldList).newTuple(obj);
        int pidx = 0;
        for (int idx : idxList) {
            if (t.get(idx) != pt.get(pidx)) {
                // if both are not nulls
                assertEquals(t.get(idx).toString(), pt.get(pidx).toString());
            }
            pidx++;
        }
    } catch (ExecException e) {
        // not expected
        throw new TException(e);
    }
    // return the full tuple
    return t;
}
Also used : TException(org.apache.thrift.TException) TypeRef(com.twitter.elephantbird.util.TypeRef) ExecException(org.apache.pig.backend.executionengine.ExecException) Field(com.twitter.elephantbird.thrift.TStructDescriptor.Field) RequiredField(org.apache.pig.LoadPushDown.RequiredField) RequiredFieldList(org.apache.pig.LoadPushDown.RequiredFieldList) RequiredField(org.apache.pig.LoadPushDown.RequiredField) ThriftBytesToTuple(com.twitter.elephantbird.pig.piggybank.ThriftBytesToTuple) Tuple(org.apache.pig.data.Tuple)

Example 12 with Field

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

the class ThriftToPig method toTuple.

@SuppressWarnings("rawtypes")
private static <T extends TBase> Tuple toTuple(TStructDescriptor tDesc, T tObj) {
    int size = tDesc.getFields().size();
    Tuple tuple = tupleFactory.newTuple(size);
    for (int i = 0; i < size; i++) {
        Field field = tDesc.getFieldAt(i);
        Object value = tDesc.getFieldValue(i, tObj);
        try {
            tuple.set(i, toPigObject(field, value, false));
        } catch (ExecException e) {
            // not expected
            throw new RuntimeException(e);
        }
    }
    return tuple;
}
Also used : Field(com.twitter.elephantbird.thrift.TStructDescriptor.Field) ExecException(org.apache.pig.backend.executionengine.ExecException) Tuple(org.apache.pig.data.Tuple)

Example 13 with Field

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

the class ThriftUtils method writeFieldNoTag.

/**
 * Serializes a single field of a thrift struct.
 *
 * @throws TException
 */
public static void writeFieldNoTag(TProtocol proto, Field field, Object value) throws TException {
    if (value == null) {
        return;
    }
    Field innerField = null;
    switch(field.getType()) {
        case TType.LIST:
            innerField = field.getListElemField();
            break;
        case TType.SET:
            innerField = field.getSetElemField();
            break;
        case TType.MAP:
            innerField = field.getMapKeyField();
            break;
        default:
            writeSingleFieldNoTag(proto, field, value);
            return;
    }
    if (field.getType() == TType.MAP) {
        Field valueField = field.getMapValueField();
        Map<?, ?> map = (Map<?, ?>) value;
        proto.writeByte(innerField.getType());
        proto.writeByte(valueField.getType());
        proto.writeI32(map.size());
        for (Entry<?, ?> entry : map.entrySet()) {
            writeSingleFieldNoTag(proto, innerField, entry.getKey());
            writeSingleFieldNoTag(proto, valueField, entry.getValue());
        }
    } else {
        // SET or LIST
        Collection<?> coll = (Collection<?>) value;
        proto.writeByte(innerField.getType());
        proto.writeI32(coll.size());
        for (Object v : coll) {
            writeSingleFieldNoTag(proto, innerField, v);
        }
    }
}
Also used : Field(com.twitter.elephantbird.thrift.TStructDescriptor.Field) Collection(java.util.Collection) Map(java.util.Map)

Example 14 with Field

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

the class ThriftToDynamicProto method mapDescriptorProtoBuilder.

/**
 * Generate a DescriptorProto.Builder for the Message type that will be used
 * to represent the entries of the input Map field.
 *
 * @param field a Map Field (field.isMap() == true)
 * @param typeName name of new message type
 */
private DescriptorProtos.DescriptorProto.Builder mapDescriptorProtoBuilder(Field field, String typeName) throws DescriptorValidationException {
    DescriptorProtos.DescriptorProto.Builder mapBuilder = DescriptorProtos.DescriptorProto.newBuilder().setName(typeName);
    Field keyField = field.getMapKeyField();
    Field valueField = field.getMapValueField();
    DescriptorProtos.FieldDescriptorProto.Builder keyBuilder = mapKeyProtoBuilder();
    DescriptorProtos.FieldDescriptorProto.Builder valueBuilder = mapValueProtoBuilder();
    setBuilderTypeFromField(keyField, keyBuilder);
    setBuilderTypeFromField(valueField, valueBuilder);
    mapBuilder.addField(keyBuilder.build());
    mapBuilder.addField(valueBuilder.build());
    return mapBuilder;
}
Also used : Field(com.twitter.elephantbird.thrift.TStructDescriptor.Field)

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