我正在尝试创建一个简单的生产者,它用配置提供的一些分区创建一个主题。
根据阿尔帕卡制作人的说法 org.apache.kafka.clients.producer.ProducerConfig
可以设置为 kafka-clients
部分。而且,还有一个 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
怎么了?是否可以在中设置kafka producer api的属性 kafka-clients
使用alpakka的部分?
2条答案
按热度按时间ldioqlga1#
似乎该主题在默认情况下获得create,这是kafka的默认行为。如果是这种情况,则需要在server.properties文件中为代理定义默认的分区数。
ego6inou2#
org.apache.kafka.clients.producer.producerconfig定义的属性
可在此配置部分中定义。
正如上面所说,
ProducerConfig
是生产者设置,而不是经纪人设置,这是什么num.partitions
是(我想你在ApacheKafka文档中显示的属性表中迷路了。。。滚动到顶部以查看正确的标题)。无法从生产者设置主题的分区。。。你需要使用
AdminClient
类来创建主题,分区数是其中的一个参数,而不是配置属性。样本代码
然后你就可以开始制作了