Can a Kafka consumer(0.8.2.2) read messages in batch

后端 未结 3 504

As per my understanding Kafka consumer reads messages from an assigned partition sequentially...

We are planning to have multiple Kafka consumer (Java) which has sam

3条回答
  •  星月不相逢
    2020-12-16 07:32

    To achieve parallelism, which seems to be what you're asking, you use topic partitions (you split topic on N parts which are called partitions). Then, in the consumer, you spawn multiple threads to consume from those partitions.

    On the Producer side, you publish messages to random partition (default) or you provide Kafka with some message attribute to calculate hash (if ordering is required), which makes sure that all msgs with the same hash go to the same partition.

    EDIT (example of offset commit request):
    This is how I did it. All methods that are not provided are non-essential.

     /**
       * Commits the provided offset for the current client (i.e. unique topic/partition/clientName combination)
       * 
       * @param offset
       * @return {@code true} or {@code false}, depending on whether commit succeeded
       * @throws Exception
       */
      public static boolean commitOffset(String topic, int partition, String clientName, SimpleConsumer consumer,
          long offset) throws Exception {
        try {
          TopicAndPartition tap = new TopicAndPartition(topic, partition);
          OffsetAndMetadata offsetMetaAndErr = new OffsetAndMetadata(offset, OffsetAndMetadata.NoMetadata(), -1L);
          Map mapForCommitOffset = new HashMap<>(1);
          mapForCommitOffset.put(tap, offsetMetaAndErr);
    
          kafka.javaapi.OffsetCommitRequest offsetCommitReq = new kafka.javaapi.OffsetCommitRequest(
              ConsumerContext.getMainIndexingConsumerGroupId(), mapForCommitOffset, 1, clientName,
              ConsumerContext.getOffsetStorageType());
    
          OffsetCommitResponse offsetCommitResp = consumer.commitOffsets(offsetCommitReq);
          Short errCode = (Short) offsetCommitResp.errors().get(tap);
          if (errCode != 0) {
            processKafkaOffsetCommitError(tap, offsetCommitResp, BrokerInfo.of(consumer.host()));
            ErrorMapping.maybeThrowException(errCode);
          }
          LOG.debug("Successfully committed offset [{}].", offset);
        } catch (Exception e) {
          LOG.error("Error while committing offset [" + offset + "].", e);
          throw e;
        }
        return true;
      }
    

提交回复
热议问题