org.apache.kafka.common.errors.SerializationException Java Examples

The following examples show how to use org.apache.kafka.common.errors.SerializationException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KsqlDelimitedSerializer.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 8 votes vote down vote up
@Override
public byte[] serialize(final String topic, final GenericRow genericRow) {
  if (genericRow == null) {
    return null;
  }
  try {
    StringWriter stringWriter = new StringWriter();
    CSVPrinter csvPrinter = new CSVPrinter(stringWriter, CSVFormat.DEFAULT);
    csvPrinter.printRecord(genericRow.getColumns());
    String result = stringWriter.toString();
    return result.substring(0, result.length() - 2).getBytes(StandardCharsets.UTF_8);
  } catch (Exception e) {
    throw new SerializationException("Error serializing CSV message", e);
  }

}
 
Example #2
Source File: KsqlGenericRowAvroSerializer.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(final String topic, final GenericRow genericRow) {
  if (genericRow == null) {
    return null;
  }
  try {
    GenericRecord avroRecord = new GenericData.Record(avroSchema);
    for (int i = 0; i < genericRow.getColumns().size(); i++) {
      Schema schema = getNonNullSchema(fields.get(i).schema());
      if (schema.getType() == Schema.Type.ARRAY) {
        if (genericRow.getColumns().get(i) != null) {
          avroRecord.put(
              fields.get(i).name(),
              Arrays.asList((Object[]) genericRow.getColumns().get(i))
          );
        }
      } else {
        avroRecord.put(fields.get(i).name(), genericRow.getColumns().get(i));
      }
    }
    return kafkaAvroSerializer.serialize(topic, avroRecord);
  } catch (Exception e) {
    throw new SerializationException(e);
  }
}
 
Example #3
Source File: KopProtocolHandlerTestBase.java    From kop with Apache License 2.0 6 votes vote down vote up
public static Integer kafkaIntDeserialize(byte[] data) {
    if (data == null) {
        return null;
    }

    if (data.length != 4) {
        throw new SerializationException("Size of data received by IntegerDeserializer is not 4");
    }

    int value = 0;
    for (byte b : data) {
        value <<= 8;
        value |= b & 0xFF;
    }
    return value;
}
 
Example #4
Source File: ConsumerSkipRecordsSerializationExceptionHandler.java    From apicurio-registry with Apache License 2.0 6 votes vote down vote up
@Override
public void accept(Consumer<?, ?> consumer, RuntimeException e) {
    if (e instanceof SerializationException) {
        Matcher m = EXCEPTION_PATTERN.matcher(e.getMessage());
        if (m.find()) {
            String topic = m.group(1);
            int partition = Integer.parseInt(m.group(2));
            long errorOffset = Long.parseLong(m.group(3));
            TopicPartition tp = new TopicPartition(topic, partition);
            long currentOffset = consumer.position(tp);
            log.error("SerializationException - skipping records in partition {} from offset {} up to and including error offset {}",
                    tp, currentOffset, errorOffset, e);
            consumer.seek(tp, errorOffset + 1);
        } else {
            super.accept(consumer, e);
        }
    } else {
        super.accept(consumer, e);
    }
}
 
Example #5
Source File: TopicPartitionSerializer.java    From kafka-graphs with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(String topic, TopicPartition data) {
    if (data == null) {
        return null;
    }
    try {
        byte[] topicBytes = data.topic().getBytes(ENCODING);

        ByteBuffer buf = ByteBuffer.allocate(ARRAY_LENGTH_SIZE + topicBytes.length
            + PARTITION_SIZE);
        buf.putInt(topicBytes.length);
        buf.put(topicBytes);
        buf.putInt(data.partition());
        return buf.array();
    } catch (UnsupportedEncodingException e) {
        throw new SerializationException("Error when serializing string to byte[]");
    }
}
 
Example #6
Source File: TopicPartitionDeserializer.java    From kafka-graphs with Apache License 2.0 6 votes vote down vote up
@Override
public TopicPartition deserialize(String topic, byte[] data) {
    if (data == null || data.length == 0) {
        return null;
    }
    try {
        ByteBuffer buf = ByteBuffer.wrap(data);
        int topicLength = buf.getInt();
        byte[] topicBytes = new byte[topicLength];
        buf.get(topicBytes);
        String otherTopic = new String(topicBytes, ENCODING);
        int partition = buf.getInt();

        return new TopicPartition(otherTopic, partition);
    } catch (UnsupportedEncodingException e) {
        throw new SerializationException("Error when deserializing byte[] to string");
    }
}
 
