Search in sources :

Example 11 with TField

use of org.apache.thrift.protocol.TField in project hive by apache.

the class DynamicSerDeFieldList method serialize.

public void serialize(Object o, ObjectInspector oi, TProtocol oprot) throws TException, SerDeException, NoSuchFieldException, IllegalAccessException {
    // This assumption should be checked during query compile time.
    assert (oi instanceof StructObjectInspector);
    StructObjectInspector soi = (StructObjectInspector) oi;
    boolean writeNulls = oprot instanceof org.apache.hadoop.hive.serde2.thrift.WriteNullsProtocol;
    // For every field
    List<? extends StructField> fields = soi.getAllStructFieldRefs();
    if (fields.size() != ordered_types.length) {
        throw new SerDeException("Trying to serialize " + fields.size() + " fields into a struct with " + ordered_types.length + " object=" + o + " objectinspector=" + oi.getTypeName());
    }
    for (int i = 0; i < fields.size(); i++) {
        Object f = soi.getStructFieldData(o, fields.get(i));
        DynamicSerDeTypeBase mt = ordered_types[i];
        if (f == null && !writeNulls) {
            continue;
        }
        if (thrift_mode) {
            field = new TField(mt.name, mt.getType(), (short) mt.fieldid);
            oprot.writeFieldBegin(field);
        }
        if (f == null) {
            ((org.apache.hadoop.hive.serde2.thrift.WriteNullsProtocol) oprot).writeNull();
        } else {
            mt.serialize(f, fields.get(i).getFieldObjectInspector(), oprot);
        }
        if (thrift_mode) {
            oprot.writeFieldEnd();
        }
    }
    if (thrift_mode) {
        oprot.writeFieldStop();
    }
}
Also used : TField(org.apache.thrift.protocol.TField) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)

Example 12 with TField

use of org.apache.thrift.protocol.TField in project parquet-mr by apache.

the class TestProtocolReadToWrite method TestExtraFieldWhenFieldIndexIsNotStartFromZero.

@Test
public void TestExtraFieldWhenFieldIndexIsNotStartFromZero() throws Exception {
    CountingErrorHandler countingHandler = new CountingErrorHandler() {

        @Override
        public void handleFieldIgnored(TField field) {
            assertEquals(3, field.id);
            fieldIgnoredCount++;
        }
    };
    BufferedProtocolReadToWrite structForRead = new BufferedProtocolReadToWrite(ThriftSchemaConverter.toStructType(StructWithIndexStartsFrom4.class), countingHandler);
    // Data has an extra field of type struct
    final ByteArrayOutputStream in = new ByteArrayOutputStream();
    StructWithExtraField dataWithNewExtraField = new StructWithExtraField(new Phone("111", "222"), new Phone("333", "444"));
    dataWithNewExtraField.write(protocol(in));
    // read using the schema that doesn't have the extra field
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    structForRead.readOne(protocol(new ByteArrayInputStream(in.toByteArray())), protocol(out));
    assertEquals(1, countingHandler.recordCountOfMissingFields);
    assertEquals(1, countingHandler.fieldIgnoredCount);
}
Also used : TField(org.apache.thrift.protocol.TField) ByteArrayInputStream(java.io.ByteArrayInputStream) Phone(org.apache.parquet.thrift.test.Phone) StructWithIndexStartsFrom4(org.apache.parquet.thrift.test.StructWithIndexStartsFrom4) StructWithExtraField(org.apache.parquet.thrift.test.StructWithExtraField) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 13 with TField

use of org.apache.thrift.protocol.TField in project pinpoint by naver.

the class TProtocolReadFieldBeginInterceptor method after.

@Override
public void after(Object target, Object[] args, Object result, Throwable throwable) {
    if (isDebug) {
        logger.afterInterceptor(target, args, result, throwable);
    }
    if (!validate(target)) {
        return;
    }
    final boolean shouldTrace = ((ServerMarkerFlagFieldAccessor) target)._$PINPOINT$_getServerMarkerFlag();
    if (shouldTrace) {
        InterceptorScopeInvocation currentTransaction = this.scope.getCurrentInvocation();
        Object attachment = currentTransaction.getAttachment();
        if (attachment instanceof ThriftClientCallContext) {
            ThriftClientCallContext clientCallContext = (ThriftClientCallContext) attachment;
            if (result instanceof TField) {
                handleClientRequest((TField) result, clientCallContext);
            }
        }
    }
}
Also used : InterceptorScopeInvocation(com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation) TField(org.apache.thrift.protocol.TField) ThriftClientCallContext(com.navercorp.pinpoint.plugin.thrift.ThriftClientCallContext) ServerMarkerFlagFieldAccessor(com.navercorp.pinpoint.plugin.thrift.field.accessor.ServerMarkerFlagFieldAccessor)

