Search in sources :

Example 1 with TField

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

the class TProtocolSerializer method writeMessage.

private void writeMessage(PMessage<?, ?> message, TProtocol protocol) throws TException, SerializerException {
    PMessageDescriptor<?, ?> type = message.descriptor();
    protocol.writeStructBegin(new TStruct(message.descriptor().getQualifiedName()));
    for (PField field : type.getFields()) {
        if (!message.has(field.getId())) {
            continue;
        }
        protocol.writeFieldBegin(new TField(field.getName(), forType(field.getDescriptor().getType()), (short) field.getId()));
        writeTypedValue(message.get(field.getId()), field.getDescriptor(), protocol);
        protocol.writeFieldEnd();
    }
    protocol.writeFieldStop();
    protocol.writeStructEnd();
}
Also used : TField(org.apache.thrift.protocol.TField) PField(net.morimekta.providence.descriptor.PField) TStruct(org.apache.thrift.protocol.TStruct)

Example 2 with TField

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

the class ProtocolReadToWrite method readOneStruct.

private void readOneStruct(TProtocol in, TProtocol out) throws TException {
    final TStruct struct = in.readStructBegin();
    out.writeStructBegin(struct);
    TField field;
    while ((field = in.readFieldBegin()).type != TType.STOP) {
        out.writeFieldBegin(field);
        readOneValue(in, out, field.type);
        in.readFieldEnd();
        out.writeFieldEnd();
    }
    out.writeFieldStop();
    in.readStructEnd();
    out.writeStructEnd();
}
Also used : TField(org.apache.thrift.protocol.TField) TStruct(org.apache.thrift.protocol.TStruct)

Example 3 with TField

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

the class BufferedProtocolReadToWrite method readOneStruct.

private boolean readOneStruct(TProtocol in, List<Action> buffer, StructType type) throws TException {
    final TStruct struct = in.readStructBegin();
    buffer.add(new Action() {

        @Override
        public void write(TProtocol out) throws TException {
            out.writeStructBegin(struct);
        }

        @Override
        public String toDebugString() {
            return "(";
        }
    });
    TField field;
    boolean hasFieldsIgnored = false;
    int childFieldsPresent = 0;
    while ((field = in.readFieldBegin()).type != TType.STOP) {
        final TField currentField = field;
        ThriftField expectedField;
        if ((expectedField = type.getChildById(field.id)) == null) {
            handleUnrecognizedField(field, type, in);
            hasFieldsIgnored |= true;
            continue;
        }
        childFieldsPresent++;
        buffer.add(new Action() {

            @Override
            public void write(TProtocol out) throws TException {
                out.writeFieldBegin(currentField);
            }

            @Override
            public String toDebugString() {
                return "f=" + currentField.id + "<t=" + typeName(currentField.type) + ">: ";
            }
        });
        hasFieldsIgnored |= readOneValue(in, field.type, buffer, expectedField.getType());
        in.readFieldEnd();
        buffer.add(FIELD_END);
    }
    // check that union had exactly 1 (no more no less) child fields.
    assertUnionHasExactlyOneChild(type, childFieldsPresent);
    in.readStructEnd();
    buffer.add(STRUCT_END);
    return hasFieldsIgnored;
}
Also used : TException(org.apache.thrift.TException) TField(org.apache.thrift.protocol.TField) TProtocol(org.apache.thrift.protocol.TProtocol) TStruct(org.apache.thrift.protocol.TStruct) ThriftField(org.apache.parquet.thrift.struct.ThriftField)

Example 4 with TField

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

the class TestProtocolReadToWrite method testMissingFieldHandling.

/**
 * When data contains extra field, it should notify the handler and read the data with extra field dropped
 * @throws Exception
 */
@Test
public void testMissingFieldHandling() throws Exception {
    CountingErrorHandler countingHandler = new CountingErrorHandler() {

        @Override
        public void handleFieldIgnored(TField field) {
            assertEquals(field.id, 4);
            fieldIgnoredCount++;
        }
    };
    BufferedProtocolReadToWrite structForRead = new BufferedProtocolReadToWrite(ThriftSchemaConverter.toStructType(StructV3.class), countingHandler);
    // Data has an extra field of type struct
    final ByteArrayOutputStream in = new ByteArrayOutputStream();
    StructV4WithExtracStructField dataWithNewSchema = new StructV4WithExtracStructField("name");
    dataWithNewSchema.setAge("10");
    dataWithNewSchema.setGender("male");
    StructV3 structV3 = new StructV3("name");
    structV3.setAge("10");
    dataWithNewSchema.setAddedStruct(structV3);
    dataWithNewSchema.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));
    // record will be read without extra field
    assertEquals(1, countingHandler.recordCountOfMissingFields);
    assertEquals(1, countingHandler.fieldIgnoredCount);
    StructV4WithExtracStructField b = StructV4WithExtracStructField.class.newInstance();
    b.read(protocol(new ByteArrayInputStream(out.toByteArray())));
    assertEquals(dataWithNewSchema.getName(), b.getName());
    assertEquals(dataWithNewSchema.getAge(), b.getAge());
    assertEquals(dataWithNewSchema.getGender(), b.getGender());
    assertEquals(null, b.getAddedStruct());
}
Also used : TField(org.apache.thrift.protocol.TField) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with TField

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

the class ThriftRequestProperty method writeTraceHeader.

public void writeTraceHeader(ThriftHeader headerKey, TProtocol oprot) throws TException {
    Object headerValue = this.thriftHeaders.get(headerKey);
    if (headerValue == null) {
        return;
    }
    byte headerType = headerKey.getType();
    TField traceField = new TField(headerKey.name(), headerKey.getType(), headerKey.getId());
    oprot.writeFieldBegin(traceField);
    try {
        if (headerType == TType.STRING) {
            // these will be read as byte buffer although it's probably safe to just use writeString here.
            // see org.apache.thrift.protocol.TProtocolUtil.skip(TProtocol, byte, int)
            oprot.writeBinary(stringToByteBuffer((String) headerValue));
        } else if (headerType == TType.I64) {
            oprot.writeI64((Long) headerValue);
        } else if (headerType == TType.I16) {
            oprot.writeI16((Short) headerValue);
        } else if (headerType == TType.BOOL) {
            oprot.writeBool((Boolean) headerValue);
        } else {
            throw new TProtocolException("Invalid pinpoint header type - " + headerType);
        }
    } finally {
        oprot.writeFieldEnd();
    }
}
Also used : TField(org.apache.thrift.protocol.TField) TProtocolException(org.apache.thrift.protocol.TProtocolException)

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