How to declare different topics in kafka_ex?

How can I declare diff topics in supervisor using kafka_ex? I already follow this instruction: https://hexdocs.pm/kafka_ex/KafkaEx.ConsumerGroup.html

My code:

children = [
supervisor(
        KafkaEx.ConsumerGroup,
        [
          MyConsumer1,
          "consumer_group_name",
          "my-topic1",
          @opts
        ]
      ),
supervisor(
        KafkaEx.ConsumerGroup,
        [
          MyConsumer2,
          "consumer_group_name",
          "my-topic2",
          @opts
        ]
      )
]

It returns

children = [
      Supervisor.child_spec({MyWorker, arg}, id: :my_worker_1),
      Supervisor.child_spec({MyWorker, arg}, id: :my_worker_2)
    ]

** (Mix) Could not start application myapp: MyApp.Application.start(:normal, []) returned an error: bad child specification, more than one child specification has the id: KafkaEx.ConsumerGroup.
If using maps as child specifications, make sure the :id keys are unique.
If using a module or {module, arg} as child, use Supervisor.child_spec/2 to change the :id, for example:

    children = [
      Supervisor.child_spec({MyWorker, arg}, id: :my_worker_1),
      Supervisor.child_spec({MyWorker, arg}, id: :my_worker_2)
    ]

It suggests that I have to make a different KafkaEx.ConsumerGroup for the id but there’s only one in kafka_ex. Please help. Can’t find any solution. What’s the syntax for that? :frowning:

1 Like

I believe you’d have to use something like this:

https://hexdocs.pm/kafka_ex/KafkaEx.Protocol.CreateTopics.html

Hope it helps!

1 Like

You need to specify an id for each of the supervisors.

For example:

children = [
supervisor(
        KafkaEx.ConsumerGroup,
        [
          MyConsumer1,
          "consumer_group_name",
          "my-topic1",
          @opts
        ],
        id: "my-topic1"
      ),
supervisor(
        KafkaEx.ConsumerGroup,
        [
          MyConsumer2,
          "consumer_group_name",
          "my-topic2",
          @opts
        ],
        id: "my-topic2"
      )
]
3 Likes

Thank you guys appreciate it.