在Kafka Streams API中使用ArrayList Serde的问题

时间:2017-09-22 13:23:59

标签: java apache-kafka apache-kafka-streams

根据我之前的question,我仍在尝试弄清楚代码的问题。

我有一个最基本的话题:键和值是Long的类型,这是我的制作人代码:

public class DemoProducer {
  public static void main(String... args) {
    Producer<Long, Long> producer = new KafkaProducer<>(createProperties());

    LongStream.range(1, 100)
        .forEach(
            i ->
                LongStream.range(100, 115)
                    .forEach(j -> producer.send(new ProducerRecord<>("test", i, j))));

    producer.close();
  }

  private static final Properties createProperties() {
    final Properties props = new Properties();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "broker:9092");
    props.put(ProducerConfig.ACKS_CONFIG, "all");
    props.put(ProducerConfig.RETRIES_CONFIG, 0);
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, LongSerializer.class.getName());
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, LongSerializer.class.getName());

    return props;
  }
}

我想使用Kafka Streams API使用key和put值在ArrayList中进行分组。

这是我的Stream应用程序,它应该进行转换并将内容添加到新主题中 - test-aggregated

public class DemoStreams {
  public static void main(String... args) {
    final Serde<Long> longSerde = Serdes.Long();

    KStreamBuilder builder = new KStreamBuilder();

    builder
        .stream(longSerde, longSerde, "test")
        .groupByKey(longSerde, longSerde)
        .aggregate(
            ArrayList::new,
            (subscriberId, reportId, queue) -> {
              queue.add(reportId);
              return queue;
            },
            new ArrayListSerde<>(longSerde))
        .to(longSerde, new ArrayListSerde<>(longSerde), "test-aggregated");

    final KafkaStreams streams = new KafkaStreams(builder, createProperties());

    streams.cleanUp();
    streams.start();

    Runtime.getRuntime().addShutdownHook(new Thread(streams::close));
  }

  private static Properties createProperties() {
    final Properties properties = new Properties();
    String longSerdes = Serdes.Long().getClass().getName();
    properties.put(StreamsConfig.APPLICATION_ID_CONFIG, "aggregation-app");
    properties.put(StreamsConfig.CLIENT_ID_CONFIG, "aggregation-app-client");
    properties.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "broker:9092");
    properties.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, longSerdes);
    properties.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, ArrayListSerde.class);
    properties.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 10 * 1000);
    properties.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);

    return properties;
  }
}

我已按照以下方式实施了我的Serde:

ArrayListSerde

public class ArrayListSerde<T> implements Serde<ArrayList<T>> {

  private final Serde<ArrayList<T>> inner;

  public ArrayListSerde(Serde<T> serde) {
    inner =
        Serdes.serdeFrom(
            new ArrayListSerializer<>(serde.serializer()),
            new ArrayListDeserializer<>(serde.deserializer()));
  }

  @Override
  public Serializer<ArrayList<T>> serializer() {
    return inner.serializer();
  }

  @Override
  public Deserializer<ArrayList<T>> deserializer() {
    return inner.deserializer();
  }

  @Override
  public void configure(Map<String, ?> configs, boolean isKey) {
    inner.serializer().configure(configs, isKey);
    inner.deserializer().configure(configs, isKey);
  }

  @Override
  public void close() {
    inner.serializer().close();
    inner.deserializer().close();
  }
}

ArrayListSerializer

public class ArrayListSerializer<T> implements Serializer<ArrayList<T>> {

  private Serializer<T> inner;

  public ArrayListSerializer(Serializer<T> inner) {
    this.inner = inner;
  }

  // Default constructor needed by Kafka
  public ArrayListSerializer() {}

  @Override
  public void configure(Map<String, ?> configs, boolean isKey) {
    // do nothing
  }

  @Override
  public byte[] serialize(String topic, ArrayList<T> queue) {
    final int size = queue.size();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final DataOutputStream dos = new DataOutputStream(baos);
    final Iterator<T> iterator = queue.iterator();
    try {
      dos.writeInt(size);
      while (iterator.hasNext()) {
        final byte[] bytes = inner.serialize(topic, iterator.next());
        dos.writeInt(bytes.length);
        dos.write(bytes);
      }
    } catch (IOException e) {
      throw new RuntimeException("Unable to serialize ArrayList", e);
    }
    return baos.toByteArray();
  }

  @Override
  public void close() {
    inner.close();
  }
}

ArrayListDeserializer

public class ArrayListDeserializer<T> implements Deserializer<ArrayList<T>> {
  private final Deserializer<T> valueDeserializer;

  public ArrayListDeserializer(final Deserializer<T> valueDeserializer) {
    this.valueDeserializer = valueDeserializer;
  }

  @Override
  public void configure(Map<String, ?> configs, boolean isKey) {
    // do nothing
  }

  @Override
  public ArrayList<T> deserialize(String topic, byte[] bytes) {
    if (bytes == null || bytes.length == 0) {
      return null;
    }

    final ArrayList<T> arrayList = new ArrayList<>();
    final DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(bytes));

