org.apache.thrift.protocol.TField Java Examples

The following examples show how to use org.apache.thrift.protocol.TField. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ThriftRequestProperty.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
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();
    }
}
 
Example #2
Source File: ThriftProtocolAgentIntercept.java    From java-specialagent with Apache License 2.0 6 votes vote down vote up
public static void writeFieldStop(final Object protocol) throws TException {
  if (injected.get())
    return;

  final TProtocol tProtocol = (TProtocol)protocol;
  final Span span = spanHolder.get();
  if (span == null)
    return;

  final Map<String,String> map = new HashMap<>();
  GlobalTracer.get().inject(span.context(), Builtin.TEXT_MAP, new TextMapAdapter(map));

  tProtocol.writeFieldBegin(new TField("span", TType.MAP, SPAN_FIELD_ID));
  tProtocol.writeMapBegin(new TMap(TType.STRING, TType.STRING, map.size()));
  for (final Entry<String,String> entry : map.entrySet()) {
    tProtocol.writeString(entry.getKey());
    tProtocol.writeString(entry.getValue());
  }

  tProtocol.writeMapEnd();
  tProtocol.writeFieldEnd();
  injected.set(true);
}
 
Example #3
Source File: BufferedProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 6 votes vote down vote up
private void handleUnrecognizedField(TField field, StructType type, TProtocol in) throws TException {
  switch (type.getStructOrUnionType()) {
    case STRUCT:
      // this is an unrecognized field in a struct, not a union
      notifyIgnoredFieldsOfRecord(field);
      //read the value and ignore it, NullProtocol will do nothing
      new ProtocolReadToWrite().readOneValue(in, new NullProtocol(), field.type);
      break;
    case UNION:
      // this is a union with an unrecognized member -- this is fatal for this record
      // in the write path, because it will be unreadable in the read path.
      // throwing here means we will either skip this record entirely, or fail completely.
      throw new DecodingSchemaMismatchException("Unrecognized union member with id: "
          + field.id + " for struct:\n" + type);
    case UNKNOWN:
      throw unknownStructOrUnion(type);
    default:
      throw unrecognizedStructOrUnion(type.getStructOrUnionType());
  }
}
 
Example #4
Source File: TProtocolReadFieldBeginInterceptor.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
@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);
            }
        }
    }
}
 
Example #5
Source File: BaseArray.java    From nettythrift with Apache License 2.0 6 votes vote down vote up
/**
 * Struct use only.
 * 
 * @return
 */
public TField newField() {
	if (createIndex < obj.length()) {
		Map.Entry<TFieldIdEnum, FieldMetaData> entry = null;
		if (addStep == 2) {
			String fieldName = obj.getString(createIndex << 1);
			entry = elementMetas.get(fieldName);
			createIndex++;
		} else {
			int i = createIndex;
			Object o;
			while (i < obj.length() && ((o = obj.get(i)) == null || o == JSONObject.NULL)) {
				currentIndex();// array index: +1
				i++;
			}
			entry = elementMetaArr[i];
			createIndex = i + 1;
		}
		FieldMetaData fm = entry.getValue();
		prevFieldMetaData = fm;
		return new TField(fm.fieldName, fm.valueMetaData.type, entry.getKey().getThriftFieldId());
	}
	return null;
}
 
Example #6
Source File: AbstractThriftBase.java    From ikasoa with MIT License 6 votes vote down vote up
/**
 * 读取操作
 */
@Override
public void read(TProtocol iprot) throws TException {
	if (!StringUtil.equals("org.apache.thrift.scheme.StandardScheme", iprot.getScheme().getName()))
		throw new TApplicationException("Service scheme must be 'org.apache.thrift.scheme.StandardScheme' !");
	TField schemeField;
	iprot.readStructBegin();
	while (true) {
		schemeField = iprot.readFieldBegin();
		if (ObjectUtil.same(schemeField.type, TType.STOP))
			break;
		if (ObjectUtil.same(schemeField.type, TType.STRING))
			str = iprot.readString();
		else
			throw new TApplicationException("field type must be 'String' !");
		iprot.readFieldEnd();
	}
	iprot.readStructEnd();
}
 
Example #7
Source File: ParquetWriteProtocol.java    From parquet-mr with Apache License 2.0 6 votes vote down vote up
@Override
public void writeFieldBegin(TField field) throws TException {
  if (field.type == TType.STOP) {
    return;
  }
  try {
    currentType = thriftFieldIdToParquetField[field.id];
    if (currentType == null) {
      throw new ParquetEncodingException("field " + field.id + " was not found in " + thriftType + " and " + schema.getType());
    }
    final int index = currentType.getIndex();
    recordConsumer.startField(currentType.getName(), index);
    currentProtocol = children[index];
  } catch (ArrayIndexOutOfBoundsException e) {
    throw new ParquetEncodingException("field " + field.id + " was not found in " + thriftType + " and " + schema.getType());
  }
}
 
