Search in sources :

Example 1 with SchemaReference

use of org.springframework.cloud.stream.schema.SchemaReference in project spring-cloud-stream by spring-cloud.

the class AvroMessageConverterSerializationTests method extractSchemaReference.

private SchemaReference extractSchemaReference(MimeType mimeType) {
    SchemaReference schemaReference = null;
    Matcher schemaMatcher = this.versionedSchema.matcher(mimeType.toString());
    if (schemaMatcher.find()) {
        String subject = schemaMatcher.group(1);
        Integer version = Integer.parseInt(schemaMatcher.group(2));
        schemaReference = new SchemaReference(subject, version, AvroSchemaRegistryClientMessageConverter.AVRO_FORMAT);
    }
    return schemaReference;
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) Matcher(java.util.regex.Matcher)

Example 2 with SchemaReference

use of org.springframework.cloud.stream.schema.SchemaReference in project spring-cloud-stream by spring-cloud.

the class AvroMessageConverterSerializationTests method sourceWriteSameVersion.

@Test
public void sourceWriteSameVersion() throws Exception {
    User specificRecord = new User();
    specificRecord.setName("joe");
    Schema v1 = new Schema.Parser().parse(AvroMessageConverterSerializationTests.class.getClassLoader().getResourceAsStream("schemas/user.avsc"));
    GenericRecord genericRecord = new GenericData.Record(v1);
    genericRecord.put("name", "joe");
    SchemaRegistryClient client = new DefaultSchemaRegistryClient();
    AvroSchemaRegistryClientMessageConverter converter = new AvroSchemaRegistryClientMessageConverter(client, new NoOpCacheManager());
    converter.setSubjectNamingStrategy(new DefaultSubjectNamingStrategy());
    converter.setDynamicSchemaGenerationEnabled(false);
    converter.afterPropertiesSet();
    Message specificMessage = converter.toMessage(specificRecord, new MutableMessageHeaders(Collections.<String, Object>emptyMap()), MimeTypeUtils.parseMimeType("application/*+avro"));
    SchemaReference specificRef = extractSchemaReference(MimeTypeUtils.parseMimeType(specificMessage.getHeaders().get("contentType").toString()));
    Message genericMessage = converter.toMessage(genericRecord, new MutableMessageHeaders(Collections.<String, Object>emptyMap()), MimeTypeUtils.parseMimeType("application/*+avro"));
    SchemaReference genericRef = extractSchemaReference(MimeTypeUtils.parseMimeType(genericMessage.getHeaders().get("contentType").toString()));
    Assert.assertEquals(genericRef, specificRef);
    Assert.assertEquals(1, genericRef.getVersion());
}
Also used : User(example.avro.User) Message(org.springframework.messaging.Message) Schema(org.apache.avro.Schema) NoOpCacheManager(org.springframework.cache.support.NoOpCacheManager) SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) DefaultSubjectNamingStrategy(org.springframework.cloud.stream.schema.avro.DefaultSubjectNamingStrategy) AvroSchemaRegistryClientMessageConverter(org.springframework.cloud.stream.schema.avro.AvroSchemaRegistryClientMessageConverter) GenericRecord(org.apache.avro.generic.GenericRecord) GenericRecord(org.apache.avro.generic.GenericRecord) MutableMessageHeaders(org.springframework.integration.support.MutableMessageHeaders) DefaultSchemaRegistryClient(org.springframework.cloud.stream.schema.client.DefaultSchemaRegistryClient) DefaultSchemaRegistryClient(org.springframework.cloud.stream.schema.client.DefaultSchemaRegistryClient) SchemaRegistryClient(org.springframework.cloud.stream.schema.client.SchemaRegistryClient) Test(org.junit.Test)

Example 3 with SchemaReference

use of org.springframework.cloud.stream.schema.SchemaReference in project spring-cloud-stream by spring-cloud.

the class AvroSchemaRegistryClientMessageConverter method resolveSchemaForWriting.

@Override
protected Schema resolveSchemaForWriting(Object payload, MessageHeaders headers, MimeType hintedContentType) {
    Schema schema;
    schema = extractSchemaForWriting(payload);
    ParsedSchema parsedSchema = this.cacheManager.getCache(REFERENCE_CACHE_NAME).get(schema, ParsedSchema.class);
    if (parsedSchema == null) {
        parsedSchema = new ParsedSchema(schema);
        this.cacheManager.getCache(REFERENCE_CACHE_NAME).putIfAbsent(schema, parsedSchema);
    }
    if (parsedSchema.getRegistration() == null) {
        SchemaRegistrationResponse response = this.schemaRegistryClient.register(toSubject(schema), AVRO_FORMAT, parsedSchema.getRepresentation());
        parsedSchema.setRegistration(response);
    }
    SchemaReference schemaReference = parsedSchema.getRegistration().getSchemaReference();
    DirectFieldAccessor dfa = new DirectFieldAccessor(headers);
    @SuppressWarnings("unchecked") Map<String, Object> _headers = (Map<String, Object>) dfa.getPropertyValue("headers");
    _headers.put(MessageHeaders.CONTENT_TYPE, "application/" + this.prefix + "." + schemaReference.getSubject() + ".v" + schemaReference.getVersion() + "+" + AVRO_FORMAT);
    return schema;
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) Schema(org.apache.avro.Schema) ParsedSchema(org.springframework.cloud.stream.schema.ParsedSchema) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) ParsedSchema(org.springframework.cloud.stream.schema.ParsedSchema) Map(java.util.Map)

