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();
}
}
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);
}
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);
}
}
}
}
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;
}
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();
}
Aggregations