@Test public void testConvertGenericToSpecific() { GenericRecord generic = new GenericData.Record(TestRecord.SCHEMA$); generic.put("name", "foo"); generic.put("kind", new GenericData.EnumSymbol(Kind.SCHEMA$, "BAR")); generic.put("hash", new GenericData.Fixed (MD5.SCHEMA$, new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5})); TestRecord specific = (TestRecord)SpecificData.get().deepCopy(TestRecord.SCHEMA$, generic); }
SpecificData.get().deepCopy( field.schema(), interop.get(field.pos())));
@Override public T deepCopy(T source) { return SpecificData.get().deepCopy(getSchema(), source); } }
/** * Extracts a {@link MessageHeader} from the provided message * * @param message the message * @return the message's header * @throws IllegalArgumentException if the message does not contain a header */ public MessageHeader extractHeader(IndexedRecord message) { assert message != null : MESSAGE_SHOULD_NOT_BE_NULL_LABEL; final Schema messageSchema = message.getSchema(); final Schema firstFieldSchema = messageSchema.getFields().get(EXPECTED_HEADER_POSITION).schema(); if (firstFieldSchema.getType() != Schema.Type.RECORD) { throw new IllegalArgumentException(PROVIDED_FIRST_FIELD_RECORD_LABEL + firstFieldSchema.getType()); } if (!firstFieldSchema.getFullName().equals(messageHeaderSchema.getFullName())) { throw new IllegalArgumentException(PROVIDED_FIRST_FIELD_HEADER_LABEL + firstFieldSchema.getFullName()); } final Object firstFieldValue = message.get(EXPECTED_HEADER_POSITION); if (firstFieldValue instanceof MessageHeader) { return MessageHeader.class.cast(firstFieldValue); } else { return (MessageHeader) SpecificData.get().deepCopy(messageHeaderSchema, firstFieldValue); } } }
@SuppressWarnings("unchecked") @Override public T deserialize(String topic, byte[] data) { LOGGER.debug("data to deserialize='{}'", DatatypeConverter.printHexBinary(data)); try { // get the schema Schema schema = targetType.newInstance().getSchema(); Injection<GenericRecord, byte[]> genericRecordInjection = GenericAvroCodecs.toBinary(schema); GenericRecord genericRecord = genericRecordInjection.invert((byte[]) data).get(); T result = (T) SpecificData.get().deepCopy(schema, genericRecord); LOGGER.debug("data='{}'", result); return result; } catch (Exception e) { throw new SerializationException( "Can't deserialize data [" + Arrays.toString(data) + "] from topic [" + topic + "]", e); } } }
@SuppressWarnings("unchecked") public D next() { try { ReadRecordStatus status = readRecord(hostBytes, lastProcessed); bytesRead += status.getBytesRead(); int prefixLen = hostBytesPrefixLen(); FromHostResult < GenericRecord > result = converter.convert( hostBytes, prefixLen, status.getRecordLen() == -1 ? hostBytes.length : status .getRecordLen() + prefixLen); bytesProcessed += lastProcessed = result.getBytesProcessed(); D specific = (D) SpecificData.get().deepCopy( result.getValue().getSchema(), result.getValue()); if (log.isDebugEnabled()) { log.debug("Avro record=" + specific.toString()); } return specific; } catch (IOException e) { throw new AvroRuntimeException(e); } }
public FromHostResult < SpecificRecord > convert(byte[] hostData, int start, int length) { FromHostResult < GenericRecord > result = converter.convert(hostData, start, length); if (result.getValue() == null) { return null; } this.specificRecord = (SpecificRecord) SpecificData.get().deepCopy( result.getValue().getSchema(), result.getValue()); return new FromHostResult < SpecificRecord >( result.getBytesProcessed(), specificRecord); }