Search in sources :

Example 11 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project kafka by apache.

the class JsonConverterTest method decimalToConnect.

@Test
public void decimalToConnect() {
    Schema schema = Decimal.schema(2);
    BigDecimal reference = new BigDecimal(new BigInteger("156"), 2);
    // Payload is base64 encoded byte[]{0, -100}, which is the two's complement encoding of 156.
    String msg = "{ \"schema\": { \"type\": \"bytes\", \"name\": \"org.apache.kafka.connect.data.Decimal\", \"version\": 1, \"parameters\": { \"scale\": \"2\" } }, \"payload\": \"AJw=\" }";
    SchemaAndValue schemaAndValue = converter.toConnectData(TOPIC, msg.getBytes());
    BigDecimal converted = (BigDecimal) schemaAndValue.value();
    assertEquals(schema, schemaAndValue.schema());
    assertEquals(reference, converted);
}
Also used : Schema(org.apache.kafka.connect.data.Schema) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 12 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project kafka by apache.

the class JsonConverterTest method decimalToConnectWithDefaultValue.

@Test
public void decimalToConnectWithDefaultValue() {
    BigDecimal reference = new BigDecimal(new BigInteger("156"), 2);
    Schema schema = Decimal.builder(2).defaultValue(reference).build();
    String msg = "{ \"schema\": { \"type\": \"bytes\", \"name\": \"org.apache.kafka.connect.data.Decimal\", \"version\": 1, \"default\": \"AJw=\", \"parameters\": { \"scale\": \"2\" } }, \"payload\": null }";
    SchemaAndValue schemaAndValue = converter.toConnectData(TOPIC, msg.getBytes());
    assertEquals(schema, schemaAndValue.schema());
    assertEquals(reference, schemaAndValue.value());
}
Also used : Schema(org.apache.kafka.connect.data.Schema) BigInteger(java.math.BigInteger) BigDecimal(java.math.BigDecimal) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 13 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project kafka by apache.

the class JsonConverterTest method nullSchemaPrimitiveToConnect.

@Test
public void nullSchemaPrimitiveToConnect() {
    SchemaAndValue converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": null }".getBytes());
    assertEquals(SchemaAndValue.NULL, converted);
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": true }".getBytes());
    assertEquals(new SchemaAndValue(null, true), converted);
    // Integers: Connect has more data types, and JSON unfortunately mixes all number types. We try to preserve
    // info as best we can, so we always use the largest integer and floating point numbers we can and have Jackson
    // determine if it's an integer or not
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": 12 }".getBytes());
    assertEquals(new SchemaAndValue(null, 12L), converted);
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": 12.24 }".getBytes());
    assertEquals(new SchemaAndValue(null, 12.24), converted);
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": \"a string\" }".getBytes());
    assertEquals(new SchemaAndValue(null, "a string"), converted);
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": [1, \"2\", 3] }".getBytes());
    assertEquals(new SchemaAndValue(null, Arrays.asList(1L, "2", 3L)), converted);
    converted = converter.toConnectData(TOPIC, "{ \"schema\": null, \"payload\": { \"field1\": 1, \"field2\": 2} }".getBytes());
    Map<String, Long> obj = new HashMap<>();
    obj.put("field1", 1L);
    obj.put("field2", 2L);
    assertEquals(new SchemaAndValue(null, obj), converted);
}
Also used : HashMap(java.util.HashMap) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 14 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project kafka by apache.

the class JsonConverterTest method decimalToConnectOptional.

@Test
public void decimalToConnectOptional() {
    Schema schema = Decimal.builder(2).optional().schema();
    String msg = "{ \"schema\": { \"type\": \"bytes\", \"name\": \"org.apache.kafka.connect.data.Decimal\", \"version\": 1, \"optional\": true, \"parameters\": { \"scale\": \"2\" } }, \"payload\": null }";
    SchemaAndValue schemaAndValue = converter.toConnectData(TOPIC, msg.getBytes());
    assertEquals(schema, schemaAndValue.schema());
    assertNull(schemaAndValue.value());
}
Also used : Schema(org.apache.kafka.connect.data.Schema) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 15 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project kafka by apache.

the class JsonConverterTest method noSchemaToConnect.

@Test
public void noSchemaToConnect() {
    Map<String, Boolean> props = Collections.singletonMap("schemas.enable", false);
    converter.configure(props, true);
    assertEquals(new SchemaAndValue(null, true), converter.toConnectData(TOPIC, "true".getBytes()));
}
Also used : SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Aggregations

SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)46 Test (org.junit.Test)36 Schema (org.apache.kafka.connect.data.Schema)19 HashMap (java.util.HashMap)14 Date (org.apache.kafka.connect.data.Date)9 Struct (org.apache.kafka.connect.data.Struct)6 BigInteger (java.math.BigInteger)5 Map (java.util.Map)5 MockTime (org.apache.kafka.common.utils.MockTime)5 ConnectorStatus (org.apache.kafka.connect.runtime.ConnectorStatus)5 EasyMock.anyObject (org.easymock.EasyMock.anyObject)5 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)4 BigDecimal (java.math.BigDecimal)3 Collection (java.util.Collection)3 GregorianCalendar (java.util.GregorianCalendar)3 ConsumerRecords (org.apache.kafka.clients.consumer.ConsumerRecords)3 TopicPartition (org.apache.kafka.common.TopicPartition)3 TaskStatus (org.apache.kafka.connect.runtime.TaskStatus)3 ByteBuffer (java.nio.ByteBuffer)2 LinkedHashMap (java.util.LinkedHashMap)2