Example 4 with SchemaReference

use of org.springframework.cloud.stream.schema.SchemaReference in project spring-cloud-stream by spring-cloud.

the class AvroSchemaRegistryClientMessageConverter method resolveWriterSchemaForDeserialization.

@Override
protected Schema resolveWriterSchemaForDeserialization(MimeType mimeType) {
    if (this.readerSchema == null) {
        Schema schema = null;
        ParsedSchema parsedSchema = null;
        SchemaReference schemaReference = extractSchemaReference(mimeType);
        if (schemaReference != null) {
            parsedSchema = cacheManager.getCache(REFERENCE_CACHE_NAME).get(schemaReference, ParsedSchema.class);
            if (parsedSchema == null) {
                String schemaContent = this.schemaRegistryClient.fetch(schemaReference);
                schema = new Schema.Parser().parse(schemaContent);
                parsedSchema = new ParsedSchema(schema);
                cacheManager.getCache(REFERENCE_CACHE_NAME).putIfAbsent(schemaReference, parsedSchema);
            }
        }
        return parsedSchema.getSchema();
    } else {
        return this.readerSchema;
    }
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) Schema(org.apache.avro.Schema) ParsedSchema(org.springframework.cloud.stream.schema.ParsedSchema) ParsedSchema(org.springframework.cloud.stream.schema.ParsedSchema)

Example 5 with SchemaReference

use of org.springframework.cloud.stream.schema.SchemaReference in project spring-cloud-stream by spring-cloud.

the class DefaultSchemaRegistryClient method register.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public SchemaRegistrationResponse register(String subject, String format, String schema) {
    Map<String, String> requestBody = new HashMap<>();
    requestBody.put("subject", subject);
    requestBody.put("format", format);
    requestBody.put("definition", schema);
    ResponseEntity<Map> responseEntity = this.restTemplate.postForEntity(this.endpoint, requestBody, Map.class);
    if (responseEntity.getStatusCode().is2xxSuccessful()) {
        SchemaRegistrationResponse registrationResponse = new SchemaRegistrationResponse();
        Map<String, Object> responseBody = (Map<String, Object>) responseEntity.getBody();
        registrationResponse.setId((Integer) responseBody.get("id"));
        registrationResponse.setSchemaReference(new SchemaReference(subject, (Integer) responseBody.get("version"), responseBody.get("format").toString()));
        return registrationResponse;
    }
    throw new RuntimeException("Failed to register schema: " + responseEntity.toString());
}
Also used : SchemaReference(org.springframework.cloud.stream.schema.SchemaReference) HashMap(java.util.HashMap) SchemaRegistrationResponse(org.springframework.cloud.stream.schema.SchemaRegistrationResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

SchemaReference (org.springframework.cloud.stream.schema.SchemaReference)10 Map (java.util.Map)4 SchemaRegistrationResponse (org.springframework.cloud.stream.schema.SchemaRegistrationResponse)4 Schema (org.apache.avro.Schema)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)2 Matcher (java.util.regex.Matcher)2 ParsedSchema (org.springframework.cloud.stream.schema.ParsedSchema)2 ConfluentSchemaRegistryClient (org.springframework.cloud.stream.schema.client.ConfluentSchemaRegistryClient)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 User (example.avro.User)1 TreeMap (java.util.TreeMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 GenericRecord (org.apache.avro.generic.GenericRecord)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 NoOpCacheManager (org.springframework.cache.support.NoOpCacheManager)1 AvroSchemaRegistryClientMessageConverter (org.springframework.cloud.stream.schema.avro.AvroSchemaRegistryClientMessageConverter)1 DefaultSubjectNamingStrategy (org.springframework.cloud.stream.schema.avro.DefaultSubjectNamingStrategy)1 DefaultSchemaRegistryClient (org.springframework.cloud.stream.schema.client.DefaultSchemaRegistryClient)1 SchemaRegistryClient (org.springframework.cloud.stream.schema.client.SchemaRegistryClient)1