All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.imrafaelmerino.kafkacli.ConsumerListCommand Maven / Gradle / Ivy

Go to download

Kafka CLI is a command-line interface tool designed to simplify the management and interaction with Apache Kafka. It allows you to manage Kafka producers and consumers, publish messages to Kafka topics, and define channels with customizable schemas and data generators

There is a newer version: 1.0.0
Show newest version
package com.github.imrafaelmerino.kafkacli;

import jio.IO;
import jio.cli.Command;
import jio.cli.State;
import jsonvalues.JsObj;

import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

class ConsumerListCommand extends Command {

    static final String LS_CONSUMERS_COMMAND = "consumer-list";
    private static final String USAGE = """
            Usage: consumer-list

            Description:
            The `consumer-list` command lists all Kafka consumers along with their statuses (up or down).

            Output:
            - The list of consumers with their names and statuses.

            Example:
            $ consumer-list
            Name                 Status
            consumer1            up
            consumer2            down
            consumer3            up

            Note:
            Ensure that the consumer configurations are correctly set in the configuration file to accurately reflect their statuses.
            """;
    final KafkaConsumers kafkaConsumers;

    ConsumerListCommand(final KafkaConsumers kafkaConsumers) {
        super(LS_CONSUMERS_COMMAND,
              USAGE,
              tokens -> tokens[0].

                      equals(LS_CONSUMERS_COMMAND));
        this.kafkaConsumers = kafkaConsumers;
    }

    @Override
    public Function> apply(final JsObj conf,
                                                final State state
                                               ) {
        return _ -> IO.lazy(() -> {
            Set result = ConfigurationQueries.getConsumers(conf);
            return result.stream()
                         .map(consumer -> String.format("%-20s %s",
                                                        consumer,
                                                        kafkaConsumers.apply(consumer) != null ? "up" : "down")
                             )
                         .collect(Collectors.joining("\n",
                                                     String.format("%-20s %s",
                                                                   "Name",
                                                                   "Status\n"),
                                                     ""));
        });
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy