use of io.confluent.ksql.GenericRow in project ksql by confluentinc.
the class KsqlJsonDeserializer method getGenericRow.
@SuppressWarnings("unchecked")
private GenericRow getGenericRow(byte[] rowJsonBytes) throws IOException {
JsonNode jsonNode = objectMapper.readTree(rowJsonBytes);
CaseInsensitiveJsonNode caseInsensitiveJsonNode = new CaseInsensitiveJsonNode(jsonNode);
Map<String, String> keyMap = caseInsensitiveJsonNode.keyMap;
List columns = new ArrayList();
for (Field field : schema.fields()) {
String jsonFieldName = field.name().substring(field.name().indexOf(".") + 1);
JsonNode fieldJsonNode = jsonNode.get(keyMap.get(jsonFieldName));
if (fieldJsonNode == null) {
columns.add(null);
} else {
columns.add(enforceFieldType(field.schema(), fieldJsonNode));
}
}
return new GenericRow(columns);
}
use of io.confluent.ksql.GenericRow in project ksql by confluentinc.
the class SchemaKStreamTest method testGroupByKey.
@Test
public void testGroupByKey() {
String selectQuery = "SELECT col0, col1 FROM test1 WHERE col0 > 100;";
PlanNode logicalPlan = planBuilder.buildLogicalPlan(selectQuery);
initialSchemaKStream = new SchemaKStream(logicalPlan.getTheSourceNode().getSchema(), kStream, ksqlStream.getKeyField(), new ArrayList<>(), SchemaKStream.Type.SOURCE, functionRegistry, new MockSchemaRegistryClient());
Expression keyExpression = new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("TEST1")), "COL0");
KsqlTopicSerDe ksqlTopicSerDe = new KsqlJsonTopicSerDe();
Serde<GenericRow> rowSerde = ksqlTopicSerDe.getGenericRowSerde(initialSchemaKStream.getSchema(), null, false, null);
List<Expression> groupByExpressions = Arrays.asList(keyExpression);
SchemaKGroupedStream groupedSchemaKStream = initialSchemaKStream.groupBy(Serdes.String(), rowSerde, groupByExpressions);
Assert.assertEquals(groupedSchemaKStream.getKeyField().name(), "COL0");
}
use of io.confluent.ksql.GenericRow in project ksql by confluentinc.
the class SchemaKStreamTest method testGroupByMultipleColumns.
@Test
public void testGroupByMultipleColumns() {
String selectQuery = "SELECT col0, col1 FROM test1 WHERE col0 > 100;";
PlanNode logicalPlan = planBuilder.buildLogicalPlan(selectQuery);
initialSchemaKStream = new SchemaKStream(logicalPlan.getTheSourceNode().getSchema(), kStream, ksqlStream.getKeyField(), new ArrayList<>(), SchemaKStream.Type.SOURCE, functionRegistry, new MockSchemaRegistryClient());
Expression col0Expression = new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("TEST1")), "COL0");
Expression col1Expression = new DereferenceExpression(new QualifiedNameReference(QualifiedName.of("TEST1")), "COL1");
KsqlTopicSerDe ksqlTopicSerDe = new KsqlJsonTopicSerDe();
Serde<GenericRow> rowSerde = ksqlTopicSerDe.getGenericRowSerde(initialSchemaKStream.getSchema(), null, false, null);
List<Expression> groupByExpressions = Arrays.asList(col1Expression, col0Expression);
SchemaKGroupedStream groupedSchemaKStream = initialSchemaKStream.groupBy(Serdes.String(), rowSerde, groupByExpressions);
Assert.assertEquals(groupedSchemaKStream.getKeyField().name(), "TEST1.COL1|+|TEST1.COL0");
}
use of io.confluent.ksql.GenericRow in project ksql by confluentinc.
the class SelectValueMapperTest method shouldApplyUdfsToColumns.
@Test
public void shouldApplyUdfsToColumns() throws Exception {
final SelectValueMapper mapper = createMapper("SELECT col0, col1, col2, CEIL(col3) FROM test1 WHERE col0 > 100;");
final GenericRow row = mapper.apply(new GenericRow(Arrays.asList(2L, "foo", "whatever", 6.9F, "boo", "hoo")));
assertThat(row, equalTo(new GenericRow(Arrays.asList(2L, "foo", "whatever", 7.0F))));
}
use of io.confluent.ksql.GenericRow in project ksql by confluentinc.
the class PageViewDataProvider method buildData.
private Map<String, GenericRow> buildData() {
Map<String, GenericRow> dataMap = new HashMap<>();
// Create page view records with:
// key = page_id
// value = (view time, user_id, page_id)
dataMap.put("1", new GenericRow(Arrays.asList(1, "USER_1", "PAGE_1")));
dataMap.put("2", new GenericRow(Arrays.asList(2, "USER_2", "PAGE_2")));
dataMap.put("3", new GenericRow(Arrays.asList(3, "USER_4", "PAGE_3")));
dataMap.put("4", new GenericRow(Arrays.asList(4, "USER_3", "PAGE_4")));
dataMap.put("5", new GenericRow(Arrays.asList(5, "USER_0", "PAGE_5")));
// Duplicate page views from different users.
dataMap.put("6", new GenericRow(Arrays.asList(6, "USER_2", "PAGE_5")));
dataMap.put("7", new GenericRow(Arrays.asList(7, "USER_3", "PAGE_5")));
return dataMap;
}
Aggregations