【问题标题】:How to create several partitions by Alpakka如何通过 Alpakka 创建多个分区
【发布时间】:2019-04-05 00:48:33
【问题描述】:

我正在尝试创建一个简单的生产者,它使用配置提供的一些分区创建一个主题。

根据Alpakka Producer Setting Docorg.apache.kafka.clients.producer.ProducerConfig 中的任何属性都可以在kafka-clients 部分中设置。并且,num.partitions 中有一个 num.partitions 属性,正如在 Producer API Doc 中所评论的那样。

因此,我将该属性添加到我的application.conf 文件中,如下所示:

topic = "topic"
topic = ${?TOPIC}

# Properties for akka.kafka.ProducerSettings can be
# defined in this section or a configuration section with
# the same layout.
akka.kafka.producer {
  # Tuning parameter of how many sends that can run in parallel.
  parallelism = 100
  parallelism = ${?PARALLELISM}

  # Duration to wait for `KafkaConsumer.close` to finish.
  close-timeout = 20s

  # Fully qualified config path which holds the dispatcher configuration
  # to be used by the producer stages. Some blocking may occur.
  # When this value is empty, the dispatcher configured for the stream
  # will be used.
  use-dispatcher = "akka.kafka.default-dispatcher"

  # The time interval to commit a transaction when using the `Transactional.sink` or `Transactional.flow`
  eos-commit-interval = 100ms

  # Properties defined by org.apache.kafka.clients.producer.ProducerConfig
  # can be defined in this configuration section.
  kafka-clients {
    bootstrap.servers = "my-kafka:9092"
    bootstrap.servers = ${?BOOTSTRAPSERVERS}
    num.partitions = "3"
    num.partitions = ${?NUM_PARTITIONS}
  }
}

生产者应用代码如下:

object Main extends App {

  val config = ConfigFactory.load()

  implicit val system: ActorSystem = ActorSystem("producer")
  implicit val materializer: Materializer = ActorMaterializer()

  val producerConfigs = config.getConfig("akka.kafka.producer")
  val producerSettings = ProducerSettings(producerConfigs, new StringSerializer, new StringSerializer)

  val topic = config.getString("topic")

  val done: Future[Done] =
    Source(1 to 100000)
      .map(_.toString)
      .map(value => new ProducerRecord[String, String](topic, value))
      .runWith(Producer.plainSink(producerSettings))

  implicit val ec: ExecutionContextExecutor = system.dispatcher
  done onComplete  {
    case Success(_) => println("Done"); system.terminate()
    case Failure(err) => println(err.toString); system.terminate()
  }

}

但是,这不起作用。 Producer 使用单个分区而不是我通过配置设置的 3 个分区创建主题:

num.partitions = "3"

最后,Kafkacat 输出如下:

~$ kafkacat -b my-kafka:9092 -L
Metadata for all topics (from broker -1: my-kafka:9092/bootstrap):
 3 brokers:
  broker 2 at my-kafka-2.my-kafka-headless.default:9092
  broker 1 at my-kafka-1.my-kafka-headless.default:9092
  broker 0 at my-kafka-0.my-kafka-headless.default:9092
 1 topics:
  topic "topic" with 1 partitions:
    partition 0, leader 2, replicas: 2, isrs: 2

怎么了?是否可以使用 Alpakka 从 kafka-clients 部分的 Kafka Producer API 设置属性?

【问题讨论】:

    标签: apache-kafka kafka-producer-api alpakka


    【解决方案1】:

    # org.apache.kafka.clients.producer.ProducerConfig 定义的属性

    #可以在这个配置部分定义。

    正如上面所说,ProducerConfig 用于生产者设置,而不是代理设置,这就是 num.partitions 的含义(我认为您迷失在 Apache Kafka 文档中显示的哪个表中...滚动到顶部以查看正确的标题)。

    没有办法从生产者那里设置主题的分区...您需要使用AdminClient 类来创建主题,并且分区的数量是那里的参数,而不是配置属性。

    示例代码

    val props = new Properties()
    props.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092")
    
    val adminClient = AdminClient.create(props)
    
    val numPartitions = 3
    val replicationFactor = 3.toShort
    val newTopic = new NewTopic("new-topic-name", numPartitions, replicationFactor)
    val configs = Map(TopicConfig.COMPRESSION_TYPE_CONFIG -> "gzip")
    // settings some configs
    newTopic.configs(configs.asJava)
    
    adminClient.createTopics(List(newTopic).asJavaCollection)
    

    然后你就可以启动生产者了

    【讨论】:

    • 谢谢,我终于看到了这个选项。我不会从应用程序创建主题作为最终解决方案。
    【解决方案2】:

    主题似乎是由 Default 创建的,这是 Kafka 的默认行为。如果是这种情况,您需要在 server.properties 文件中为您的代理定义默认分区数。

    # The default number of log partitions per topic. More partitions allow greater
    # parallelism for consumption, but this will also result in more files across
    # the brokers.
    num.partitions=3
    

    【讨论】:

    • 嗨,是的,但主题以前不存在。我希望生产者应用程序创建具有特定数量分区的主题。似乎 Alpakka 不允许为主题设置多个分区。 Alpakka 提供了一个名为“def createTopic(number: Int = 0, partitions: Int = 1, replication: Int = 1)”的方法来实现,但是该方法是由Alpakka Kafka Testkit提供的。
    • @SergioRodríguezCalvo 不建议让应用程序使用服务器端默认值自动创建主题。您应该提前通过其他方式创建主题。
    • 是的,我知道,但就我而言,该应用程序是概念验证。最后,我不会从应用程序创建主题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多