Search in sources :

Example 36 with GenericRow

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);
}
Also used : GenericRow(io.confluent.ksql.GenericRow) Field(org.apache.kafka.connect.data.Field) ArrayList(java.util.ArrayList) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayList(java.util.ArrayList) List(java.util.List)

Example 37 with GenericRow

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");
}
Also used : DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) ArrayList(java.util.ArrayList) GenericRow(io.confluent.ksql.GenericRow) PlanNode(io.confluent.ksql.planner.plan.PlanNode) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) KsqlTopicSerDe(io.confluent.ksql.serde.KsqlTopicSerDe) KsqlJsonTopicSerDe(io.confluent.ksql.serde.json.KsqlJsonTopicSerDe) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Test(org.junit.Test)

Example 38 with GenericRow

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");
}
Also used : DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) ArrayList(java.util.ArrayList) GenericRow(io.confluent.ksql.GenericRow) PlanNode(io.confluent.ksql.planner.plan.PlanNode) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) KsqlTopicSerDe(io.confluent.ksql.serde.KsqlTopicSerDe) KsqlJsonTopicSerDe(io.confluent.ksql.serde.json.KsqlJsonTopicSerDe) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) Test(org.junit.Test)

Example 39 with GenericRow

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))));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) Test(org.junit.Test)

Example 40 with GenericRow

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;
}
Also used : GenericRow(io.confluent.ksql.GenericRow) HashMap(java.util.HashMap)

Aggregations

GenericRow (io.confluent.ksql.GenericRow)65 Test (org.junit.Test)38 HashMap (java.util.HashMap)27 Schema (org.apache.kafka.connect.data.Schema)19 List (java.util.List)15 StringDeserializer (org.apache.kafka.common.serialization.StringDeserializer)15 ArrayList (java.util.ArrayList)11 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)9 IntegrationTest (io.confluent.common.utils.IntegrationTest)8 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 KsqlTopicSerDe (io.confluent.ksql.serde.KsqlTopicSerDe)5 KafkaTopicClient (io.confluent.ksql.util.KafkaTopicClient)5 KafkaTopicClientImpl (io.confluent.ksql.util.KafkaTopicClientImpl)5 KsqlConfig (io.confluent.ksql.util.KsqlConfig)5 Map (java.util.Map)5 GenericRecord (org.apache.avro.generic.GenericRecord)4 Windowed (org.apache.kafka.streams.kstream.Windowed)4 KafkaAvroDeserializer (io.confluent.kafka.serializers.KafkaAvroDeserializer)3 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)3