    try {
      final int records = dataInputStream.readInt();
      for (int i = 0; i < records; i++) {
        final byte[] valueBytes = new byte[dataInputStream.readInt()];
        dataInputStream.read(valueBytes);
        arrayList.add(valueDeserializer.deserialize(topic, valueBytes));
      }
    } catch (IOException e) {
      throw new RuntimeException("Unable to deserialize ArrayList", e);
    }

    return arrayList;
  }

  @Override
  public void close() {
    // do nothing
  }
}

但是我最终得到了这个例外:

Exception in thread "permission-agg4-client-StreamThread-1" org.apache.kafka.streams.errors.StreamsException: stream-thread [aggregation-app-client-StreamThread-1] Failed to rebalance.
    at org.apache.kafka.streams.processor.internals.StreamThread.pollRequests(StreamThread.java:543)
    at org.apache.kafka.streams.processor.internals.StreamThread.runOnce(StreamThread.java:490)
    at org.apache.kafka.streams.processor.internals.StreamThread.runLoop(StreamThread.java:480)
    at org.apache.kafka.streams.processor.internals.StreamThread.run(StreamThread.java:457)
Caused by: org.apache.kafka.streams.errors.StreamsException: Failed to configure value serde class utils.ArrayListSerde
    at org.apache.kafka.streams.StreamsConfig.defaultValueSerde(StreamsConfig.java:770)
    at org.apache.kafka.streams.processor.internals.AbstractProcessorContext.<init>(AbstractProcessorContext.java:59)
    at org.apache.kafka.streams.processor.internals.ProcessorContextImpl.<init>(ProcessorContextImpl.java:40)
    at org.apache.kafka.streams.processor.internals.StreamTask.<init>(StreamTask.java:138)
    at org.apache.kafka.streams.processor.internals.StreamThread.createStreamTask(StreamThread.java:1078)
    at org.apache.kafka.streams.processor.internals.StreamThread$TaskCreator.createTask(StreamThread.java:255)
    at org.apache.kafka.streams.processor.internals.StreamThread$AbstractTaskCreator.createTasks(StreamThread.java:245)
    at org.apache.kafka.streams.processor.internals.StreamThread.addStreamTasks(StreamThread.java:1147)
    at org.apache.kafka.streams.processor.internals.StreamThread.access$800(StreamThread.java:68)
    at org.apache.kafka.streams.processor.internals.StreamThread$RebalanceListener.onPartitionsAssigned(StreamThread.java:184)
    at org.apache.kafka.clients.consumer.internals.ConsumerCoordinator.onJoinComplete(ConsumerCoordinator.java:265)
    at org.apache.kafka.clients.consumer.internals.AbstractCoordinator.joinGroupIfNeeded(AbstractCoordinator.java:367)
    at org.apache.kafka.clients.consumer.internals.AbstractCoordinator.ensureActiveGroup(AbstractCoordinator.java:316)
    at org.apache.kafka.clients.consumer.internals.ConsumerCoordinator.poll(ConsumerCoordinator.java:297)
    at org.apache.kafka.clients.consumer.KafkaConsumer.pollOnce(KafkaConsumer.java:1078)
    at org.apache.kafka.clients.consumer.KafkaConsumer.poll(KafkaConsumer.java:1043)
    at org.apache.kafka.streams.processor.internals.StreamThread.pollRequests(StreamThread.java:536)
    ... 3 more
Caused by: org.apache.kafka.common.KafkaException: Could not instantiate class utils.ArrayListSerde Does it have a public no-argument constructor?
    at org.apache.kafka.common.utils.Utils.newInstance(Utils.java:286)
    at org.apache.kafka.common.config.AbstractConfig.getConfiguredInstance(AbstractConfig.java:246)
    at org.apache.kafka.streams.StreamsConfig.defaultValueSerde(StreamsConfig.java:764)
    ... 19 more
Caused by: java.lang.InstantiationException: utils.ArrayListSerde
    at java.lang.Class.newInstance(Class.java:427)
    at org.apache.kafka.common.utils.Utils.newInstance(Utils.java:282)
    ... 21 more
Caused by: java.lang.NoSuchMethodException: utils.ArrayListSerde.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 22 more

我尝试根据Confluent的GitHub页面中的PriorityQueue示例实现Serde:https://github.com/confluentinc/kafka-streams-examples/tree/3.3.0-post/src/main/java/io/confluent/examples/streams/utils

1 个答案:

答案 0 :(得分:7)

如错误所示,所有Serde都需要一个非参数构造函数:

  

引起:org.apache.kafka.common.KafkaException:无法实例化类utils.ArrayListSerde它是否有公共无参数构造函数?

你的班级ArrayListSerde只有构造函数:

public ArrayListSerde(Serde<T> serde) { ... }

因此,我们得到了这个错误。

比较ArrayListSerializer

// Default constructor needed by Kafka
public ArrayListSerializer() {}