Example #7
Source File: KafkaValueSerializer.java    From kareldb with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(String topic, NavigableMap<Long, VersionedValue> object) {
    if (object == null) {
        return null;
    }
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        out.write(MAGIC_BYTE);
        out.write(ByteBuffer.allocate(VERSION_SIZE).putInt(version).array());
        BinaryEncoder encoder = encoderFactory.directBinaryEncoder(out, null);
        writer.write(toArray(object), encoder);
        encoder.flush();
        byte[] bytes = out.toByteArray();
        out.close();
        return bytes;
    } catch (IOException | RuntimeException e) {
        // avro serialization can throw AvroRuntimeException, NullPointerException,
        // ClassCastException, etc
        LOG.error("Error serializing Avro value " + e.getMessage());
        throw new SerializationException("Error serializing Avro value", e);
    }
}
 
Example #8
Source File: KafkaKeySerializer.java    From kareldb with Apache License 2.0 6 votes vote down vote up
@Override
public byte[] serialize(String topic, Comparable[] object) {
    if (object == null) {
        return null;
    }
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        BinaryEncoder encoder = encoderFactory.directBinaryEncoder(out, null);
        writer.write(toRecord(object), encoder);
        encoder.flush();
        byte[] bytes = out.toByteArray();
        out.close();
        return bytes;
    } catch (IOException | RuntimeException e) {
        // avro serialization can throw AvroRuntimeException, NullPointerException,
        // ClassCastException, etc
        LOG.error("Error serializing Avro key " + e.getMessage());
        throw new SerializationException("Error serializing Avro key", e);
    }
}
 
Example #9
Source File: DefaultKafkaListenerExceptionHandler.java    From micronaut-kafka with Apache License 2.0 6 votes vote down vote up
/**
 * Seeks past a serialization exception if an error occurs.
 * @param cause The cause
 * @param consumerBean The consumer bean
 * @param kafkaConsumer The kafka consumer
 */
protected void seekPastDeserializationError(
        @Nonnull SerializationException cause,
        @Nonnull Object consumerBean,
        @Nonnull Consumer<?, ?> kafkaConsumer) {
    try {
        final String message = cause.getMessage();
        final Matcher matcher = SERIALIZATION_EXCEPTION_MESSAGE_PATTERN.matcher(message);
        if (matcher.find()) {
            final String topic = matcher.group(1);
            final int partition = Integer.valueOf(matcher.group(2));
            final int offset = Integer.valueOf(matcher.group(3));
            TopicPartition tp = new TopicPartition(topic, partition);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Seeking past unserializable consumer record for partition {}-{} and offset {}", topic, partition, offset);
            }
            kafkaConsumer.seek(tp, offset + 1);
        }
    } catch (Throwable e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Kafka consumer [" + consumerBean + "] failed to seek past unserializable value: " + e.getMessage(), e);
        }
    }
}
 
Example #10
Source File: DefaultKafkaListenerExceptionHandler.java    From micronaut-kafka with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(KafkaListenerException exception) {
    final Throwable cause = exception.getCause();
    final Object consumerBean = exception.getKafkaListener();
    if (cause instanceof SerializationException) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Kafka consumer [" + consumerBean + "] failed to deserialize value: " + cause.getMessage(), cause);
        }

        if (skipRecordOnDeserializationFailure) {
            final Consumer<?, ?> kafkaConsumer = exception.getKafkaConsumer();
            seekPastDeserializationError((SerializationException) cause, consumerBean, kafkaConsumer);
        }
    } else {
        if (LOG.isErrorEnabled()) {
            Optional<ConsumerRecord<?, ?>> consumerRecord = exception.getConsumerRecord();
            if (consumerRecord.isPresent()) {
                LOG.error("Error processing record [" + consumerRecord + "] for Kafka consumer [" + consumerBean + "] produced error: " + cause.getMessage(), cause);

            } else {
                LOG.error("Kafka consumer [" + consumerBean + "] produced error: " + cause.getMessage(), cause);
            }
        }
    }
}
 
Example #11
Source File: JsonDeserializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Deserialize to a JsonNode
 * @param topic topic name
 * @param data message bytes
 * @return JsonNode
 */
@Override
public JsonNode deserialize(String topic, byte[] data) {
    if(data==null){
        return null;
    }
    try{
        return objectMapper.readTree(data);
    }catch (Exception e){
        throw new SerializationException(e);
    }
}
 
Example #12
Source File: JsonDeserializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Deserialize to a POJO
 *
 * @param topic topic name
 * @param data  message bytes
 * @return Specific Java Object
 */
@Override
public T deserialize(String topic, byte[] data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.readValue(data, className);
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}
 
Example #13
Source File: JsonSerializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Serialize Java Object
 *
 * @param topic Kafka topic name
 * @param data  data as Java Object
 * @return byte[]
 */
@Override
public byte[] serialize(String topic, T data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);
    } catch (Exception e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}
 
