Search in sources :

Example 1 with SchemaKStream

use of io.confluent.ksql.structured.SchemaKStream in project ksql by confluentinc.

the class AggregateNodeTest method shouldBuildCorrectAggregateSchema.

@Test
public void shouldBuildCorrectAggregateSchema() {
    SchemaKStream stream = build();
    final List<Field> expected = Arrays.asList(new Field("COL0", 0, Schema.INT64_SCHEMA), new Field("KSQL_COL_1", 1, Schema.FLOAT64_SCHEMA), new Field("KSQL_COL_2", 2, Schema.INT64_SCHEMA));
    assertThat(stream.getSchema().fields(), equalTo(expected));
}
Also used : Field(org.apache.kafka.connect.data.Field) SchemaKStream(io.confluent.ksql.structured.SchemaKStream) Test(org.junit.Test)

Example 2 with SchemaKStream

use of io.confluent.ksql.structured.SchemaKStream in project ksql by confluentinc.

the class AggregateNodeTest method shouldBeSchemaKTableResult.

@Test
public void shouldBeSchemaKTableResult() {
    SchemaKStream stream = build();
    assertThat(stream.getClass(), equalTo(SchemaKTable.class));
}
Also used : SchemaKTable(io.confluent.ksql.structured.SchemaKTable) SchemaKStream(io.confluent.ksql.structured.SchemaKStream) Test(org.junit.Test)

Example 3 with SchemaKStream

use of io.confluent.ksql.structured.SchemaKStream in project ksql by confluentinc.

the class AggregateNodeTest method shouldBeWindowedWhenStatementSpecifiesWindowing.

@Test
public void shouldBeWindowedWhenStatementSpecifiesWindowing() {
    SchemaKStream stream = build();
    assertTrue(((SchemaKTable) stream).isWindowed());
}
Also used : SchemaKStream(io.confluent.ksql.structured.SchemaKStream) Test(org.junit.Test)

Example 4 with SchemaKStream

use of io.confluent.ksql.structured.SchemaKStream in project ksql by confluentinc.

the class KsqlStructuredDataOutputNodeTest method shouldCreateSinkWithCorrectCleanupPolicyStream.

@Test
public void shouldCreateSinkWithCorrectCleanupPolicyStream() {
    KafkaTopicClient topicClientForWindowTable = EasyMock.mock(KafkaTopicClient.class);
    StreamsBuilder streamsBuilder = new StreamsBuilder();
    topicClientForWindowTable.createTopic("output", 4, (short) 3, Collections.emptyMap());
    EasyMock.replay(topicClientForWindowTable);
    SchemaKStream schemaKStream = outputNode.buildStream(streamsBuilder, ksqlConfig, topicClientForWindowTable, new FunctionRegistry(), new HashMap<>(), new MockSchemaRegistryClient());
    assertThat(schemaKStream, instanceOf(SchemaKStream.class));
    EasyMock.verify();
}
Also used : StreamsBuilder(org.apache.kafka.streams.StreamsBuilder) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) KafkaTopicClient(io.confluent.ksql.util.KafkaTopicClient) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) SchemaKStream(io.confluent.ksql.structured.SchemaKStream) Test(org.junit.Test)

Example 5 with SchemaKStream

use of io.confluent.ksql.structured.SchemaKStream in project ksql by confluentinc.

