创建Kafka Producer并调用方法send(),flush()和close()的正确顺序是什么?

时间:2017-12-15 13:01:17

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

我有下面提到的不同模型,我创建Kafka生产者并调用不同的方法,但不确定什么是正确的编程方法,以便流不应该破坏,性能不应该受到影响。请帮助。

模型1:

for(int i=1; i < 100; i++){
    Producer<String, String> producer = new KafkaProducer<String, String>(props);

    ProducerRecord<String, String> data = new ProducerRecord<String, String>(
        topicName, 
        String.valueOf(i)
    );

    producer.send(data);
    producer.close();
}

模型2:

Producer<String, String> producer = new KafkaProducer<String, String>(props);
for(int i=1; i < 100; i++) {

    ProducerRecord<String, String> data = new ProducerRecord<String, String>(
        topicName, 
        String.valueOf(i)
    );

    producer.send(data);
    producer.close();
}

模型3:

Producer<String, String> producer = new KafkaProducer<String, String>(props);
for(int i=1; i < 100; i++){

    ProducerRecord<String, String> data = new ProducerRecord<String, String>(
        topicName, 
        String.valueOf(i)
    );

    producer.send(data);
}
producer.close();

模型4:

for(int i=1; i < 100; i++){
    Producer<String, String> producer = new KafkaProducer<String, String>(props);

    ProducerRecord<String, String> data = new ProducerRecord<String, String>(
        topicName,
        String.valueOf(i)
    );

    producer.send(data);
    producer.flush();
    producer.close();
}

模型5:

Producer<String, String> producer = new KafkaProducer<String, String>(props);
for(int i=1; i < 100; i++){
    ProducerRecord<String, String> data = new ProducerRecord<String, String>(
        topicName, 
        String.valueOf(i)
    );

    producer.send(data);
    producer.flush();
    producer.close();
}

模型6:

Producer<String, String> producer = new KafkaProducer<String, String>(props);
for(int i=1; i < 100; i++){

    ProducerRecord<String, String> data = new ProducerRecord<String, String>(
        topicName, 
        String.valueOf(i)
    );

    producer.send(data);
    producer.flush();
}
producer.close();

2 个答案:

答案 0 :(得分:1)

模型3似乎应该是正确的,以下更改

Producer<String, String> producer = new KafkaProducer<String, String>(props);
    try {
        for (int i = 1; i < 100; i++) {
            ProducerRecord<String, String> data = new ProducerRecord<String, String>(topicName, String.valueOf(i));
            producer.send(data);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        producer.close();
    }

答案 1 :(得分:1)

您可以使用以下示例:

Type1<-matrix(ncol=1,nrow=nrow(OrdersWide2))
for (i in 1:nrow(OrdersWide2)){
  Type1[i,]<-detect(OrdersWide2[i,2:ncol(OrdersWide2)], function(x) x != "", .right = F)
}
Type1<-as.data.frame(Type1)
colnames(Type1)<-"Type1"
Orders3<-cbind(OrdersWide2,Type1)

如你所知,send()方法是异步的。调用时,它会将记录添加到待处理记录发送的缓冲区中并立即返回。这允许生产者将各个记录批处理在一起以提高效率。

我们可以将buffer.memory或batch.size设置为自动刷新