List of usage examples for org.apache.thrift.protocol TType STRING
byte STRING
To view the source code for org.apache.thrift.protocol TType STRING.
Click Source Link
From source file:cereal.impl.ThriftStructMapping.java
License:Apache License
@Override public List<Field> getFields(E obj) { checkNotNull(obj, "The struct was null"); List<Field> fields; try {//www . jav a2s .c o m @SuppressWarnings("rawtypes") Class<? extends TBase> tbaseClz = obj.getClass(); if (null == getFieldValue) { synchronized (this) { if (null == getFieldValue) { Class<?> fieldsClz = Class.forName(obj.getClass().getName() + "$_Fields"); getFieldValue = tbaseClz.getMethod("getFieldValue", fieldsClz); isSet = tbaseClz.getMethod("isSet", fieldsClz); } } } Map<? extends TFieldIdEnum, FieldMetaData> thriftFields = FieldMetaData.getStructMetaDataMap(tbaseClz); fields = new ArrayList<>(); for (Entry<? extends TFieldIdEnum, FieldMetaData> entry : thriftFields.entrySet()) { TFieldIdEnum field = entry.getKey(); FieldMetaData fMetaData = entry.getValue(); if ((boolean) isSet.invoke(obj, field)) { Object value = getFieldValue.invoke(obj, field); FieldValueMetaData fvMetaData = fMetaData.valueMetaData; switch (fvMetaData.type) { case TType.BOOL: Boolean booleanVal = (Boolean) value; fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData), getVisibility(fMetaData), value(booleanVal.toString()))); break; case TType.BYTE: Byte byteVal = (Byte) value; fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData), getVisibility(fMetaData), value(byteVal.toString()))); break; case TType.DOUBLE: Double dblVal = (Double) value; fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData), getVisibility(fMetaData), value(dblVal.toString()))); break; case TType.I16: Short shortVal = (Short) value; fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData), getVisibility(fMetaData), value(shortVal.toString()))); break; case TType.I32: Integer intVal = (Integer) value; fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData), getVisibility(fMetaData), value(intVal.toString()))); break; case TType.I64: Long longVal = (Long) value; fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData), getVisibility(fMetaData), value(longVal.toString()))); break; case TType.STRING: byte[] bytes; if (fvMetaData.isBinary()) { bytes = (byte[]) value; } else { String strVal = (String) value; bytes = strVal.getBytes(UTF_8); } fields.add(new FieldImpl(text(fMetaData.fieldName), getGrouping(fMetaData), getVisibility(fMetaData), new Value(bytes))); break; default: log.warn("Ignoring field: {}", field.getFieldName()); break; } } } } catch (Exception e) { throw new RuntimeException(e); } return fields; }
From source file:cereal.impl.ThriftStructMapping.java
License:Apache License
@Override public void update(Iterable<Entry<Key, Value>> iter, InstanceOrBuilder<E> instOrBuilder) { checkNotNull(iter, "Iterator is null"); checkNotNull(instOrBuilder, "InstOrBuilder is null"); checkArgument(Type.INSTANCE == instOrBuilder.getType(), "Expected INSTANCE in InstanceOrBuilder"); try {//from w w w .j a v a 2s . c om @SuppressWarnings("rawtypes") Class<? extends TBase> tbaseClz = instOrBuilder.getWrappedClass(); if (null == setFieldValue) { synchronized (this) { if (null == setFieldValue) { Class<?> fieldsClz = Class.forName(instOrBuilder.getWrappedClass().getName() + "$_Fields"); setFieldValue = tbaseClz.getMethod("setFieldValue", fieldsClz, Object.class); } } } for (Entry<Key, Value> entry : iter) { String fieldName = entry.getKey().getColumnQualifier().toString(); Map<? extends TFieldIdEnum, FieldMetaData> thriftFields = FieldMetaData .getStructMetaDataMap(tbaseClz); for (Entry<? extends TFieldIdEnum, FieldMetaData> fieldEntry : thriftFields.entrySet()) { TFieldIdEnum fieldId = fieldEntry.getKey(); if (fieldName.equals(fieldId.getFieldName())) { FieldValueMetaData fvMetaData = fieldEntry.getValue().valueMetaData; Value v = entry.getValue(); Object obj = instOrBuilder.get(); switch (fvMetaData.type) { case TType.BOOL: Boolean booleanVal = Boolean.parseBoolean(v.toString()); setFieldValue.invoke(obj, fieldId, booleanVal); break; case TType.BYTE: Byte byteVal = Byte.parseByte(v.toString()); setFieldValue.invoke(obj, fieldId, byteVal); break; case TType.DOUBLE: Double dblVal = Double.parseDouble(v.toString()); setFieldValue.invoke(obj, fieldId, dblVal); break; case TType.I16: Short shortVal = Short.parseShort(v.toString()); setFieldValue.invoke(obj, fieldId, shortVal); break; case TType.I32: Integer intVal = Integer.parseInt(v.toString()); setFieldValue.invoke(obj, fieldId, intVal); break; case TType.I64: Long longVal = Long.parseLong(v.toString()); setFieldValue.invoke(obj, fieldId, longVal); break; case TType.STRING: if (fvMetaData.isBinary()) { setFieldValue.invoke(obj, fieldId, ByteBuffer.wrap(v.get())); } else { String strVal = v.toString(); setFieldValue.invoke(obj, fieldId, strVal); } break; default: log.warn("Ignoring field: {}", fieldName); break; } } } } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:com.ambiata.poacher.mr.TDeserializerCopy.java
License:Apache License
/** * Deserialize only a string field (addressed by recursively using field id) * from a byte record.//from w w w . j a va 2s . co m * @param bytes The serialized object to read from * @param fieldIdPathFirst First of the FieldId's that define a path to a string field * @param fieldIdPathRest The rest FieldId's that define a path to a string field * @throws TException */ public String partialDeserializeString(byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum... fieldIdPathRest) throws TException { return (String) partialDeserializeField(TType.STRING, bytes, fieldIdPathFirst, fieldIdPathRest); }
From source file:com.ambiata.poacher.mr.TDeserializerCopy.java
License:Apache License
private Object partialDeserializeField(byte ttype, byte[] bytes, TFieldIdEnum fieldIdPathFirst, TFieldIdEnum... fieldIdPathRest) throws TException { try {/* w ww . j a v a 2 s . c o m*/ TField field = locateField(bytes, fieldIdPathFirst, fieldIdPathRest); if (field != null) { // if this point is reached, iprot will be positioned at the start of the field. switch (ttype) { case TType.BOOL: if (field.type == TType.BOOL) { return protocol_.readBool(); } break; case TType.BYTE: if (field.type == TType.BYTE) { return protocol_.readByte(); } break; case TType.DOUBLE: if (field.type == TType.DOUBLE) { return protocol_.readDouble(); } break; case TType.I16: if (field.type == TType.I16) { return protocol_.readI16(); } break; case TType.I32: if (field.type == TType.I32) { return protocol_.readI32(); } break; case TType.I64: if (field.type == TType.I64) { return protocol_.readI64(); } break; case TType.STRING: if (field.type == TType.STRING) { return protocol_.readString(); } break; case 100: // hack to differentiate between string and binary if (field.type == TType.STRING) { return protocol_.readBinary(); } break; } } return null; } catch (Exception e) { throw new TException(e); } finally { trans_.clear(); protocol_.reset(); } }
From source file:com.baidu.oped.apm.plugin.thrift.ThriftRequestProperty.java
License:Apache License
public void setTraceHeader(ThriftHeader headerKey, Object value) throws TException { byte headerType = headerKey.getType(); if (headerType == TType.STRING) { // skipped Strings are read as byte buffer. // see org.apache.thrift.protocol.TProtocolUtil.skip(TProtocol, byte, int) this.thriftHeaders.put(headerKey, byteBufferToString((ByteBuffer) value)); } else if (headerType == TType.I64) { this.thriftHeaders.put(headerKey, (Long) value); } else if (headerType == TType.I16) { this.thriftHeaders.put(headerKey, (Short) value); } else if (headerType == TType.BOOL) { this.thriftHeaders.put(headerKey, (Boolean) value); } else {// w ww. j av a2 s . com throw new TProtocolException("Invalid apm header type - " + headerType); } }
From source file:com.baidu.oped.apm.plugin.thrift.ThriftRequestProperty.java
License:Apache License
public void writeTraceHeader(ThriftHeader headerKey, TProtocol oprot) throws TException { Object headerValue = this.thriftHeaders.get(headerKey); if (headerValue == null) { return;/*from ww w . ja v a 2 s. c o m*/ } 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 apm header type - " + headerType); } } finally { oprot.writeFieldEnd(); } }
From source file:com.bigdata.dastor.thrift.AuthenticationException.java
License:Apache License
public void read(TProtocol iprot) throws TException { TField field;// w ww . j a v a2 s .c o m iprot.readStructBegin(); while (true) { field = iprot.readFieldBegin(); if (field.type == TType.STOP) { break; } switch (field.id) { case 1: // WHY if (field.type == TType.STRING) { this.why = iprot.readString(); } else { TProtocolUtil.skip(iprot, field.type); } break; default: TProtocolUtil.skip(iprot, field.type); } iprot.readFieldEnd(); } iprot.readStructEnd(); // check for required fields of primitive type, which can't be checked in the validate method validate(); }
From source file:com.bigdata.dastor.thrift.AuthenticationRequest.java
License:Apache License
public void write(TProtocol oprot) throws TException { validate();/*from w ww. j av a 2 s . co m*/ oprot.writeStructBegin(STRUCT_DESC); if (this.credentials != null) { oprot.writeFieldBegin(CREDENTIALS_FIELD_DESC); { oprot.writeMapBegin(new TMap(TType.STRING, TType.STRING, this.credentials.size())); for (Map.Entry<String, String> _iter20 : this.credentials.entrySet()) { oprot.writeString(_iter20.getKey()); oprot.writeString(_iter20.getValue()); } oprot.writeMapEnd(); } oprot.writeFieldEnd(); } oprot.writeFieldStop(); oprot.writeStructEnd(); }
From source file:com.bigdata.dastor.thrift.Column.java
License:Apache License
public void read(TProtocol iprot) throws TException { TField field;//from w w w. j a va2s. c om iprot.readStructBegin(); while (true) { field = iprot.readFieldBegin(); if (field.type == TType.STOP) { break; } switch (field.id) { case 1: // NAME if (field.type == TType.STRING) { this.name = iprot.readBinary(); } else { TProtocolUtil.skip(iprot, field.type); } break; case 2: // VALUE if (field.type == TType.STRING) { this.value = iprot.readBinary(); } else { TProtocolUtil.skip(iprot, field.type); } break; case 3: // TIMESTAMP if (field.type == TType.I64) { this.timestamp = iprot.readI64(); setTimestampIsSet(true); } else { TProtocolUtil.skip(iprot, field.type); } break; default: TProtocolUtil.skip(iprot, field.type); } iprot.readFieldEnd(); } iprot.readStructEnd(); // check for required fields of primitive type, which can't be checked in the validate method if (!isSetTimestamp()) { throw new TProtocolException( "Required field 'timestamp' was not found in serialized data! Struct: " + toString()); } validate(); }
From source file:com.bigdata.dastor.thrift.ColumnParent.java
License:Apache License
public void read(TProtocol iprot) throws TException { TField field;/*from w w w .ja v a2s .c o m*/ iprot.readStructBegin(); while (true) { field = iprot.readFieldBegin(); if (field.type == TType.STOP) { break; } switch (field.id) { case 3: // COLUMN_FAMILY if (field.type == TType.STRING) { this.column_family = iprot.readString(); } else { TProtocolUtil.skip(iprot, field.type); } break; case 4: // SUPER_COLUMN if (field.type == TType.STRING) { this.super_column = iprot.readBinary(); } else { TProtocolUtil.skip(iprot, field.type); } break; default: TProtocolUtil.skip(iprot, field.type); } iprot.readFieldEnd(); } iprot.readStructEnd(); // check for required fields of primitive type, which can't be checked in the validate method validate(); }