the class AggregateNode 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 StructuredDataSourceNode streamSourceNode = getTheSourceNode();
    final SchemaKStream sourceSchemaKStream = getSource().buildStream(builder, ksqlConfig, kafkaTopicClient, functionRegistry, props, schemaRegistryClient);
    if (sourceSchemaKStream instanceof SchemaKTable) {
        throw new KsqlException("Unsupported aggregation. KSQL currently only supports aggregation on a Stream.");
    }
    // Pre aggregate computations
    final List<Pair<String, Expression>> aggArgExpansionList = new ArrayList<>();
    final Map<String, Integer> expressionNames = new HashMap<>();
    collectAggregateArgExpressions(getRequiredColumnList(), aggArgExpansionList, expressionNames);
    collectAggregateArgExpressions(getAggregateFunctionArguments(), aggArgExpansionList, expressionNames);
    final SchemaKStream aggregateArgExpanded = sourceSchemaKStream.select(aggArgExpansionList);
    KsqlTopicSerDe ksqlTopicSerDe = streamSourceNode.getStructuredDataSource().getKsqlTopic().getKsqlTopicSerDe();
    final Serde<GenericRow> genericRowSerde = ksqlTopicSerDe.getGenericRowSerde(aggregateArgExpanded.getSchema(), ksqlConfig, true, schemaRegistryClient);
    final SchemaKGroupedStream schemaKGroupedStream = aggregateArgExpanded.groupBy(Serdes.String(), genericRowSerde, getGroupByExpressions());
    // Aggregate computations
    final SchemaBuilder aggregateSchema = SchemaBuilder.struct();
    final Map<Integer, Integer> aggValToValColumnMap = createAggregateValueToValueColumnMap(aggregateArgExpanded, aggregateSchema);
    final Schema aggStageSchema = buildAggregateSchema(aggregateArgExpanded.getSchema(), functionRegistry);
    final Serde<GenericRow> aggValueGenericRowSerde = ksqlTopicSerDe.getGenericRowSerde(aggStageSchema, ksqlConfig, true, schemaRegistryClient);
    final KudafInitializer initializer = new KudafInitializer(aggValToValColumnMap.size());
    final SchemaKTable schemaKTable = schemaKGroupedStream.aggregate(initializer, new KudafAggregator(createAggValToFunctionMap(expressionNames, aggregateArgExpanded, aggregateSchema, initializer, aggValToValColumnMap.size(), functionRegistry), aggValToValColumnMap), getWindowExpression(), aggValueGenericRowSerde);
    SchemaKTable result = new SchemaKTable(aggStageSchema, schemaKTable.getKtable(), schemaKTable.getKeyField(), schemaKTable.getSourceSchemaKStreams(), schemaKTable.isWindowed(), SchemaKStream.Type.AGGREGATE, functionRegistry, schemaRegistryClient);
    if (getHavingExpressions() != null) {
        result = result.filter(getHavingExpressions());
    }
    return result.select(getFinalSelectExpressions());
}
Also used : SchemaKTable(io.confluent.ksql.structured.SchemaKTable) HashMap(java.util.HashMap) Schema(org.apache.kafka.connect.data.Schema) ArrayList(java.util.ArrayList) KudafInitializer(io.confluent.ksql.function.udaf.KudafInitializer) KudafAggregator(io.confluent.ksql.function.udaf.KudafAggregator) KsqlException(io.confluent.ksql.util.KsqlException) SchemaKGroupedStream(io.confluent.ksql.structured.SchemaKGroupedStream) GenericRow(io.confluent.ksql.GenericRow) KsqlTopicSerDe(io.confluent.ksql.serde.KsqlTopicSerDe) SchemaBuilder(org.apache.kafka.connect.data.SchemaBuilder) SchemaKStream(io.confluent.ksql.structured.SchemaKStream) Pair(io.confluent.ksql.util.Pair)

Aggregations

SchemaKStream (io.confluent.ksql.structured.SchemaKStream)17 SchemaKTable (io.confluent.ksql.structured.SchemaKTable)10 Test (org.junit.Test)8 KsqlException (io.confluent.ksql.util.KsqlException)5 StreamsBuilder (org.apache.kafka.streams.StreamsBuilder)4 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)3 FunctionRegistry (io.confluent.ksql.function.FunctionRegistry)3 KsqlTopicSerDe (io.confluent.ksql.serde.KsqlTopicSerDe)3 KafkaTopicClient (io.confluent.ksql.util.KafkaTopicClient)3 Field (org.apache.kafka.connect.data.Field)3 GenericRow (io.confluent.ksql.GenericRow)2 KsqlTable (io.confluent.ksql.metastore.KsqlTable)2 QueuedSchemaKStream (io.confluent.ksql.structured.QueuedSchemaKStream)2 HashMap (java.util.HashMap)2 Schema (org.apache.kafka.connect.data.Schema)2 KudafAggregator (io.confluent.ksql.function.udaf.KudafAggregator)1 KudafInitializer (io.confluent.ksql.function.udaf.KudafInitializer)1 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)1 AddTimestampColumn (io.confluent.ksql.physical.AddTimestampColumn)1 KsqlBareOutputNode (io.confluent.ksql.planner.plan.KsqlBareOutputNode)1