Java Code Examples for io.confluent.kafka.schemaregistry.client.SchemaMetadata#getSchema()

The following examples show how to use io.confluent.kafka.schemaregistry.client.SchemaMetadata#getSchema() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaRegistryMockTest.java    From fluent-kafka-streams-tests with MIT License 6 votes vote down vote up
@Test
void shouldHaveLatestSchemaVersion() throws IOException, RestClientException {
    final Schema valueSchema1 = createSchema("value_schema");
    final String topic = "test-topic";
    final int id1 = this.schemaRegistry.registerValueSchema(topic, valueSchema1);

    final List<Schema.Field> fields = Collections.singletonList(
            new Schema.Field("f1", Schema.create(Schema.Type.STRING), "", (Object) null));
    final Schema valueSchema2 = Schema.createRecord("value_schema", "no doc", "", false, fields);
    final int id2 = this.schemaRegistry.registerValueSchema(topic, valueSchema2);

    final List<Integer> versions = this.schemaRegistry.getSchemaRegistryClient().getAllVersions(topic + "-value");
    assertThat(versions.size()).isEqualTo(2);

    final SchemaMetadata metadata = this.schemaRegistry.getSchemaRegistryClient().getLatestSchemaMetadata(topic + "-value");
    final int metadataId = metadata.getId();
    assertThat(metadataId).isNotEqualTo(id1);
    assertThat(metadataId).isEqualTo(id2);
    final String schemaString = metadata.getSchema();
    final Schema retrievedSchema = new Schema.Parser().parse(schemaString);
    assertThat(retrievedSchema).isEqualTo(valueSchema2);
}
 
Example 2
Source File: SchemaRegistryMockExtensionTest.java    From fluent-kafka-streams-tests with MIT License 6 votes vote down vote up
@Test
void shouldHaveSchemaVersions() throws IOException, RestClientException {
    final Schema valueSchema = this.createSchema("value_schema");
    final String topic = "test-topic";
    final int id = this.schemaRegistry.registerValueSchema(topic, valueSchema);

    final List<Integer> versions = this.schemaRegistry.getSchemaRegistryClient().getAllVersions(topic + "-value");
    assertThat(versions.size()).isOne();

    final SchemaMetadata metadata =
            this.schemaRegistry.getSchemaRegistryClient().getSchemaMetadata(topic + "-value", versions.get(0));
    assertThat(metadata.getId()).isEqualTo(id);
    final String schemaString = metadata.getSchema();
    final Schema retrievedSchema = new Schema.Parser().parse(schemaString);
    assertThat(retrievedSchema).isEqualTo(valueSchema);
}
 
Example 3
Source File: SchemaRegistryMockExtensionTest.java    From fluent-kafka-streams-tests with MIT License 6 votes vote down vote up
@Test
void shouldHaveLatestSchemaVersion() throws IOException, RestClientException {
    final Schema valueSchema1 = this.createSchema("value_schema");
    final String topic = "test-topic";
    final int id1 = this.schemaRegistry.registerValueSchema(topic, valueSchema1);

    final List<Schema.Field> fields = Collections.singletonList(
            new Schema.Field("f1", Schema.create(Schema.Type.STRING), "", (Object) null));
    final Schema valueSchema2 = Schema.createRecord("value_schema", "no doc", "", false, fields);
    final int id2 = this.schemaRegistry.registerValueSchema(topic, valueSchema2);

    final List<Integer> versions = this.schemaRegistry.getSchemaRegistryClient().getAllVersions(topic + "-value");
    assertThat(versions.size()).isEqualTo(2);

    final SchemaMetadata metadata =
            this.schemaRegistry.getSchemaRegistryClient().getLatestSchemaMetadata(topic + "-value");
    final int metadataId = metadata.getId();
    assertThat(metadataId).isNotEqualTo(id1);
    assertThat(metadataId).isEqualTo(id2);
    final String schemaString = metadata.getSchema();
    final Schema retrievedSchema = new Schema.Parser().parse(schemaString);
    assertThat(retrievedSchema).isEqualTo(valueSchema2);
}
 
Example 4
Source File: SchemaRegistryMockRuleTest.java    From fluent-kafka-streams-tests with MIT License 6 votes vote down vote up
@Test
public void shouldHaveSchemaVersions() throws IOException, RestClientException {
    final Schema valueSchema = this.createSchema("value_schema");
    final String topic = "test-topic";
    final int id = this.schemaRegistry.registerValueSchema(topic, valueSchema);

    final List<Integer> versions = this.schemaRegistry.getSchemaRegistryClient().getAllVersions(topic + "-value");
    assertThat(versions.size()).isOne();

    final SchemaMetadata metadata =
            this.schemaRegistry.getSchemaRegistryClient().getSchemaMetadata(topic + "-value", versions.get(0));
    assertThat(metadata.getId()).isEqualTo(id);
    final String schemaString = metadata.getSchema();
    final Schema retrievedSchema = new Schema.Parser().parse(schemaString);
    assertThat(retrievedSchema).isEqualTo(valueSchema);
}
 
