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

kafka.entity.changelog.Client Maven / Gradle / Ivy

package kafka.entity.changelog;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import java.io.IOException;
import kafka.entity.changelog.topic.TopicCommander;

public class Client {
  public static void main(String[] args) {
    if (args.length < 1) {
      printHelp();
      System.exit(0);
    }
    Config config = ConfigFactory.load().getConfig("kafka-entity-changelog");
    if (args[0].equals("topics")) {
      TopicCommander commander = TopicCommander.newBuilder().build();
      if (args[1].equals("create")) {
        createTopic(args, config, commander);
      }
      if (args[1].equals("config")) {
        if (args[2].equals("add")) {
          addTopicConfig(args);
        }
      }
    }
  }

  private static void addTopicConfig(String[] args) {
    String key;
    String value;
    short replicationFactor;
    for (int i = 3; i < args.length; i++) {
      if (args[i].equals("--config")) {
        i++;
        String[] pair = args[i].split("=");
        key = pair[0];
        value = pair[1];
      }
    }
  }

  private static void createTopic(String[] args, Config config, TopicCommander commander) {
    String name = null;
    int partitions = config.getInt("topic.default-partitions");
    int replicationFactor = config.getInt("topic.default-replication-factor");
    String username = "anonymous";
    String source = "unknown";
    for (int i = 2; i < args.length; i++) {
      if (args[i].equals("--name") || args[i].equals("-n")) {
        i++;
        name = args[i];
      }
      if (args[i].equals("--partitions") || args[i].equals("-p")) {
        i++;
        partitions = Integer.parseInt(args[i]);
      }
      if (args[i].equals("--replication-factor") || args[i].equals("-rf")) {
        i++;
        partitions = Integer.parseInt(args[i]);
      }
      if (args[i].equals("--username") || args[i].equals("-u")) {
        i++;
        username = (args[i]);
      }
      if (args[i].equals("--source") || args[i].equals("-s")) {
        i++;
        source = (args[i]);
      }
    }
    if (name != null && !name.isEmpty()) {
      try {
        commander.createTopic(name, partitions, (short) replicationFactor, username, source);
        System.out.println("Topic creation command sent.");
        System.exit(0);
      } catch (IOException e) {
        System.out.println("Error creating topic");
        e.printStackTrace();
        printCreateTopicHelp();
        System.exit(1);
      }
    } else {
      printCreateTopicHelp();
      System.exit(0);
    }
  }

  private static void printCreateTopicHelp() {
    System.out.println("kafka-event-changelog client - `topic create` command");
    System.out.println();
    System.out.println("Arguments:");
    System.out.println("\t--name | -n: Topic name");
    System.out.println("\t--partitions | -p: Number of partitions");
    System.out.println("\t--replication-factor | -rf: Replication factor");
    System.out.println("\t--username | -u: Username who trigger creation");
    System.out.println("\t--source | -s: Source URL to link creation of topic");
  }

  private static void printHelp() {
    System.out.println("kafka-event-changelog client");
    System.out.println();
    System.out.println("Commands:");
    System.out.println("\ttopic:");
    System.out.println("\t\tcreate:");
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy