Search in sources :

Example 16 with SchemaRegistryClient

use of io.confluent.kafka.schemaregistry.client.SchemaRegistryClient in project beam by apache.

the class ConfluentSchemaRegistryDeserializerProviderTest method testGetCoder.

@Test
public void testGetCoder() {
    String schemaRegistryUrl = "mock://my-scope-name";
    String subject = "mytopic";
    SchemaRegistryClient mockRegistryClient = mockSchemaRegistryClient(schemaRegistryUrl, subject);
    CoderRegistry coderRegistry = CoderRegistry.createDefault();
    AvroCoder coderV0 = (AvroCoder) mockDeserializerProvider(schemaRegistryUrl, subject, null).getCoder(coderRegistry);
    assertEquals(AVRO_SCHEMA, coderV0.getSchema());
    try {
        Integer version = mockRegistryClient.getVersion(subject, AVRO_SCHEMA_V1);
        AvroCoder coderV1 = (AvroCoder) mockDeserializerProvider(schemaRegistryUrl, subject, version).getCoder(coderRegistry);
        assertEquals(AVRO_SCHEMA_V1, coderV1.getSchema());
    } catch (IOException | RestClientException e) {
        throw new RuntimeException("Unable to register schema for subject: " + subject, e);
    }
}
Also used : CoderRegistry(org.apache.beam.sdk.coders.CoderRegistry) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) IOException(java.io.IOException) AvroCoder(org.apache.beam.sdk.coders.AvroCoder) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) Test(org.junit.Test)

Example 17 with SchemaRegistryClient

use of io.confluent.kafka.schemaregistry.client.SchemaRegistryClient 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 18 with SchemaRegistryClient

use of io.confluent.kafka.schemaregistry.client.SchemaRegistryClient in project ksql by confluentinc.

the class AvroUtilTest method shouldNotPassAvroCheckIfSchemaDoesNotExist.

@Test
public void shouldNotPassAvroCheckIfSchemaDoesNotExist() throws Exception {
    SchemaRegistryClient schemaRegistryClient = mock(SchemaRegistryClient.class);
    SchemaMetadata schemaMetadata = new SchemaMetadata(1, 1, null);
    expect(schemaRegistryClient.getLatestSchemaMetadata(anyString())).andReturn(schemaMetadata);
    replay(schemaRegistryClient);
    AbstractStreamCreateStatement abstractStreamCreateStatement = getAbstractStreamCreateStatement("CREATE STREAM S1 WITH " + "(kafka_topic='s1_topic', " + "value_format='avro' );");
    try {
        avroUtil.checkAndSetAvroSchema(abstractStreamCreateStatement, new HashMap<>(), schemaRegistryClient);
        fail();
    } catch (Exception e) {
        assertThat("Expected different message message.", e.getMessage(), equalTo(" Could not " + "fetch the AVRO schema " + "from schema registry. null "));
    }
}
Also used : SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) AbstractStreamCreateStatement(io.confluent.ksql.parser.tree.AbstractStreamCreateStatement) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) IOException(java.io.IOException) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) Test(org.junit.Test)

Example 19 with SchemaRegistryClient

use of io.confluent.kafka.schemaregistry.client.SchemaRegistryClient in project ksql by confluentinc.

the class SchemaKStream method groupBy.