Example #8
Source File: TestProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #9
Source File: TTextProtocol.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
public TField readFieldBegin() throws TException {
    if (!getCurrentContext().hasMoreChildren()) {
        return new TField("", UNUSED_TYPE, (short) 0);
    }

    getCurrentContext().read();

    final JsonNode jsonName = getCurrentContext().getCurrentChild();

    if (!jsonName.isTextual()) {
        throw new RuntimeException("Expected String for a field name");
    }

    final String fieldName = jsonName.asText();
    currentFieldClass.push(getCurrentContext().getClassByFieldName(fieldName));

    return getCurrentContext().getTFieldByName(fieldName);
}
 
Example #10
Source File: ProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #11
Source File: ThriftRecordConverter.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
public GroupFieldhandler(GroupConverter delegate, final ThriftField field, List<TProtocol> events) {
  this.delegate = delegate;
  this.events = events;
  this.readFieldBegin = new ParquetProtocol("readFieldBegin()") {
    @Override
    public TField readFieldBegin() throws TException {
      return new TField(field.getName(), field.getType().getType().getThriftType(), field.getFieldId());
    }
  };
}
 
Example #12
Source File: ThriftRecordConverter.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
public PrimitiveFieldHandler(PrimitiveConverter delegate, final ThriftField field, List<TProtocol> events) {
  this.delegate = delegate;
  this.events = events;
  final byte thriftType =
      field.getType().getType() == ThriftTypeID.ENUM ?
          ThriftTypeID.I32.getThriftType() : // enums are serialized as I32
          field.getType().getType().getThriftType();
  this.readFieldBegin = new ParquetProtocol("readFieldBegin()") {
    @Override
    public TField readFieldBegin() throws TException {
      return new TField(field.getName(), thriftType, field.getFieldId());
    }
  };
}
 
Example #13
Source File: EventBasedThriftReader.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
/**
 * reads the content of a struct (fields) from the underlying protocol and passes the events to c
 * @param c the field consumer
 * @throws TException if any thrift related error occurs during the reading
 */
public void readStructContent(FieldConsumer c) throws TException {
  TField field;
  while (true) {
    field = protocol.readFieldBegin();
    if (field.type == TType.STOP) {
      break;
    }
    c.consumeField(protocol, this, field.id, field.type);
  }
}
 
Example #14
Source File: TestProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 5 votes vote down vote up
/**
 * 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());
}
 
Example #15
Source File: EventBasedThriftReader.java    From parquet-format with Apache License 2.0 5 votes vote down vote up
/**
 * reads the content of a struct (fields) from the underlying protocol and passes the events to c
 * @param c the field consumer
 * @throws TException
 */
public void readStructContent(FieldConsumer c) throws TException {
  TField field;
  while (true) {
    field = protocol.readFieldBegin();
    if (field.type == TType.STOP) {
      break;
    }
    c.consumeField(protocol, this, field.id, field.type);
  }
}
 
Example #16
Source File: BinFileRead.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TException {
    TTransport trans = new TSimpleFileTransport("data", true, false);
    TProtocol proto = new TBinaryProtocol(trans);		

    Trade trade_read = new Trade();
    TField field = new TField();

    TStruct struct_obj = proto.readStructBegin();		
    while(true) {						
        field = proto.readFieldBegin();			
        if (field.id == TType.STOP) {				
            break;
        }
        switch(field.id) {					
        case 1:
            trade_read.symbol = proto.readString();	
            break;
        case 2:
            trade_read.price = proto.readDouble();
            break;
        case 3:
            trade_read.size = proto.readI32();
            break;
        default:
            TProtocolUtil.skip(proto,field.type);	
            break;
        }
        proto.readFieldEnd();				
    }
    proto.readStructEnd();				

    System.out.println("Trade: " + trade_read.symbol + " " +
                       trade_read.size + " @ " + trade_read.price);
}
 
Example #17
Source File: CompFileWrite.java    From ThriftBook with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws TException {
    TTransport trans = new TSimpleFileTransport("data.comp", false, true);
    TProtocol proto = new TCompactProtocol(trans);			

    Trade trade = new Trade();
    trade.symbol = "F";
    trade.price = 13.10;
    trade.size = 2500;

    proto.writeStructBegin(new TStruct());

    proto.writeFieldBegin(new TField("symbol", 
                                     TType.STRING, 
                                     (short) 1));
    proto.writeString(trade.symbol);
    proto.writeFieldEnd();

    proto.writeFieldBegin(new TField("price", 
                                     TType.DOUBLE, 
                                     (short) 2));
    proto.writeDouble(trade.price);
    proto.writeFieldEnd();

    proto.writeFieldBegin(new TField("size", 
                                     TType.I32, 
                                     (short) 3));
    proto.writeI32(trade.size);
    proto.writeFieldEnd();

    proto.writeFieldStop();
    proto.writeStructEnd();
    
    System.out.println("Wrote trade to file");
}
 
Example #18
Source File: TTextProtocol.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public void writeFieldBegin(TField field) throws TException {
    try {
        getCurrentWriter().writeFieldName(field.name);
    } catch (IOException ex) {
        throw new TException(ex);
    }

    currentFieldClass.push(getCurrentContext().getClassByFieldName(field.name));
}
 
