Search in sources :

Example 66 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class DecimalMinKudafTest method shouldFindCorrectMinForMerge.

@Test
public void shouldFindCorrectMinForMerge() {
    final DecimalMinKudaf doubleMinKudaf = getDecimalMinKudaf(3);
    final Merger<GenericKey, BigDecimal> merger = doubleMinKudaf.getMerger();
    final BigDecimal mergeResult1 = merger.apply(null, new BigDecimal(10.0), new BigDecimal(12.0));
    assertThat(mergeResult1, equalTo(new BigDecimal(10.0, new MathContext(3))));
    final BigDecimal mergeResult2 = merger.apply(null, new BigDecimal(10.0), new BigDecimal(-12.0));
    assertThat(mergeResult2, equalTo(new BigDecimal(-12.0, new MathContext(3))));
    final BigDecimal mergeResult3 = merger.apply(null, new BigDecimal(-10.0), new BigDecimal(0.0));
    assertThat(mergeResult3, equalTo(new BigDecimal(-10.0, new MathContext(3))));
}
Also used : GenericKey(io.confluent.ksql.GenericKey) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) Test(org.junit.Test)

Example 67 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class RowGenerator method generateRow.

public Pair<GenericKey, GenericRow> generateRow() {
    final Object generatedObject = generator.generate();
    if (!(generatedObject instanceof GenericRecord)) {
        throw new RuntimeException(String.format("Expected Avro Random Generator to return instance of GenericRecord, found %s instead", generatedObject.getClass().getName()));
    }
    final GenericRecord randomAvroMessage = (GenericRecord) generatedObject;
    final GenericRow row = new GenericRow(generator.schema().getFields().size());
    SimpleDateFormat timeformatter = null;
    /*
     * Populate the record entries
     */
    String sessionisationValue = null;
    for (final Schema.Field field : generator.schema().getFields()) {
        final boolean isSession = field.schema().getProp("session") != null;
        final boolean isSessionSiblingIntHash = field.schema().getProp("session-sibling-int-hash") != null;
        final String timeFormatFromLong = field.schema().getProp("format_as_time");
        if (isSession) {
            final String currentValue = (String) randomAvroMessage.get(field.name());
            final String newCurrentValue = handleSessionisationOfValue(sessionManager, currentValue);
            sessionisationValue = newCurrentValue;
            row.append(newCurrentValue);
        } else if (isSessionSiblingIntHash && sessionisationValue != null) {
            // super cheeky hack to link int-ids to session-values - if anything fails then we use
            // the 'avro-gen' randomised version
            handleSessionSiblingField(randomAvroMessage, row, sessionisationValue, field);
        } else if (timeFormatFromLong != null) {
            final Date date = new Date(System.currentTimeMillis());
            if (timeFormatFromLong.equals("unix_long")) {
                row.append(date.getTime());
            } else {
                if (timeformatter == null) {
                    timeformatter = new SimpleDateFormat(timeFormatFromLong);
                }
                row.append(timeformatter.format(date));
            }
        } else {
            final Object value = randomAvroMessage.get(field.name());
            if (value instanceof Record) {
                final Field ksqlField = valueSchema.field(field.name());
                final Record record = (Record) value;
                final Object ksqlValue = avroData.toConnectData(record.getSchema(), record).value();
                row.append(DataGenSchemaUtil.getOptionalValue(ksqlField.schema(), ksqlValue));
            } else {
                row.append(value);
            }
        }
    }
    final GenericKey key = GenericKey.genericKey(row.get(keyFieldIndex));
    return Pair.of(key, row);
}
Also used : Schema(org.apache.avro.Schema) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) ConnectSchema(org.apache.kafka.connect.data.ConnectSchema) Date(java.util.Date) GenericRow(io.confluent.ksql.GenericRow) Field(org.apache.kafka.connect.data.Field) Record(org.apache.avro.generic.GenericData.Record) GenericRecord(org.apache.avro.generic.GenericRecord) GenericKey(io.confluent.ksql.GenericKey) GenericRecord(org.apache.avro.generic.GenericRecord) SimpleDateFormat(java.text.SimpleDateFormat)

Example 68 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class RowGeneratorTest method shouldGenerateCorrectRow.

