Search in sources :

Example 1 with KsqlAvroTopicSerDe

use of io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe in project ksql by confluentinc.

the class AvroUtilTest method shouldFailForInvalidResultAvroSchema.

@Test
public void shouldFailForInvalidResultAvroSchema() throws IOException, RestClientException {
    SchemaRegistryClient schemaRegistryClient = mock(SchemaRegistryClient.class);
    KsqlTopic resultTopic = new KsqlTopic("testTopic", "testTopic", new KsqlAvroTopicSerDe());
    Schema resultSchema = SerDeUtil.getSchemaFromAvro(ordersAveroSchemaStr);
    PersistentQueryMetadata persistentQueryMetadata = new PersistentQueryMetadata("", null, null, "", null, DataSource.DataSourceType.KSTREAM, "", mock(KafkaTopicClient.class), resultSchema, resultTopic, null);
    expect(schemaRegistryClient.testCompatibility(anyString(), anyObject())).andReturn(false);
    replay(schemaRegistryClient);
    try {
        avroUtil.validatePersistentQueryResults(persistentQueryMetadata, schemaRegistryClient);
        fail();
    } catch (Exception e) {
        assertThat("Incorrect exception message", "Cannot register avro schema for testTopic since " + "it is not valid for schema registry.", equalTo(e.getMessage()));
    }
}
Also used : KsqlAvroTopicSerDe(io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe) Schema(org.apache.kafka.connect.data.Schema) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) IOException(java.io.IOException) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic) Test(org.junit.Test)

Example 2 with KsqlAvroTopicSerDe

use of io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe in project ksql by confluentinc.

the class KsqlStructuredDataOutputNode method buildStream.

@Override
public SchemaKStream buildStream(final StreamsBuilder builder, final KsqlConfig ksqlConfig, final KafkaTopicClient kafkaTopicClient, final FunctionRegistry functionRegistry, final Map<String, Object> props, final SchemaRegistryClient schemaRegistryClient) {
    final SchemaKStream schemaKStream = getSource().buildStream(builder, ksqlConfig, kafkaTopicClient, functionRegistry, props, schemaRegistryClient);
    final Set<Integer> rowkeyIndexes = SchemaUtil.getRowTimeRowKeyIndexes(getSchema());
    final Builder outputNodeBuilder = Builder.from(this);
    final Schema schema = SchemaUtil.removeImplicitRowTimeRowKeyFromSchema(getSchema());
    outputNodeBuilder.withSchema(schema);
    if (getTopicSerde() instanceof KsqlAvroTopicSerDe) {
        addAvroSchemaToResultTopic(outputNodeBuilder);
    }
    final Map<String, Object> outputProperties = getOutputProperties();
    if (outputProperties.containsKey(KsqlConfig.SINK_NUMBER_OF_PARTITIONS_PROPERTY)) {
        ksqlConfig.put(KsqlConfig.SINK_NUMBER_OF_PARTITIONS_PROPERTY, outputProperties.get(KsqlConfig.SINK_NUMBER_OF_PARTITIONS_PROPERTY));
    }
    if (outputProperties.containsKey(KsqlConfig.SINK_NUMBER_OF_REPLICAS_PROPERTY)) {
        ksqlConfig.put(KsqlConfig.SINK_NUMBER_OF_REPLICAS_PROPERTY, outputProperties.get(KsqlConfig.SINK_NUMBER_OF_REPLICAS_PROPERTY));
    }
    final SchemaKStream result = createOutputStream(schemaKStream, outputNodeBuilder, functionRegistry, outputProperties, schemaRegistryClient);
    final KsqlStructuredDataOutputNode noRowKey = outputNodeBuilder.build();
    createSinkTopic(noRowKey.getKafkaTopicName(), ksqlConfig, kafkaTopicClient, shoulBeCompacted(result));
    result.into(noRowKey.getKafkaTopicName(), noRowKey.getKsqlTopic().getKsqlTopicSerDe().getGenericRowSerde(noRowKey.getSchema(), ksqlConfig, false, schemaRegistryClient), rowkeyIndexes);
    result.setOutputNode(outputNodeBuilder.withSchema(SchemaUtil.addImplicitRowTimeRowKeyToSchema(noRowKey.getSchema())).build());
    return result;
}
Also used : KsqlAvroTopicSerDe(io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe) StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) Schema(org.apache.kafka.connect.data.Schema) SchemaKStream(io.confluent.ksql.structured.SchemaKStream)

Example 3 with KsqlAvroTopicSerDe

use of io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe in project ksql by confluentinc.

the class AvroUtilTest method shouldValidatePersistentQueryResultCorrectly.

