Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE 621: Configuration to disable schema auto creation by the client #622

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/schema-registry.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Important settings from the above are
config.put(KafkaAvroSerializer.STORE_SCHEMA_VERSION_ID_IN_HEADER, "true");
config.put(KafkaAvroSerde.KEY_SCHEMA_VERSION_ID_HEADER_NAME, "key.schema.version.id"); // optional
config.put(KafkaAvroSerde.VALUE_SCHEMA_VERSION_ID_HEADER_NAME, "value.schema.version.id"); // optional
config.put("schema.auto.create", "true"); // optional

**store.schema.version.id.in.header**
By default, this is set to 'false' to maintain backward compatibility. User needs to enable it to save the schema version
Expand All @@ -159,6 +160,10 @@ key.serializer is set to 'KafkaAvroSerializer'.
Configurable header name to save the Record Value schema version information. This configuration is applicable only when
value.serializer is set to 'KafkaAvroSerializer'.

**schema.auto.create**
By default, this is set to 'true' to maintain backward compatibility. User needs to disable it so the client won't try to
create schemas if it does not exists.

Run consumer to retrieve schema and deserialze the messages
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@
import com.hortonworks.registries.schemaregistry.SchemaIdVersion;
import com.hortonworks.registries.schemaregistry.SchemaMetadata;
import com.hortonworks.registries.schemaregistry.SchemaVersion;
import com.hortonworks.registries.schemaregistry.SchemaVersionInfo;
import com.hortonworks.registries.schemaregistry.client.ISchemaRegistryClient;
import com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException;
import com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException;
import com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException;
import com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException;
import com.hortonworks.registries.schemaregistry.exceptions.RegistryException;
import org.apache.commons.lang3.BooleanUtils;

import java.util.Map;

/**
* This class implements {@link SnapshotSerializer} and internally creates schema registry client to connect to the
Expand All @@ -36,13 +40,26 @@
*/
public abstract class AbstractSnapshotSerializer<I, O> extends AbstractSerDes implements SnapshotSerializer<I, O, SchemaMetadata> {

private static final String AUTO_CREATE_SCHEMA = "schema.auto.create";

private boolean autoCreateSchema = true;

public AbstractSnapshotSerializer() {
}

public AbstractSnapshotSerializer(ISchemaRegistryClient schemaRegistryClient) {
super(schemaRegistryClient);
}

@SuppressWarnings("unchecked")
@Override
protected void doInit(Map<String, ?> config) {
super.doInit(config);

this.autoCreateSchema = BooleanUtils.toBoolean(((Map<String, Object>) config)
.getOrDefault(AUTO_CREATE_SCHEMA, "true").toString());
}

@Override
public final O serialize(I input, SchemaMetadata schemaMetadata) throws SerDesException {
ensureInitialized();
Expand All @@ -51,8 +68,15 @@ public final O serialize(I input, SchemaMetadata schemaMetadata) throws SerDesEx
String schema = getSchemaText(input);

// register that schema and get the version
SchemaIdVersion schemaIdVersion;
try {
SchemaIdVersion schemaIdVersion = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema, "Schema registered by serializer:" + this.getClass()));
if(autoCreateSchema){
schemaIdVersion = schemaRegistryClient.addSchemaVersion(schemaMetadata,
new SchemaVersion(schema, "Schema registered by serializer:" + this.getClass()));
} else {
SchemaVersionInfo info = schemaRegistryClient.getLatestSchemaVersionInfo(schemaMetadata.getName());
schemaIdVersion = new SchemaIdVersion(info.getSchemaMetadataId(), info.getVersion());
}
// write the version and given object to the output
return doSerialize(input, schemaIdVersion);
} catch (SchemaNotFoundException | IncompatibleSchemaException | InvalidSchemaException | SchemaBranchNotFoundException e) {
Expand Down