How to create topics in apache kafka?

前端 未结 6 1203
-上瘾入骨i
-上瘾入骨i 2021-01-31 17:09

What is the bestway to create topics in kafka?

  • How many replicas/partitions to be defined when we create topics?

In the new producer API, when i try

6条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 17:53

    You can create a topic programmatically .

    public class CreateTopic {
      public static void main(String[] args) throws ExecutionException, InterruptedException {
          Properties config = new Properties();
          config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
          AdminClient admin = AdminClient.create(config);
          //creating new topic
          System.out.println("-- creating --");
          NewTopic newTopic = new NewTopic("my-new-topic", 1, (short) 1);
          admin.createTopics(Collections.singleton(newTopic));
    
          //listing
          System.out.println("-- listing --");
          admin.listTopics().names().get().forEach(System.out::println);
      }
    }
    

提交回复
热议问题