Search in sources :

Example 31 with GenericRow

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

the class KsqlDelimitedSerializerTest method shouldSerializeRowWithNull.

@Test
public void shouldSerializeRowWithNull() {
    List columns = Arrays.asList(1511897796092L, 1L, "item_1", null);
    GenericRow genericRow = new GenericRow(columns);
    KsqlDelimitedSerializer ksqlDelimitedSerializer = new KsqlDelimitedSerializer(orderSchema);
    byte[] bytes = ksqlDelimitedSerializer.serialize("t1", genericRow);
    String delimitedString = new String(bytes);
    assertThat("Incorrect serialization.", delimitedString, equalTo("1511897796092,1,item_1,"));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) List(java.util.List) Test(org.junit.Test)

Example 32 with GenericRow

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

the class KsqlJsonDeserializerTest method shouldDeserializeJsonCorrectlyWithRedundantFields.

@Test
public void shouldDeserializeJsonCorrectlyWithRedundantFields() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    Map<String, Object> orderRow = new HashMap<>();
    orderRow.put("ordertime", 1511897796092L);
    orderRow.put("@orderid", 1L);
    orderRow.put("itemid", "Item_1");
    orderRow.put("orderunits", 10.0);
    orderRow.put("arraycol", new Double[] { 10.0, 20.0 });
    orderRow.put("mapcol", Collections.singletonMap("key1", 10.0));
    byte[] jsonBytes = objectMapper.writeValueAsBytes(orderRow);
    Schema newOrderSchema = SchemaBuilder.struct().field("ordertime".toUpperCase(), org.apache.kafka.connect.data.Schema.INT64_SCHEMA).field("orderid".toUpperCase(), org.apache.kafka.connect.data.Schema.INT64_SCHEMA).field("itemid".toUpperCase(), org.apache.kafka.connect.data.Schema.STRING_SCHEMA).field("orderunits".toUpperCase(), org.apache.kafka.connect.data.Schema.FLOAT64_SCHEMA).build();
    KsqlJsonDeserializer ksqlJsonDeserializer = new KsqlJsonDeserializer(newOrderSchema);
    GenericRow genericRow = ksqlJsonDeserializer.deserialize("", jsonBytes);
    assertThat(genericRow.getColumns().size(), equalTo(4));
    assertThat((Long) genericRow.getColumns().get(0), equalTo(1511897796092L));
    assertThat((Long) genericRow.getColumns().get(1), equalTo(1L));
    assertThat(((String) genericRow.getColumns().get(2)), equalTo("Item_1"));
    assertThat((Double) genericRow.getColumns().get(3), equalTo(10.0));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) HashMap(java.util.HashMap) Schema(org.apache.kafka.connect.data.Schema) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 33 with GenericRow

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

the class KsqlJsonDeserializerTest method shouldTreatNullAsNull.

@Test
public void shouldTreatNullAsNull() throws JsonProcessingException {
    final ObjectMapper objectMapper = new ObjectMapper();
    final KsqlJsonDeserializer deserializer = new KsqlJsonDeserializer(orderSchema);
    Map<String, Object> row = new HashMap<>();
    row.put("ordertime", null);
    row.put("@orderid", null);
    row.put("itemid", null);
    row.put("orderunits", null);
    row.put("arrayCol", new Double[] { 0.0, null });
    row.put("mapCol", null);
    final GenericRow expected = new GenericRow(Arrays.asList(null, null, null, null, new Double[] { 0.0, null }, null));
    GenericRow genericRow = deserializer.deserialize("", objectMapper.writeValueAsBytes(row));
    assertThat(genericRow, equalTo(expected));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 34 with GenericRow

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

the class KsqlJsonDeserializerTest method shouldDeserializeEvenWithMissingFields.

@Test
public void shouldDeserializeEvenWithMissingFields() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    Map<String, Object> orderRow = new HashMap<>();
    orderRow.put("ordertime", 1511897796092L);
    orderRow.put("@orderid", 1L);
    orderRow.put("itemid", "Item_1");
    orderRow.put("orderunits", 10.0);
    byte[] jsonBytes = objectMapper.writeValueAsBytes(orderRow);
    KsqlJsonDeserializer ksqlJsonDeserializer = new KsqlJsonDeserializer(orderSchema);
    GenericRow genericRow = ksqlJsonDeserializer.deserialize("", jsonBytes);
    assertThat(genericRow.getColumns().size(), equalTo(6));
    assertThat((Long) genericRow.getColumns().get(0), equalTo(1511897796092L));
    assertThat((Long) genericRow.getColumns().get(1), equalTo(1L));
    assertThat((String) genericRow.getColumns().get(2), equalTo("Item_1"));
    assertThat((Double) genericRow.getColumns().get(3), equalTo(10.0));
    Assert.assertNull(genericRow.getColumns().get(4));
    Assert.assertNull(genericRow.getColumns().get(5));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 35 with GenericRow

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

the class KsqlJsonDeserializerTest method shouldDeserializeJsonCorrectly.

@Test
public void shouldDeserializeJsonCorrectly() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    Map<String, Object> orderRow = new HashMap<>();
    orderRow.put("ordertime", 1511897796092L);
    orderRow.put("@orderid", 1L);
    orderRow.put("itemid", "Item_1");
    orderRow.put("orderunits", 10.0);
    orderRow.put("arraycol", new Double[] { 10.0, 20.0 });
    orderRow.put("mapcol", Collections.singletonMap("key1", 10.0));
    byte[] jsonBytes = objectMapper.writeValueAsBytes(orderRow);
    KsqlJsonDeserializer ksqlJsonDeserializer = new KsqlJsonDeserializer(orderSchema);
    GenericRow genericRow = ksqlJsonDeserializer.deserialize("", jsonBytes);
    assertThat(genericRow.getColumns().size(), equalTo(6));
    assertThat((Long) genericRow.getColumns().get(0), equalTo(1511897796092L));
    assertThat((Long) genericRow.getColumns().get(1), equalTo(1L));
    assertThat(((String) genericRow.getColumns().get(2)), equalTo("Item_1"));
    assertThat((Double) genericRow.getColumns().get(3), equalTo(10.0));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) HashMap(java.util.HashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

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