public SchemaKGroupedStream groupBy(final Serde<String> keySerde, final Serde<GenericRow> valSerde, final List<Expression> groupByExpressions) {
    boolean rekey = rekeyRequired(groupByExpressions);
    if (!rekey) {
        KGroupedStream kgroupedStream = kstream.groupByKey(Serialized.with(keySerde, valSerde));
        return new SchemaKGroupedStream(schema, kgroupedStream, keyField, Collections.singletonList(this), functionRegistry, schemaRegistryClient);
    }
    // Collect the column indexes, and build the new key as <column1>+<column2>+...
    StringBuilder aggregateKeyName = new StringBuilder();
    List<Integer> newKeyIndexes = new ArrayList<>();
    boolean addSeparator = false;
    for (Expression groupByExpr : groupByExpressions) {
        if (addSeparator) {
            aggregateKeyName.append("|+|");
        } else {
            addSeparator = true;
        }
        aggregateKeyName.append(groupByExpr.toString());
        newKeyIndexes.add(SchemaUtil.getIndexInSchema(groupByExpr.toString(), getSchema()));
    }
    KGroupedStream kgroupedStream = kstream.filter((key, value) -> value != null).groupBy((key, value) -> {
        StringBuilder newKey = new StringBuilder();
        boolean addSeparator1 = false;
        for (int index : newKeyIndexes) {
            if (addSeparator1) {
                newKey.append("|+|");
            } else {
                addSeparator1 = true;
            }
            newKey.append(String.valueOf(value.getColumns().get(index)));
        }
        return newKey.toString();
    }, Serialized.with(keySerde, valSerde));
    // TODO: if the key is a prefix of the grouping columns then we can
    // use the repartition reflection hack to tell streams not to
    // repartition.
    Field newKeyField = new Field(aggregateKeyName.toString(), -1, Schema.STRING_SCHEMA);
    return new SchemaKGroupedStream(schema, kgroupedStream, newKeyField, Collections.singletonList(this), functionRegistry, schemaRegistryClient);
}
Also used : Arrays(java.util.Arrays) KGroupedStream(org.apache.kafka.streams.kstream.KGroupedStream) Produced(org.apache.kafka.streams.kstream.Produced) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) Serialized(org.apache.kafka.streams.kstream.Serialized) KStream(org.apache.kafka.streams.kstream.KStream) Joined(org.apache.kafka.streams.kstream.Joined) Schema(org.apache.kafka.connect.data.Schema) ArrayList(java.util.ArrayList) Pair(io.confluent.ksql.util.Pair) Serde(org.apache.kafka.common.serialization.Serde) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) Serdes(org.apache.kafka.common.serialization.Serdes) CodeGenRunner(io.confluent.ksql.codegen.CodeGenRunner) SchemaUtil(io.confluent.ksql.util.SchemaUtil) OutputNode(io.confluent.ksql.planner.plan.OutputNode) Field(org.apache.kafka.connect.data.Field) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) Set(java.util.Set) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Expression(io.confluent.ksql.parser.tree.Expression) List(java.util.List) ValueJoiner(org.apache.kafka.streams.kstream.ValueJoiner) GenericRow(io.confluent.ksql.GenericRow) Optional(java.util.Optional) KsqlException(io.confluent.ksql.util.KsqlException) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) KsqlTopicSerDe(io.confluent.ksql.serde.KsqlTopicSerDe) Collections(java.util.Collections) GenericRowValueTypeEnforcer(io.confluent.ksql.util.GenericRowValueTypeEnforcer) Field(org.apache.kafka.connect.data.Field) KGroupedStream(org.apache.kafka.streams.kstream.KGroupedStream) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) ArrayList(java.util.ArrayList)

Example 20 with SchemaRegistryClient

use of io.confluent.kafka.schemaregistry.client.SchemaRegistryClient in project ksql by confluentinc.

the class KsqlResourceTest method setUp.

@Before
public void setUp() throws IOException, RestClientException {
    SchemaRegistryClient schemaRegistryClient = new MockSchemaRegistryClient();
    registerSchema(schemaRegistryClient);
    ksqlRestConfig = new KsqlRestConfig(TestKsqlResourceUtil.getDefaultKsqlConfig());
    KsqlConfig ksqlConfig = new KsqlConfig(ksqlRestConfig.getKsqlConfigProperties());
    ksqlEngine = new KsqlEngine(ksqlConfig, new MockKafkaTopicClient(), schemaRegistryClient, new MetaStoreImpl());
}
Also used : KsqlEngine(io.confluent.ksql.KsqlEngine) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) KsqlRestConfig(io.confluent.ksql.rest.server.KsqlRestConfig) MockKafkaTopicClient(io.confluent.ksql.rest.server.mock.MockKafkaTopicClient) MetaStoreImpl(io.confluent.ksql.metastore.MetaStoreImpl) KsqlConfig(io.confluent.ksql.util.KsqlConfig) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) Before(org.junit.Before)

Aggregations

SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)28 Test (org.junit.Test)21 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)11 KsqlConfig (io.confluent.ksql.util.KsqlConfig)10 GenericRow (io.confluent.ksql.GenericRow)8 List (java.util.List)8 HashMap (java.util.HashMap)7 Schema (org.apache.avro.Schema)6 GenericRecord (org.apache.avro.generic.GenericRecord)5 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)4 KafkaAvroDeserializer (io.confluent.kafka.serializers.KafkaAvroDeserializer)4 KafkaAvroSerializer (io.confluent.kafka.serializers.KafkaAvroSerializer)4 IOException (java.io.IOException)4 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)4 Bytes (org.apache.kafka.common.utils.Bytes)4 Schema (org.apache.kafka.connect.data.Schema)4 Map (java.util.Map)3 GenericData (org.apache.avro.generic.GenericData)3 Test (org.testng.annotations.Test)3 SchemaMetadata (io.confluent.kafka.schemaregistry.client.SchemaMetadata)2