/** * Get a new set of properties for consumers that want to talk to this server. * * @param groupId the group ID for the consumer; may not be null * @param clientId the optional identifier for the client; may be null if not needed * @param autoOffsetReset how to pick a starting offset when there is no initial offset in ZooKeeper or if an offset is * out of range; may be null for the default to be used * @return the mutable consumer properties * @see #getProducerProperties(String) */ public Properties getConsumerProperties(String groupId, String clientId, OffsetResetStrategy autoOffsetReset) { if (groupId == null) throw new IllegalArgumentException("The groupId is required"); Properties props = new Properties(); props.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, brokerList()); props.setProperty(ConsumerConfig.GROUP_ID_CONFIG, groupId); props.setProperty(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, Boolean.FALSE.toString()); if (autoOffsetReset != null) { props.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, autoOffsetReset.toString().toLowerCase()); } if (clientId != null) props.setProperty(ConsumerConfig.CLIENT_ID_CONFIG, clientId); return props; }
config.getInt(ConsumerConfig.REQUEST_TIMEOUT_MS_CONFIG), OffsetResetStrategy offsetResetStrategy = OffsetResetStrategy.valueOf(config.getString(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG).toUpperCase(Locale.ROOT)); this.subscriptions = new SubscriptionState(offsetResetStrategy); this.assignors = config.getConfiguredInstances(
" must be provided in the config"); this.offsetResetStrategy = OffsetResetStrategy.valueOf(offsetResetStrategy.toUpperCase(Locale.ENGLISH)); if (this.offsetResetStrategy.equals(OffsetResetStrategy.NONE)) throw new IllegalArgumentException("The offset reset strategy 'none' is not valid. Must be either 'earliest'" + " or 'latest'");
private void initializeOffsetIfNeeded(final TopicPartition topicPartition, final OffsetAndMetadata currentOffsetMetadata) { if (currentOffsetMetadata == null) { // If there's no explicit offset - commit an explicit offset according to the offset reset strategy. if (OffsetResetStrategy.EARLIEST.name().equalsIgnoreCase(offsetResetStrategy)) { seekToBeginning(topicPartition); } else { seekToEnd(topicPartition); } final long currentPosition = kafkaConsumer.position(topicPartition); consumedOffsets.put(topicPartition, new OffsetAndMetadata(currentPosition)); } }
consumerConfigs.setProperty(ConsumerConfig.GROUP_ID_CONFIG, "hello-consumer"); consumerConfigs.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, OffsetResetStrategy.EARLIEST.name().toLowerCase()); final Consumer<String, String> kafkaConsumer = new KafkaConsumer<>( consumerConfigs);
Map<TopicPartition, Long> defaultOffsets; logger.info("The following partitions do not have existing offset data: {}", topicPartitionsWithUnknownOffset); if (unknownOffsetResetPosition.equals(OffsetResetStrategy.EARLIEST.toString().toLowerCase())) { logger.info("Using earliest offsets for partitions without existing offset data."); defaultOffsets = consumer.beginningOffsets(topicPartitionsWithUnknownOffset); } else if (unknownOffsetResetPosition.equals(OffsetResetStrategy.LATEST.toString().toLowerCase())) { logger.info("Using latest offsets for partitions without existing offset data."); defaultOffsets = consumer.endOffsets(topicPartitionsWithUnknownOffset); } else if (unknownOffsetResetPosition.equals(OffsetResetStrategy.NONE.toString().toLowerCase())) { logger.info("Will try to use existing consumer group offsets for partitions."); defaultOffsets = topicPartitionsWithUnknownOffset.stream() String.format("Unable to find committed offsets for consumer group for when %s=%s", ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, OffsetResetStrategy.NONE.toString().toLowerCase()));