@Test
public void shouldValidatePersistentQueryResultCorrectly() throws IOException, RestClientException {
    SchemaRegistryClient schemaRegistryClient = mock(SchemaRegistryClient.class);
    KsqlTopic resultTopic = new KsqlTopic("testTopic", "testTopic", new KsqlAvroTopicSerDe());
    Schema resultSchema = SerDeUtil.getSchemaFromAvro(ordersAveroSchemaStr);
    PersistentQueryMetadata persistentQueryMetadata = new PersistentQueryMetadata("", null, null, "", null, DataSource.DataSourceType.KSTREAM, "", mock(KafkaTopicClient.class), resultSchema, resultTopic, null);
    org.apache.avro.Schema.Parser parser = new org.apache.avro.Schema.Parser();
    org.apache.avro.Schema avroSchema = parser.parse(ordersAveroSchemaStr);
    expect(schemaRegistryClient.testCompatibility(anyString(), EasyMock.isA(avroSchema.getClass()))).andReturn(true);
    replay(schemaRegistryClient);
    avroUtil.validatePersistentQueryResults(persistentQueryMetadata, schemaRegistryClient);
}
Also used : KsqlAvroTopicSerDe(io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe) Schema(org.apache.kafka.connect.data.Schema) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic) KsqlParser(io.confluent.ksql.parser.KsqlParser) Test(org.junit.Test)

Example 4 with KsqlAvroTopicSerDe

use of io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe in project ksql by confluentinc.

the class Analyzer method analyzeNonStdOutSink.

private void analyzeNonStdOutSink() {
    List<Pair<StructuredDataSource, String>> fromDataSources = analysis.getFromDataSources();
    StructuredDataSource intoStructuredDataSource = analysis.getInto();
    String intoKafkaTopicName = analysis.getIntoKafkaTopicName();
    if (intoKafkaTopicName == null) {
        intoKafkaTopicName = intoStructuredDataSource.getName();
    }
    KsqlTopicSerDe intoTopicSerde = fromDataSources.get(0).getLeft().getKsqlTopic().getKsqlTopicSerDe();
    if (analysis.getIntoFormat() != null) {
        switch(analysis.getIntoFormat().toUpperCase()) {
            case DataSource.AVRO_SERDE_NAME:
                intoTopicSerde = new KsqlAvroTopicSerDe();
                break;
            case DataSource.JSON_SERDE_NAME:
                intoTopicSerde = new KsqlJsonTopicSerDe();
                break;
            case DataSource.DELIMITED_SERDE_NAME:
                intoTopicSerde = new KsqlDelimitedTopicSerDe();
                break;
            default:
                throw new KsqlException(String.format("Unsupported format: %s", analysis.getIntoFormat()));
        }
    } else {
        if (intoTopicSerde instanceof KsqlAvroTopicSerDe) {
            intoTopicSerde = new KsqlAvroTopicSerDe();
        }
    }
    KsqlTopic newIntoKsqlTopic = new KsqlTopic(intoKafkaTopicName, intoKafkaTopicName, intoTopicSerde);
    KsqlStream intoKsqlStream = new KsqlStream(sqlExpression, intoStructuredDataSource.getName(), null, null, null, newIntoKsqlTopic);
    analysis.setInto(intoKsqlStream);
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) KsqlStream(io.confluent.ksql.metastore.KsqlStream) KsqlDelimitedTopicSerDe(io.confluent.ksql.serde.delimited.KsqlDelimitedTopicSerDe) KsqlAvroTopicSerDe(io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe) KsqlTopicSerDe(io.confluent.ksql.serde.KsqlTopicSerDe) KsqlJsonTopicSerDe(io.confluent.ksql.serde.json.KsqlJsonTopicSerDe) KsqlException(io.confluent.ksql.util.KsqlException) Pair(io.confluent.ksql.util.Pair) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic)

Example 5 with KsqlAvroTopicSerDe

use of io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe in project ksql by confluentinc.

the class KsqlStructuredDataOutputNode method addAvroSchemaToResultTopic.

private void addAvroSchemaToResultTopic(final Builder builder) {
    final KsqlAvroTopicSerDe ksqlAvroTopicSerDe = new KsqlAvroTopicSerDe();
    builder.withKsqlTopic(new KsqlTopic(getKsqlTopic().getName(), getKsqlTopic().getKafkaTopicName(), ksqlAvroTopicSerDe));
}
Also used : KsqlAvroTopicSerDe(io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe) KsqlTopic(io.confluent.ksql.metastore.KsqlTopic)

Aggregations

KsqlAvroTopicSerDe (io.confluent.ksql.serde.avro.KsqlAvroTopicSerDe)5 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)4 Schema (org.apache.kafka.connect.data.Schema)3 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)2 Test (org.junit.Test)2 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)1 KsqlStream (io.confluent.ksql.metastore.KsqlStream)1 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)1 KsqlParser (io.confluent.ksql.parser.KsqlParser)1 KsqlTopicSerDe (io.confluent.ksql.serde.KsqlTopicSerDe)1 KsqlDelimitedTopicSerDe (io.confluent.ksql.serde.delimited.KsqlDelimitedTopicSerDe)1 KsqlJsonTopicSerDe (io.confluent.ksql.serde.json.KsqlJsonTopicSerDe)1 SchemaKStream (io.confluent.ksql.structured.SchemaKStream)1 KsqlException (io.confluent.ksql.util.KsqlException)1 Pair (io.confluent.ksql.util.Pair)1 IOException (java.io.IOException)1 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)1