/** * Get the schema of a field * * @param record the input record which has the schema id * @param schemaIdLocation a dot separated location string the schema id * @return a schema referenced by the schema id */ protected Schema getFieldSchema(GenericRecord record, String schemaIdLocation) throws Exception { Optional<Object> schemaIdValue = AvroUtils.getFieldValue(record, schemaIdLocation); if (!schemaIdValue.isPresent()) { throw new Exception("Schema id with key " + schemaIdLocation + " not found in the record"); } String schemaKey = String.valueOf(schemaIdValue.get()); return (Schema) registry.getSchemaByKey(schemaKey); }
/** * Get actual schema from registry and deserialize payload using it. */ @Override public Iterable<GenericRecord> convertRecord(String outputSchema, GenericRecord inputRecord, WorkUnitState workUnit) throws DataConversionException { try { String schemaIdField = workUnit.contains(PAYLOAD_SCHEMA_ID_FIELD) ? workUnit.getProp(PAYLOAD_SCHEMA_ID_FIELD) : DEFAULT_PAYLOAD_SCHEMA_ID_FIELD; String payloadField = workUnit.contains(PAYLOAD_FIELD) ? workUnit.getProp(PAYLOAD_FIELD) : DEFAULT_PAYLOAD_FIELD; String schemaKey = String.valueOf(inputRecord.get(schemaIdField)); Schema payloadSchema = (Schema) this.registry.getSchemaByKey(schemaKey); byte[] payload = getPayload(inputRecord, payloadField); GenericRecord outputRecord = deserializePayload(payload, payloadSchema); if (this.fieldRemover.isPresent()) { payloadSchema = this.fieldRemover.get().removeFields(payloadSchema); } return new SingleRecordIterable<>(AvroUtils.convertRecordSchema(outputRecord, payloadSchema)); } catch (IOException | SchemaRegistryException | ExecutionException e) { throw new DataConversionException(e); } }
when(mockRegistry.getSchemaByKey(any())).thenReturn(inputSchema.getField("nestedRecord").schema());
when(mockRegistry.getSchemaByKey(any())).thenReturn(inputSchema.getField("nestedRecord").schema());
@Test public void testRegisterAndGetByKey() throws SchemaRegistryException { Properties properties = new Properties(); properties.setProperty(KafkaSchemaRegistry.KAFKA_SCHEMA_REGISTRY_URL, TEST_URL); SchemaRegistryClient schemaRegistryClient = new MockSchemaRegistryClient(); KafkaSchemaRegistry<Integer, Schema> kafkaSchemaRegistry = new ConfluentKafkaSchemaRegistry(properties, schemaRegistryClient); Schema schema = SchemaBuilder.record(TEST_RECORD_NAME).namespace(TEST_NAMESPACE).fields().name(TEST_FIELD_NAME).type() .stringType().noDefault().endRecord(); Integer id = kafkaSchemaRegistry.register(schema); Assert.assertEquals(schema, kafkaSchemaRegistry.getSchemaByKey(id)); }
/** * Get the schema of a field * * @param record the input record which has the schema id * @param schemaIdLocation a dot separated location string the schema id * @return a schema referenced by the schema id */ protected Schema getFieldSchema(GenericRecord record, String schemaIdLocation) throws Exception { Optional<Object> schemaIdValue = AvroUtils.getFieldValue(record, schemaIdLocation); if (!schemaIdValue.isPresent()) { throw new Exception("Schema id with key " + schemaIdLocation + " not found in the record"); } String schemaKey = String.valueOf(schemaIdValue.get()); return (Schema) registry.getSchemaByKey(schemaKey); }
/** * Get actual schema from registry and deserialize payload using it. */ @Override public Iterable<GenericRecord> convertRecord(String outputSchema, GenericRecord inputRecord, WorkUnitState workUnit) throws DataConversionException { try { String schemaIdField = workUnit.contains(PAYLOAD_SCHEMA_ID_FIELD) ? workUnit.getProp(PAYLOAD_SCHEMA_ID_FIELD) : DEFAULT_PAYLOAD_SCHEMA_ID_FIELD; String payloadField = workUnit.contains(PAYLOAD_FIELD) ? workUnit.getProp(PAYLOAD_FIELD) : DEFAULT_PAYLOAD_FIELD; String schemaKey = String.valueOf(inputRecord.get(schemaIdField)); Schema payloadSchema = (Schema) this.registry.getSchemaByKey(schemaKey); byte[] payload = getPayload(inputRecord, payloadField); GenericRecord outputRecord = deserializePayload(payload, payloadSchema); if (this.fieldRemover.isPresent()) { payloadSchema = this.fieldRemover.get().removeFields(payloadSchema); } return new SingleRecordIterable<>(AvroUtils.convertRecordSchema(outputRecord, payloadSchema)); } catch (IOException | SchemaRegistryException | ExecutionException e) { throw new DataConversionException(e); } }