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