Example #14
Source File: JsonDeserializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Deserialize to a POJO
 *
 * @param topic topic name
 * @param data  message bytes
 * @return Specific Java Object
 */
@Override
public T deserialize(String topic, byte[] data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.readValue(data, className);
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}
 
Example #15
Source File: JsonSerializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Serialize Java Object
 *
 * @param topic Kafka topic name
 * @param data  data as Java Object
 * @return byte[]
 */
@Override
public byte[] serialize(String topic, T data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);
    } catch (Exception e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}
 
Example #16
Source File: JsonSerializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Serialize Java Object
 *
 * @param topic Kafka topic name
 * @param data  data as Java Object
 * @return byte[]
 */
@Override
public byte[] serialize(String topic, T data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);
    } catch (Exception e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}
 
Example #17
Source File: JsonSerializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Serialize Java Object
 *
 * @param topic Kafka topic name
 * @param data  data as Java Object
 * @return byte[]
 */
@Override
public byte[] serialize(String topic, T data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);
    } catch (Exception e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}
 
Example #18
Source File: JsonDeserializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Deserialize to a POJO
 *
 * @param topic topic name
 * @param data  message bytes
 * @return Specific Java Object
 */
@Override
public T deserialize(String topic, byte[] data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.readValue(data, className);
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}
 
Example #19
Source File: JsonDeserializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Deserialize to a POJO
 *
 * @param topic topic name
 * @param data  message bytes
 * @return Specific Java Object
 */
@Override
public T deserialize(String topic, byte[] data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.readValue(data, className);
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}
 
Example #20
Source File: JsonSerializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Serialize Java Object
 *
 * @param topic Kafka topic name
 * @param data  data as Java Object
 * @return byte[]
 */
@Override
public byte[] serialize(String topic, T data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);
    } catch (Exception e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}
 
Example #21
Source File: JsonDeserializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Deserialize to a POJO
 *
 * @param topic topic name
 * @param data  message bytes
 * @return Specific Java Object
 */
@Override
public T deserialize(String topic, byte[] data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.readValue(data, className);
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}
 
Example #22
Source File: JsonSerializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Serialize Java Object
 *
 * @param topic Kafka topic name
 * @param data  data as Java Object
 * @return byte[]
 */
@Override
public byte[] serialize(String topic, T data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);
    } catch (Exception e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}
 
Example #23
Source File: JsonSerializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Serialize JsonNode
 *
 * @param topic Kafka topic name
 * @param data  data as JsonNode
 * @return byte[]
 */
@Override
public byte[] serialize(String topic, JsonNode data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);
    } catch (JsonProcessingException e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}
 
Example #24
Source File: JsonDeserializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Deserialize to a POJO
 *
 * @param topic topic name
 * @param data  message bytes
 * @return Specific Java Object
 */
@Override
public T deserialize(String topic, byte[] data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.readValue(data, className);
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}
 
Example #25
Source File: JsonSerializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Serialize Java Object
 *
 * @param topic Kafka topic name
 * @param data  data as Java Object
 * @return byte[]
 */
@Override
public byte[] serialize(String topic, T data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);
    } catch (Exception e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}
 
Example #26
Source File: JsonDeserializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Deserialize to a POJO
 *
 * @param topic topic name
 * @param data  message bytes
 * @return Specific Java Object
 */
@Override
public T deserialize(String topic, byte[] data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.readValue(data, className);
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}
 
Example #27
Source File: JsonSerializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Serialize Java Object
 *
 * @param topic Kafka topic name
 * @param data  data as Java Object
 * @return byte[]
 */
@Override
public byte[] serialize(String topic, T data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);
    } catch (Exception e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}
 
Example #28
Source File: JsonDeserializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Deserialize to a POJO
 *
 * @param topic topic name
 * @param data  message bytes
 * @return Specific Java Object
 */
@Override
public T deserialize(String topic, byte[] data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.readValue(data, className);
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}
 
Example #29
Source File: JsonSerializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Serialize Java Object
 *
 * @param topic Kafka topic name
 * @param data  data as Java Object
 * @return byte[]
 */
@Override
public byte[] serialize(String topic, T data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.writeValueAsBytes(data);
    } catch (Exception e) {
        throw new SerializationException("Error serializing JSON message", e);
    }
}
 
Example #30
Source File: JsonDeserializer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Deserialize to a POJO
 *
 * @param topic topic name
 * @param data  message bytes
 * @return Specific Java Object
 */
@Override
public T deserialize(String topic, byte[] data) {
    if (data == null) {
        return null;
    }
    try {
        return objectMapper.readValue(data, className);
    } catch (Exception e) {
        throw new SerializationException(e);
    }
}