Example 14 with TField

use of org.apache.thrift.protocol.TField in project commons by twitter.

the class StructContext method computeFieldNameMap.

/**
 * Compute a new field name map for the current thrift message
 * we are parsing.
 */
private Map<String, TField> computeFieldNameMap() {
    Map<String, TField> map = new HashMap<String, TField>();
    Class<? extends TBase> clazz = getCurrentThriftMessageClass();
    // Get the metaDataMap for this Thrift class
    Map<? extends TFieldIdEnum, FieldMetaData> metaDataMap = FieldMetaData.getStructMetaDataMap(clazz);
    for (TFieldIdEnum key : metaDataMap.keySet()) {
        final String fieldName = key.getFieldName();
        final FieldMetaData metaData = metaDataMap.get(key);
        // Workaround a bug in the generated thrift message read()
        // method by mapping the ENUM type to the INT32 type
        // The thrift generated parsing code requires that, when expecting
        // a value of enum, we actually parse a value of type int32. The
        // generated read() method then looks up the enum value in a map.
        byte type = (TType.ENUM == metaData.valueMetaData.type) ? TType.I32 : metaData.valueMetaData.type;
        map.put(fieldName, new TField(fieldName, type, key.getThriftFieldId()));
    }
    return map;
}
Also used : FieldMetaData(org.apache.thrift.meta_data.FieldMetaData) TField(org.apache.thrift.protocol.TField) HashMap(java.util.HashMap) TFieldIdEnum(org.apache.thrift.TFieldIdEnum)

Example 15 with TField

use of org.apache.thrift.protocol.TField in project providence by morimekta.

the class TProtocolSerializer method readMessage.

private <Message extends PMessage<Message, Field>, Field extends PField> Message readMessage(TProtocol protocol, PMessageDescriptor<Message, Field> descriptor) throws SerializerException, TException {
    TField f;
    PMessageBuilder<Message, Field> builder = descriptor.builder();
    // ignored.
    protocol.readStructBegin();
    while ((f = protocol.readFieldBegin()) != null) {
        if (f.type == BinaryType.STOP) {
            break;
        }
        PField field;
        // f.name is never fulled out, rely on f.id being correct.
        field = descriptor.findFieldById(f.id);
        if (field != null) {
            if (f.type != forType(field.getDescriptor().getType())) {
                throw new SerializerException("Incompatible serialized type " + asString(f.type) + " for field " + field.getName() + ", expected " + asString(forType(field.getDescriptor().getType())));
            }
            Object value = readTypedValue(f.type, field.getDescriptor(), protocol, true);
            if (value != null) {
                builder.set(field.getId(), value);
            }
        } else {
            TProtocolUtil.skip(protocol, f.type);
        }
        protocol.readFieldEnd();
    }
    protocol.readStructEnd();
    if (readStrict) {
        try {
            builder.validate();
        } catch (IllegalStateException e) {
            throw new SerializerException(e, e.getMessage());
        }
    }
    return builder.build();
}
Also used : TField(org.apache.thrift.protocol.TField) PField(net.morimekta.providence.descriptor.PField) TField(org.apache.thrift.protocol.TField) PMessage(net.morimekta.providence.PMessage) TMessage(org.apache.thrift.protocol.TMessage) PField(net.morimekta.providence.descriptor.PField) SerializerException(net.morimekta.providence.serializer.SerializerException)

Aggregations

TField (org.apache.thrift.protocol.TField)17 TStruct (org.apache.thrift.protocol.TStruct)6 Test (org.junit.Test)5 Properties (java.util.Properties)3 Configuration (org.apache.hadoop.conf.Configuration)3 TCTLSeparatedProtocol (org.apache.hadoop.hive.serde2.thrift.TCTLSeparatedProtocol)3 TConfiguration (org.apache.thrift.TConfiguration)3 TList (org.apache.thrift.protocol.TList)3 TMap (org.apache.thrift.protocol.TMap)3 TMemoryBuffer (org.apache.thrift.transport.TMemoryBuffer)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 PField (net.morimekta.providence.descriptor.PField)2 JsonElement (com.google.gson.JsonElement)1 InterceptorScopeInvocation (com.navercorp.pinpoint.bootstrap.interceptor.scope.InterceptorScopeInvocation)1 ThriftClientCallContext (com.navercorp.pinpoint.plugin.thrift.ThriftClientCallContext)1 ServerMarkerFlagFieldAccessor (com.navercorp.pinpoint.plugin.thrift.field.accessor.ServerMarkerFlagFieldAccessor)1 ArrayList (java.util.ArrayList)1 EnumMap (java.util.EnumMap)1