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))));
}
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);
}
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));
}
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);
}
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)))));
}
Aggregations