@Override public String getFingerprint(Schema schema) { final String subject = schema.getName(); final int guid; try { guid = theClient.register(subject, schema); } catch (IOException | RestClientException e) { throw new RuntimeException(e); } return Integer.toString(guid); }
@Override public Integer register(Schema schema, String name) throws SchemaRegistryException { try { String schemaName = name + this.schemaNameSuffix; return this.schemaRegistryClient.register(schemaName, schema); } catch (IOException | RestClientException e) { throw new SchemaRegistryException(e); } } }
private static void registerSchema(final SchemaRegistryClient schemaRegistryClient) throws IOException, RestClientException { final String ordersAveroSchemaStr = "{" + "\"namespace\": \"kql\"," + " \"name\": \"orders\"," + " \"type\": \"record\"," + " \"fields\": [" + " {\"name\": \"ordertime\", \"type\": \"long\"}," + " {\"name\": \"orderid\", \"type\": \"long\"}," + " {\"name\": \"itemid\", \"type\": \"string\"}," + " {\"name\": \"orderunits\", \"type\": \"double\"}," + " {\"name\": \"arraycol\", \"type\": {\"type\": \"array\", \"items\": \"double\"}}," + " {\"name\": \"mapcol\", \"type\": {\"type\": \"map\", \"values\": \"double\"}}" + " ]" + "}"; final org.apache.avro.Schema.Parser parser = new org.apache.avro.Schema.Parser(); final org.apache.avro.Schema avroSchema = parser.parse(ordersAveroSchemaStr); schemaRegistryClient.register("orders-topic" + KsqlConstants.SCHEMA_REGISTRY_VALUE_SUFFIX, avroSchema); }
@SuppressWarnings("SameParameterValue") private void givenAvroSchemaNotEvolveable(final String topicName) { final org.apache.avro.Schema schema = org.apache.avro.Schema.create(Type.INT); try { schemaRegistryClient.register(topicName + KsqlConstants.SCHEMA_REGISTRY_VALUE_SUFFIX, schema); } catch (final Exception e) { fail(e.getMessage()); } }
@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")); }
@Override public Integer register(Schema schema, String name) throws SchemaRegistryException { try { String schemaName = name + this.schemaNameSuffix; return this.schemaRegistryClient.register(schemaName, schema); } catch (IOException | RestClientException e) { throw new SchemaRegistryException(e); } } }
@Override public String getFingerprint(Schema schema) { final String subject = schema.getName(); final int guid; try { guid = theClient.register(subject, schema); } catch (IOException | RestClientException e) { throw new RuntimeException(e); } return Integer.toString(guid); }
@Override public SchemaReference registerSchema(String subject, String schema) throws SchemaManagerException { SchemaReference schemaReference; try { org.apache.avro.Schema avroSchema = new org.apache.avro.Schema.Parser().parse(schema); int id = schemaRegistryClient.register(subject, avroSchema); int version = schemaRegistryClient.getLatestSchemaMetadata(subject).getVersion(); schemaReference = new SchemaReference(subject, id, version); } catch (IOException | RestClientException | RuntimeException e) { log.error("caught an exception while registering a new schema for subject '{}'", subject); throw new SchemaManagerException(e); } return schemaReference; }
/** * Create an AvroConfluent codec * * @param messageClass the class to encode and decode * @param schemaRegistryUrls a comma separated list of Confluent Schema Registry URL */ public AvroConfluentCodec(Class<T> messageClass, String schemaRegistryUrls) { this.messageClass = messageClass; schema = ReflectData.get().getSchema(messageClass); schemaName = messageClass.getName(); if (schemaRegistryUrls.contains(",")) { client = new CachedSchemaRegistryClient(Arrays.asList(schemaRegistryUrls.split(",")), DEFAULT_IDENTITY_MAP_CAPACITY); } else { client = new CachedSchemaRegistryClient(schemaRegistryUrls, DEFAULT_IDENTITY_MAP_CAPACITY); } try { this.schemaId = client.register(messageClass.getName(), schema); } catch (RestClientException | IOException e) { throw new StreamRuntimeException(e); } this.serializer = new KafkaAvroSerializer(client); this.encoder = new RawMessageEncoder<>(ReflectData.get(), schema); }