-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-14752: Kafka examples improvements - consumer changes #13514
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5723a74
KAFKA-14752: Kafka examples improvements - consumer changes
fvaleri 1f0d679
Add changes requested by Christo
fvaleri adb0f6f
Improve error handling
fvaleri aacb156
Add feedback from Luke
fvaleri 1862749
Revert to 1 second poll timeout
fvaleri 5dbaabb
Only reset affected partitions
fvaleri 3d63836
Fix checkstyle
fvaleri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package kafka.examples; | ||
|
|
||
| import org.apache.kafka.clients.admin.Admin; | ||
| import org.apache.kafka.clients.admin.AdminClientConfig; | ||
| import org.apache.kafka.clients.admin.NewTopic; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.apache.kafka.clients.producer.RecordMetadata; | ||
| import org.apache.kafka.common.errors.TopicExistsException; | ||
| import org.apache.kafka.common.errors.UnknownTopicOrPartitionException; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Properties; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ExecutionException; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static java.lang.String.format; | ||
|
|
||
| public class Utils { | ||
| private Utils() { | ||
| } | ||
|
|
||
| public static void printHelp(String message, Object... args) { | ||
| System.out.println(format(message, args)); | ||
| } | ||
|
|
||
| public static void printOut(String message, Object... args) { | ||
| System.out.printf("%s - %s%n", Thread.currentThread().getName(), format(message, args)); | ||
| } | ||
|
|
||
| public static void printErr(String message, Object... args) { | ||
| System.err.printf("%s - %s%n", Thread.currentThread().getName(), format(message, args)); | ||
| } | ||
|
|
||
| public static void maybePrintRecord(long numRecords, ConsumerRecord<Integer, String> record) { | ||
| maybePrintRecord(numRecords, record.key(), record.value(), record.topic(), record.partition(), record.offset()); | ||
| } | ||
|
|
||
| public static void maybePrintRecord(long numRecords, int key, String value, RecordMetadata metadata) { | ||
| maybePrintRecord(numRecords, key, value, metadata.topic(), metadata.partition(), metadata.offset()); | ||
| } | ||
|
|
||
| private static void maybePrintRecord(long numRecords, int key, String value, String topic, int partition, long offset) { | ||
| // we only print 10 records when there are 20 or more to send | ||
|
fvaleri marked this conversation as resolved.
|
||
| if (key % Math.max(1, numRecords / 10) == 0) { | ||
| printOut("Sample: record(%d, %s), partition(%s-%d), offset(%d)", key, value, topic, partition, offset); | ||
| } | ||
| } | ||
|
|
||
| public static void recreateTopics(String bootstrapServers, int numPartitions, String... topicNames) { | ||
| Properties props = new Properties(); | ||
| props.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers); | ||
| props.put(AdminClientConfig.CLIENT_ID_CONFIG, "client-" + UUID.randomUUID()); | ||
| try (Admin admin = Admin.create(props)) { | ||
| // delete topics if present | ||
| try { | ||
| admin.deleteTopics(Arrays.asList(topicNames)).all().get(); | ||
| } catch (ExecutionException e) { | ||
| if (!(e.getCause() instanceof UnknownTopicOrPartitionException)) { | ||
| throw e; | ||
| } | ||
| printErr("Topics deletion error: %s", e.getCause()); | ||
| } | ||
| printOut("Deleted topics: %s", Arrays.toString(topicNames)); | ||
| // create topics in a retry loop | ||
| while (true) { | ||
| // use default RF to avoid NOT_ENOUGH_REPLICAS error with minISR>1 | ||
|
fvaleri marked this conversation as resolved.
Outdated
|
||
| short replicationFactor = -1; | ||
| List<NewTopic> newTopics = Arrays.stream(topicNames) | ||
| .map(name -> new NewTopic(name, numPartitions, replicationFactor)) | ||
| .collect(Collectors.toList()); | ||
| try { | ||
| admin.createTopics(newTopics).all().get(); | ||
| printOut("Created topics: %s", Arrays.toString(topicNames)); | ||
| break; | ||
| } catch (ExecutionException e) { | ||
| if (!(e.getCause() instanceof TopicExistsException)) { | ||
| throw e; | ||
| } | ||
| printOut("Waiting for topics metadata cleanup"); | ||
| TimeUnit.MILLISECONDS.sleep(1_000); | ||
| } | ||
| } | ||
| } catch (Throwable e) { | ||
| throw new RuntimeException("Topics creation error", e); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.