use of io.confluent.ksql.GenericRow in project ksql by confluentinc.
the class UdfIntTest method testApplyUdfsToColumns.
private void testApplyUdfsToColumns(String resultStreamName, String inputStreamName, DataSource.DataSourceSerDe dataSourceSerde) throws Exception {
final String queryString = String.format("CREATE STREAM %s AS SELECT %s FROM %s WHERE %s;", resultStreamName, "ITEMID, ORDERUNITS*10, PRICEARRAY[0]+10, KEYVALUEMAP['key1']*KEYVALUEMAP['key2']+10, " + "PRICEARRAY[1]>1000", inputStreamName, "ORDERUNITS > 20 AND ITEMID LIKE '%_8'");
ksqlContext.sql(queryString);
Schema resultSchema = ksqlContext.getMetaStore().getSource(resultStreamName).getSchema();
Map<String, GenericRow> expectedResults = Collections.singletonMap("8", new GenericRow(Arrays.asList(null, null, "ITEM_8", 800.0, 1110.0, 12.0, true)));
Map<String, GenericRow> results = testHarness.consumeData(resultStreamName, resultSchema, 4, new StringDeserializer(), IntegrationTestHarness.RESULTS_POLL_MAX_TIME_MS, dataSourceSerde);
assertThat(results, equalTo(expectedResults));
}
use of io.confluent.ksql.GenericRow in project ksql by confluentinc.
the class UdfIntTest method testShouldCastSelectedColumns.
private void testShouldCastSelectedColumns(String resultStreamName, String inputStreamName, DataSource.DataSourceSerDe dataSourceSerde) throws Exception {
final String selectColumns = " CAST (ORDERUNITS AS INTEGER), CAST( PRICEARRAY[1]>1000 AS STRING), CAST (SUBSTRING" + "(ITEMID, 5) AS DOUBLE), CAST(ORDERUNITS AS VARCHAR) ";
final String queryString = String.format("CREATE STREAM %s AS SELECT %s FROM %s WHERE %s;", resultStreamName, selectColumns, inputStreamName, "ORDERUNITS > 20 AND ITEMID LIKE '%_8'");
ksqlContext.sql(queryString);
Schema resultSchema = ksqlContext.getMetaStore().getSource(resultStreamName).getSchema();
Map<String, GenericRow> expectedResults = Collections.singletonMap("8", new GenericRow(Arrays.asList(null, null, 80, "true", 8.0, "80.0")));
Map<String, GenericRow> results = testHarness.consumeData(resultStreamName, resultSchema, 4, new StringDeserializer(), IntegrationTestHarness.RESULTS_POLL_MAX_TIME_MS, dataSourceSerde);
assertThat(results, equalTo(expectedResults));
}
use of io.confluent.ksql.GenericRow in project ksql by confluentinc.
the class UdfIntTest method testApplyUdfsToColumnsDelimited.
@Test
public void testApplyUdfsToColumnsDelimited() throws Exception {
final String testStreamName = "SelectUDFsStreamDelimited".toUpperCase();
final String queryString = String.format("CREATE STREAM %s AS SELECT %s FROM %s WHERE %s;", testStreamName, "ID, DESCRIPTION", delimitedStreamName, "ID LIKE '%_1'");
ksqlContext.sql(queryString);
Map<String, GenericRow> expectedResults = Collections.singletonMap("ITEM_1", new GenericRow(Arrays.asList("ITEM_1", "home cinema")));
Map<String, GenericRow> results = testHarness.consumeData(testStreamName, itemDataProvider.schema(), 1, new StringDeserializer(), IntegrationTestHarness.RESULTS_POLL_MAX_TIME_MS, DataSource.DataSourceSerDe.DELIMITED);
assertThat(results, equalTo(expectedResults));
}
use of io.confluent.ksql.GenericRow in project ksql by confluentinc.
the class WindowingIntTest method shouldAggregateTumblingWindow.
@Test
public void shouldAggregateTumblingWindow() throws Exception {
testHarness.publishTestData(topicName, dataProvider, now);
final String streamName = "TUMBLING_AGGTEST";
final String queryString = String.format("CREATE TABLE %s AS SELECT %s FROM ORDERS WINDOW %s WHERE ITEMID = 'ITEM_1' GROUP BY ITEMID;", streamName, "ITEMID, COUNT(ITEMID), SUM(ORDERUNITS)", "TUMBLING ( SIZE 10 SECONDS)");
ksqlContext.sql(queryString);
Schema resultSchema = ksqlContext.getMetaStore().getSource(streamName).getSchema();
final GenericRow expected = new GenericRow(Arrays.asList(null, null, "ITEM_1", 2, /**
* 2 x items *
*/
20.0));
final Map<String, GenericRow> results = new HashMap<>();
TestUtils.waitForCondition(() -> {
final Map<Windowed<String>, GenericRow> windowedResults = testHarness.consumeData(streamName, resultSchema, 1, new TimeWindowedDeserializer<>(new StringDeserializer()), MAX_POLL_PER_ITERATION);
updateResults(results, windowedResults);
final GenericRow actual = results.get("ITEM_1");
return expected.equals(actual);
}, 60000, "didn't receive correct results within timeout");
AdminClient adminClient = AdminClient.create(testHarness.ksqlConfig.getKsqlStreamConfigProps());
KafkaTopicClient topicClient = new KafkaTopicClientImpl(adminClient);
Set<String> topicBeforeCleanup = topicClient.listTopicNames();
assertThat("Expected to have 5 topics instead have : " + topicBeforeCleanup.size(), topicBeforeCleanup.size(), equalTo(5));
QueryMetadata queryMetadata = ksqlContext.getRunningQueries().iterator().next();
queryMetadata.close();
Set<String> topicsAfterCleanUp = topicClient.listTopicNames();
assertThat("Expected to see 3 topics after clean up but seeing " + topicsAfterCleanUp.size(), topicsAfterCleanUp.size(), equalTo(3));
assertThat(topicClient.getTopicCleanupPolicy(streamName), equalTo(KafkaTopicClient.TopicCleanupPolicy.DELETE));
}
use of io.confluent.ksql.GenericRow 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);
}
Aggregations