Search in sources :

Example 86 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project apache-kafka-on-k8s by banzaicloud.

the class WorkerSinkTask method convertMessages.

private void convertMessages(ConsumerRecords<byte[], byte[]> msgs) {
    origOffsets.clear();
    for (ConsumerRecord<byte[], byte[]> msg : msgs) {
        log.trace("{} Consuming and converting message in topic '{}' partition {} at offset {} and timestamp {}", this, msg.topic(), msg.partition(), msg.offset(), msg.timestamp());
        SchemaAndValue keyAndSchema = keyConverter.toConnectData(msg.topic(), msg.key());
        SchemaAndValue valueAndSchema = valueConverter.toConnectData(msg.topic(), msg.value());
        Headers headers = convertHeadersFor(msg);
        Long timestamp = ConnectUtils.checkAndConvertTimestamp(msg.timestamp());
        SinkRecord origRecord = new SinkRecord(msg.topic(), msg.partition(), keyAndSchema.schema(), keyAndSchema.value(), valueAndSchema.schema(), valueAndSchema.value(), msg.offset(), timestamp, msg.timestampType(), headers);
        log.trace("{} Applying transformations to record in topic '{}' partition {} at offset {} and timestamp {} with key {} and value {}", this, msg.topic(), msg.partition(), msg.offset(), timestamp, keyAndSchema.value(), valueAndSchema.value());
        SinkRecord transRecord = transformationChain.apply(origRecord);
        origOffsets.put(new TopicPartition(origRecord.topic(), origRecord.kafkaPartition()), new OffsetAndMetadata(origRecord.kafkaOffset() + 1));
        if (transRecord != null) {
            messageBatch.add(transRecord);
        } else {
            log.trace("{} Transformations returned null, so dropping record in topic '{}' partition {} at offset {} and timestamp {} with key {} and value {}", this, msg.topic(), msg.partition(), msg.offset(), timestamp, keyAndSchema.value(), valueAndSchema.value());
        }
    }
    sinkTaskMetricsGroup.recordConsumedOffsets(origOffsets);
}
Also used : ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) Headers(org.apache.kafka.connect.header.Headers) TopicPartition(org.apache.kafka.common.TopicPartition) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue)

Example 87 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project apache-kafka-on-k8s by banzaicloud.

the class KafkaStatusBackingStore method parseConnectorStatus.

private ConnectorStatus parseConnectorStatus(String connector, byte[] data) {
    try {
        SchemaAndValue schemaAndValue = converter.toConnectData(topic, data);
        if (!(schemaAndValue.value() instanceof Map)) {
            log.error("Invalid connector status type {}", schemaAndValue.value().getClass());
            return null;
        }
        @SuppressWarnings("unchecked") Map<String, Object> statusMap = (Map<String, Object>) schemaAndValue.value();
        TaskStatus.State state = TaskStatus.State.valueOf((String) statusMap.get(STATE_KEY_NAME));
        String trace = (String) statusMap.get(TRACE_KEY_NAME);
        String workerUrl = (String) statusMap.get(WORKER_ID_KEY_NAME);
        int generation = ((Long) statusMap.get(GENERATION_KEY_NAME)).intValue();
        return new ConnectorStatus(connector, state, trace, workerUrl, generation);
    } catch (Exception e) {
        log.error("Failed to deserialize connector status", e);
        return null;
    }
}
Also used : ConnectorStatus(org.apache.kafka.connect.runtime.ConnectorStatus) TaskStatus(org.apache.kafka.connect.runtime.TaskStatus) HashMap(java.util.HashMap) Map(java.util.Map) RetriableException(org.apache.kafka.common.errors.RetriableException) ConfigException(org.apache.kafka.common.config.ConfigException) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue)

Example 88 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project apache-kafka-on-k8s by banzaicloud.

the class JsonConverterTest method timeToConnectWithDefaultValue.

@Test
public void timeToConnectWithDefaultValue() {
    java.util.Date reference = new java.util.Date(0);
    Schema schema = Time.builder().defaultValue(reference).schema();
    String msg = "{ \"schema\": { \"type\": \"int32\", \"name\": \"org.apache.kafka.connect.data.Time\", \"version\": 1, \"default\": 0 }, \"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) Date(org.apache.kafka.connect.data.Date) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 89 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project apache-kafka-on-k8s by banzaicloud.

the class JsonConverterTest method timeToConnectOptionalWithDefaultValue.

@Test
public void timeToConnectOptionalWithDefaultValue() {
    java.util.Date reference = new java.util.Date(0);
    Schema schema = Time.builder().optional().defaultValue(reference).schema();
    String msg = "{ \"schema\": { \"type\": \"int32\", \"name\": \"org.apache.kafka.connect.data.Time\", \"version\": 1, \"optional\": true, \"default\": 0 }, \"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) Date(org.apache.kafka.connect.data.Date) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) Test(org.junit.Test)

Example 90 with SchemaAndValue

use of org.apache.kafka.connect.data.SchemaAndValue in project apache-kafka-on-k8s by banzaicloud.

the class JsonConverterTest method timestampToConnectOptional.

@Test
public void timestampToConnectOptional() {
    Schema schema = Timestamp.builder().optional().schema();
    String msg = "{ \"schema\": { \"type\": \"int64\", \"name\": \"org.apache.kafka.connect.data.Timestamp\", \"version\": 1, \"optional\": true }, \"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)

Aggregations

SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)140 Test (org.junit.Test)57 Schema (org.apache.kafka.connect.data.Schema)49 Test (org.junit.jupiter.api.Test)46 HashMap (java.util.HashMap)32 Struct (org.apache.kafka.connect.data.Struct)21 Date (org.apache.kafka.connect.data.Date)18 BigInteger (java.math.BigInteger)12 Map (java.util.Map)12 ConnectorStatus (org.apache.kafka.connect.runtime.ConnectorStatus)11 BigDecimal (java.math.BigDecimal)10 TopicPartition (org.apache.kafka.common.TopicPartition)9 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)8 TaskStatus (org.apache.kafka.connect.runtime.TaskStatus)8 Callback (org.apache.kafka.clients.producer.Callback)7 SinkRecord (org.apache.kafka.connect.sink.SinkRecord)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 Collection (java.util.Collection)6 GregorianCalendar (java.util.GregorianCalendar)6 LinkedHashMap (java.util.LinkedHashMap)6