Example 5
Source File: SchemaRegistryMockRuleTest.java    From fluent-kafka-streams-tests with MIT License 6 votes vote down vote up
@Test
public void shouldHaveLatestSchemaVersion() throws IOException, RestClientException {
    final Schema valueSchema1 = this.createSchema("value_schema");
    final String topic = "test-topic";
    final int id1 = this.schemaRegistry.registerValueSchema(topic, valueSchema1);

    final List<Schema.Field> fields = Collections.singletonList(
            new Schema.Field("f1", Schema.create(Schema.Type.STRING), "", (Object) null));
    final Schema valueSchema2 = Schema.createRecord("value_schema", "no doc", "", false, fields);
    final int id2 = this.schemaRegistry.registerValueSchema(topic, valueSchema2);

    final List<Integer> versions = this.schemaRegistry.getSchemaRegistryClient().getAllVersions(topic + "-value");
    assertThat(versions.size()).isEqualTo(2);

    final SchemaMetadata metadata =
            this.schemaRegistry.getSchemaRegistryClient().getLatestSchemaMetadata(topic + "-value");
    final int metadataId = metadata.getId();
    assertThat(metadataId).isNotEqualTo(id1);
    assertThat(metadataId).isEqualTo(id2);
    final String schemaString = metadata.getSchema();
    final Schema retrievedSchema = new Schema.Parser().parse(schemaString);
    assertThat(retrievedSchema).isEqualTo(valueSchema2);
}
 
Example 6
Source File: SchemaRegistryMockTest.java    From fluent-kafka-streams-tests with MIT License 5 votes vote down vote up
@Test
void shouldHaveSchemaVersions() throws IOException, RestClientException {
    final Schema valueSchema = createSchema("value_schema");
    final String topic = "test-topic";
    final int id = this.schemaRegistry.registerValueSchema(topic, valueSchema);

    final List<Integer> versions = this.schemaRegistry.getSchemaRegistryClient().getAllVersions(topic + "-value");
    assertThat(versions.size()).isOne();

    final SchemaMetadata metadata = this.schemaRegistry.getSchemaRegistryClient().getSchemaMetadata(topic + "-value", versions.get(0));
    assertThat(metadata.getId()).isEqualTo(id);
    final String schemaString = metadata.getSchema();
    final Schema retrievedSchema = new Schema.Parser().parse(schemaString);
    assertThat(retrievedSchema).isEqualTo(valueSchema);
}
 
Example 7
Source File: AvroUtil.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 4 votes vote down vote up
public Pair<AbstractStreamCreateStatement, String> checkAndSetAvroSchema(
    final AbstractStreamCreateStatement abstractStreamCreateStatement,
    final Map<String, Object> streamsProperties,
    final SchemaRegistryClient schemaRegistryClient
) {

  Map<String, Expression> ddlProperties = abstractStreamCreateStatement.getProperties();
  if (!ddlProperties.containsKey(DdlConfig.VALUE_FORMAT_PROPERTY)) {
    throw new KsqlException(String.format(
        "%s should be set in WITH clause of CREATE STREAM/TABLE statement.",
        DdlConfig.VALUE_FORMAT_PROPERTY
    )
    );
  }
  final String serde = StringUtil.cleanQuotes(
      ddlProperties.get(DdlConfig.VALUE_FORMAT_PROPERTY).toString()
  );
  if (!serde.equalsIgnoreCase(DataSource.AVRO_SERDE_NAME)) {
    return new Pair<>(abstractStreamCreateStatement, null);
  }

  String kafkaTopicName = StringUtil.cleanQuotes(
      ddlProperties.get(DdlConfig.KAFKA_TOPIC_NAME_PROPERTY).toString()
  );
  try {
    // If the schema is not specified infer it from the Avro schema in Schema Registry.
    if (abstractStreamCreateStatement.getElements().isEmpty()) {
      SchemaMetadata schemaMetadata = fetchSchemaMetadata(
          abstractStreamCreateStatement,
          schemaRegistryClient,
          kafkaTopicName
      );

      String avroSchemaString = schemaMetadata.getSchema();
      streamsProperties.put(DdlConfig.AVRO_SCHEMA, avroSchemaString);
      Schema schema = SerDeUtil.getSchemaFromAvro(avroSchemaString);
      AbstractStreamCreateStatement abstractStreamCreateStatementCopy = addAvroFields(
          abstractStreamCreateStatement,
          schema,
          schemaMetadata.getId()
      );
      return new Pair<>(
          abstractStreamCreateStatementCopy,
          SqlFormatter.formatSql(abstractStreamCreateStatementCopy)
      );
    } else {
      return new Pair<>(abstractStreamCreateStatement, null);
    }
  } catch (Exception e) {
    String errorMessage = String.format(
        " Unable to verify the AVRO schema is compatible with KSQL. %s ",
        e.getMessage()
    );
    throw new KsqlException(errorMessage);
  }
}