Example #19
Source File: TReplaceListProtocol.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
@Override
public void writeFieldBegin(TField field) throws TException {
    if (!writeFieldBegin) {
        protocol.writeFieldBegin(field);
        if (replaceFields.containsKey(field.name)) {
            writeFieldBegin = true;
            currentField = field;
        }
    }
}
 
Example #20
Source File: StructContext.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
protected TField getTFieldByName(String name) throws TException {
    if (!fieldNameMap.containsKey(name)) {
        throw new TException("Unknown field: " + name);
    }
    return fieldNameMap.get(name);
}
 
Example #21
Source File: TApplicationExceptions.java    From armeria with Apache License 2.0 5 votes vote down vote up
/**
 * Reads a {@link TApplicationException} from the specified {@link TProtocol}.
 *
 * <p>Note: This has been copied from {@link TApplicationException#read(TProtocol)} due to API differences
 * between libthrift 0.9.x and 0.10.x.
 */
public static TApplicationException read(TProtocol iprot) throws TException {
    TField field;
    iprot.readStructBegin();

    String message = null;
    int type = TApplicationException.UNKNOWN;

    while (true) {
        field = iprot.readFieldBegin();
        if (field.type == TType.STOP) {
            break;
        }
        switch (field.id) {
            case 1:
                if (field.type == TType.STRING) {
                    message = iprot.readString();
                } else {
                    TProtocolUtil.skip(iprot, field.type);
                }
                break;
            case 2:
                if (field.type == TType.I32) {
                    type = iprot.readI32();
                } else {
                    TProtocolUtil.skip(iprot, field.type);
                }
                break;
            default:
                TProtocolUtil.skip(iprot, field.type);
                break;
        }
        iprot.readFieldEnd();
    }
    iprot.readStructEnd();

    return new TApplicationException(type, message);
}
 
Example #22
Source File: InternalScribeCodec.java    From zipkin-reporter-java with Apache License 2.0 5 votes vote down vote up
static boolean parseResponse(TBinaryProtocol iprot) throws TException {
  Boolean result = null;
  iprot.readStructBegin();
  TField schemeField;
  while ((schemeField = iprot.readFieldBegin()).type != TType.STOP) {
    if (schemeField.id == 0 /* SUCCESS */ && schemeField.type == TType.I32) {
      result = iprot.readI32() == 0;
    } else {
      TProtocolUtil.skip(iprot, schemeField.type);
    }
  }
  iprot.readStructEnd();
  if (result != null) return result;
  throw new TApplicationException(MISSING_RESULT, "Log failed: unknown result");
}
 
Example #23
Source File: AbstractThriftBase.java    From ikasoa with MIT License 5 votes vote down vote up
/**
 * 写入操作
 */
@Override
public void write(TProtocol oprot) throws TException {
	if (!StringUtil.equals("org.apache.thrift.scheme.StandardScheme", oprot.getScheme().getName()))
		throw new TApplicationException("Service scheme must be 'org.apache.thrift.scheme.StandardScheme' !");
	oprot.writeStructBegin(getTStruct());
	if (ObjectUtil.isNotNull(str)) {
		oprot.writeFieldBegin(new TField("value", TType.STRING, (short) 0));
		oprot.writeString(str);
		oprot.writeFieldEnd();
	}
	oprot.writeFieldStop();
	oprot.writeStructEnd();
}
 
Example #24
Source File: TProtocolReadFieldBeginInterceptor.java    From pinpoint with Apache License 2.0 5 votes vote down vote up
private void handleClientRequest(TField field, ThriftClientCallContext clientCallContext) {
    ThriftHeader traceHeaderKey = ThriftHeader.findThriftHeaderKeyById(field.id);
    // check if field is pinpoint header field
    if (traceHeaderKey == null || field.type != traceHeaderKey.getType()) {
        clientCallContext.setTraceHeaderToBeRead(NONE);
    } else {
        clientCallContext.setTraceHeaderToBeRead(traceHeaderKey);
    }
}
 
Example #25
Source File: TestProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
@Override
public void handleFieldIgnored(TField field) {
  fieldIgnoredCount++;
}
 
Example #26
Source File: ParquetReadProtocol.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
public TField readFieldBegin() throws TException {
  LOG.debug("readFieldBegin()");
  return next().readFieldBegin();
}
 
Example #27
Source File: TReplaceListProtocol.java    From pinpoint with Apache License 2.0 4 votes vote down vote up
@Override
public TField readFieldBegin() throws TException {
    throw new TException("unsupported operation");
}
 
Example #28
Source File: ParquetProtocol.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
@Override
public TField readFieldBegin() throws TException {
  throw exception();
}
 
Example #29
Source File: ParquetProtocol.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
@Override
public void writeFieldBegin(TField field) throws TException {
  throw exception();
}
 
Example #30
Source File: BufferedProtocolReadToWrite.java    From parquet-mr with Apache License 2.0 4 votes vote down vote up
@Override
public TField readFieldBegin() throws TException {
  return null;
}