@Test
public void shouldGenerateCorrectRow() throws IOException {
    final Generator generator = new Generator(new File("./src/main/resources/orders_schema.avro"), new Random());
    final RowGenerator rowGenerator = new RowGenerator(generator, "orderid", Optional.empty());
    final Pair<GenericKey, GenericRow> rowPair = rowGenerator.generateRow();
    final GenericKey key = rowPair.getLeft();
    assertThat(key, is(notNullValue()));
    assertThat(key.get(0), is(instanceOf(Integer.class)));
    assertThat(rowPair.getRight().values(), hasSize(5));
    assertThat(rowPair.getRight().get(4), instanceOf(Struct.class));
    final Struct struct = (Struct) rowPair.getRight().get(4);
    assertThat(struct.schema().fields(), hasSize(3));
    assertThat(struct.schema().field("city").schema().type(), equalTo(Type.STRING));
    assertThat(struct.schema().field("state").schema().type(), equalTo(Type.STRING));
    assertThat(struct.schema().field("zipcode").schema().type(), equalTo(Type.INT64));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) Random(java.util.Random) GenericKey(io.confluent.ksql.GenericKey) File(java.io.File) Generator(io.confluent.avro.random.generator.Generator) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 69 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class ProducerFactory method getProducer.

static DataGenProducer getProducer(final Format keyFormat, final Format valueFormat, final String valueDelimiter, final Properties props) {
    final KsqlConfig ksqlConfig = new KsqlConfig(props);
    final Optional<SchemaRegistryClient> srClient = SchemaRegistryClientFactory.getSrClient(keyFormat, valueFormat, ksqlConfig);
    final SerializerFactory<GenericKey> keySerializerFactory = keySerializerFactory(keyFormat, ksqlConfig, srClient);
    final Map<String, String> formatInfoProperties = new HashMap<>();
    if (valueDelimiter != null) {
        if (!(valueFormat instanceof DelimitedFormat)) {
            throw new IllegalArgumentException("valueDelimiter can only be specified with delimited format");
        }
        formatInfoProperties.put(DelimitedFormat.DELIMITER, valueDelimiter);
    }
    final SerializerFactory<GenericRow> valueSerializerFactory = valueSerializerFactory(valueFormat, ksqlConfig, srClient, formatInfoProperties);
    return new DataGenProducer(keySerializerFactory, valueSerializerFactory);
}
Also used : HashMap(java.util.HashMap) DelimitedFormat(io.confluent.ksql.serde.delimited.DelimitedFormat) KsqlConfig(io.confluent.ksql.util.KsqlConfig) GenericRow(io.confluent.ksql.GenericRow) GenericKey(io.confluent.ksql.GenericKey) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)

Example 70 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class TestDriverPipelineTest method shouldHandleMultiSourceTopology.

@Test
public void shouldHandleMultiSourceTopology() {
    // Given (a topology that pipes a and b directly to c):
    final TopologyTestDriver driver = mock(TopologyTestDriver.class);
    final TestInputTopic<GenericKey, GenericRow> inA = givenInput(driver, "a");
    final TestInputTopic<GenericKey, GenericRow> inB = givenInput(driver, "b");
    givenOutput(driver, "c");
    givenPipe(inA, "c");
    givenPipe(inB, "c");
    pipeline.addDriver(driver, ImmutableList.of(inf("a"), inf("b")), inf("c"));
    // When:
    pipeline.pipeInput("a", KEY, ROW1, 1);
    pipeline.pipeInput("b", KEY, ROW2, 1);
    // Then:
    assertThat(pipeline.getAllRecordsForTopic("c"), is(ImmutableList.of(new TestRecord<>(KEY, ROW1, Instant.ofEpochMilli(1)), new TestRecord<>(KEY, ROW2, Instant.ofEpochMilli(1)))));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) GenericKey(io.confluent.ksql.GenericKey) Test(org.junit.Test)

Aggregations

GenericKey (io.confluent.ksql.GenericKey)147 GenericRow (io.confluent.ksql.GenericRow)100 Test (org.junit.Test)93 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)24 Windowed (org.apache.kafka.streams.kstream.Windowed)20 WindowedRow (io.confluent.ksql.execution.streams.materialization.WindowedRow)14 PhysicalSchema (io.confluent.ksql.schema.ksql.PhysicalSchema)14 Materialized (org.apache.kafka.streams.kstream.Materialized)13 ValueAndTimestamp (org.apache.kafka.streams.state.ValueAndTimestamp)13 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)12 MaterializationException (io.confluent.ksql.execution.streams.materialization.MaterializationException)9 IntegrationTest (io.confluent.common.utils.IntegrationTest)8 Materialization (io.confluent.ksql.execution.streams.materialization.Materialization)8 Row (io.confluent.ksql.execution.streams.materialization.Row)8 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)8 TimeWindow (org.apache.kafka.streams.kstream.internals.TimeWindow)8 IntegrationTest (org.apache.kafka.test.IntegrationTest)8 InOrder (org.mockito.InOrder)8 MaterializedTable (io.confluent.ksql.execution.streams.materialization.MaterializedTable)7 Objects (java.util.Objects)7