@Override public Schema readSchema(InputStream in) throws IOException { DataInputStream dataInputStream = new DataInputStream(in); if (dataInputStream.readByte() != 0) { throw new IOException("Unknown data format. Magic number does not match"); } else { int schemaId = dataInputStream.readInt(); try { return schemaRegistryClient.getById(schemaId); } catch (RestClientException e) { throw new IOException(format("Could not find schema with id %s in registry", schemaId), e); } } }
@Test public void shouldMatchAvroFormatter() throws Exception { // Given: final Schema schema = parseAvroSchema( "{\n" + " \"fields\": [\n" + " { \"name\": \"str1\", \"type\": \"string\" }\n" + " ],\n" + " \"name\": \"myrecord\",\n" + " \"type\": \"record\"\n" + "}"); final GenericData.Record avroRecord = new GenericData.Record(schema); avroRecord.put("str1", "My first string"); expect(schemaRegistryClient.register(anyString(), anyObject())).andReturn(1); expect(schemaRegistryClient.getById(anyInt())).andReturn(schema).times(2); replay(schemaRegistryClient); final byte[] avroData = serializeAvroRecord(avroRecord); // When: final Result result = getFormatter(avroData); // Then: assertThat(result.format, is(Format.AVRO)); assertThat(result.formatted, endsWith(", key, {\"str1\": \"My first string\"}\n")); }
Schema writeSchema; try { writeSchema = client.getById(id); } catch (IOException e) { throw new StreamRuntimeException("Cannot retrieve write schema id: " + id + " on " + messageClass, e);
Schema schema = schemaRegistry.getById(id); if (schema == null) throw new IllegalStateException("Unknown schema id: " + id);