Using Admin API
1. Creating Admin Client
| Aspect | Description | Detail |
|---|---|---|
| AdminClient | Programmatic cluster mgmt | Async |
| Config | bootstrap.servers | Required |
| Futures | KafkaFuture results | get() |
Example: Create admin client
Properties p = new Properties();
p.put("bootstrap.servers", "localhost:9092");
Admin admin = Admin.create(p);
2. Creating Topics
| Param | Description | Detail |
|---|---|---|
| NewTopic | name, partitions, RF | Spec |
| configs | Per-topic overrides | Optional |
| createTopics | Batch create | Async |
Example: Create topic
admin.createTopics(List.of(
new NewTopic("orders", 6, (short) 3))).all().get();
3. Listing Topics
| Method | Description | Detail |
|---|---|---|
| listTopics() | All topic names | Set |
| ListTopicsOptions | Include internal | __topics |
| names() | Future of names | get() |
4. Describing Topics
| Field | Description | Detail |
|---|---|---|
| partitions | Per-partition info | Leader/ISR |
| replicas | Assigned brokers | List |
| describeTopics | Topic metadata | Map result |
Example: Describe topic
TopicDescription d = admin.describeTopics(List.of("orders"))
.allTopicNames().get().get("orders");
System.out.println(d.partitions().size());
5. Deleting Topics
| Aspect | Description | Detail |
|---|---|---|
| deleteTopics | Remove topics | Async |
| delete.topic.enable | Must be true | Broker config |
| Irreversible | Data lost | Confirm |
Warning: Topic deletion permanently removes all data; verify the topic name before deleting.
6. Creating Partitions
| Aspect | Description | Detail |
|---|---|---|
| createPartitions | Increase count only | No decrease |
| NewPartitions | Target total count | increaseTo |
| Caution | Breaks key→partition | Reorders |
Example: Add partitions
admin.createPartitions(
Map.of("orders", NewPartitions.increaseTo(12))).all().get();
7. Altering Configs
| Aspect | Description | Detail |
|---|---|---|
| incrementalAlterConfigs | SET/DELETE/APPEND | Preferred |
| ConfigResource | TOPIC/BROKER | Target |
| Dynamic | No restart | Live |
Example: Alter topic config
ConfigResource cr = new ConfigResource(ConfigResource.Type.TOPIC, "orders");
admin.incrementalAlterConfigs(Map.of(cr, List.of(
new AlterConfigOp(new ConfigEntry("retention.ms","604800000"),
AlterConfigOp.OpType.SET)))).all().get();
8. Describing Configs
| Aspect | Description | Detail |
|---|---|---|
| describeConfigs | Current config values | Effective |
| source | Default/dynamic/static | Origin |
| ConfigResource | What to inspect | Topic/broker |
Example: Describe config
Config c = admin.describeConfigs(List.of(cr)).all().get().get(cr);
c.entries().forEach(e -> System.out.println(e.name()+"="+e.value()));
9. Describing Consumer Groups
| Field | Description | Detail |
|---|---|---|
| members | Active consumers | Assignment |
| state | Stable/Rebalancing | Health |
| coordinator | Group coordinator broker | Node |
Example: Describe group
ConsumerGroupDescription g = admin
.describeConsumerGroups(List.of("order-svc"))
.all().get().get("order-svc");
10. Listing Consumer Groups
| Method | Description | Detail |
|---|---|---|
| listConsumerGroups | All groups | Names + state |
| valid() | Successful listings | Future |
| Filter | By state optional | inStates |
Example: List groups
admin.listConsumerGroups().valid().get()
.forEach(g -> System.out.println(g.groupId()));
11. Deleting Consumer Groups
| Aspect | Description | Detail |
|---|---|---|
| deleteConsumerGroups | Remove group + offsets | Async |
| Empty Only | No active members | Required |
| Error | NonEmptyGroup if active | Stop first |
12. Deleting Records
| Aspect | Description | Detail |
|---|---|---|
| deleteRecords | Truncate before offset | Low-water mark |
| RecordsToDelete | beforeOffset(n) | Per partition |
| Use | GDPR, cleanup | Reclaim space |