Kafka Template–2.2.0 api

来源:互联网 发布:红帽系统yum源 编辑:程序博客网 时间:2024/06/11 00:27

Kafka Template–2.2.0 api

KafkaTemplate

KafkaTemplate这个类包装了个生产者,来提供方便的发送数据到kafka的topic里面。 
同步和异步的方法都有,异步方法返回一个Future

ListenableFuture<SendResult<K, V>> sendDefault(V data);ListenableFuture<SendResult<K, V>> sendDefault(K key, V data);ListenableFuture<SendResult<K, V>> sendDefault(Integer partition, K key, V data);ListenableFuture<SendResult<K, V>> sendDefault(Integer partition, Long timestamp, K key, V data);ListenableFuture<SendResult<K, V>> send(String topic, V data);ListenableFuture<SendResult<K, V>> send(String topic, K key, V data);ListenableFuture<SendResult<K, V>> send(String topic, Integer partition, K key, V data);ListenableFuture<SendResult<K, V>> send(String topic, Integer partition, Long timestamp, K key, V data);ListenableFuture<SendResult<K, V>> send(Message<?> message);Map<MetricName, ? extends Metric> metrics();List<PartitionInfo> partitionsFor(String topic);<T> T execute(ProducerCallback<K, V, T> callback);// Flush the producer.void flush();interface ProducerCallback<K, V, T> {    T doInKafka(Producer<K, V> producer);}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • sendDefault 这个方法,需要提供一个默认的topic。
  • API使用一个timestamp作为一个参数,这个时间戳奖杯存储在record里面。
  • 用户的时间戳是否被存储,取决于在kafka的topic里面配置的时间戳类型。如果topic被配置成了CREATE_TIME,则将使用用户指定的时间戳,如果没指定,将自动生成。如果设置为了LOG_APPEND_TIME那么用户指定的时间戳将会被忽略,使用broker的local time.
  • metrics方法和partitionsFor方法,委托给一些潜在的生产者;而execute方法则是提供了直接访问潜在生产者的途径。 
    为了使用这个template,配置个工厂类,并将这个工厂类提供给templage类的构造方法。 
    举个栗子:
@Beanpublic ProducerFactory<Integer, String> producerFactory() {    return new DefaultKafkaProducerFactory<>(producerConfigs());}@Beanpublic Map<String, Object> producerConfigs() {    Map<String, Object> props = new HashMap<>();    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");    ...    return props;}@Beanpublic KafkaTemplate<Integer, String> kafkaTemplate() {    return new KafkaTemplate<Integer, String>(producerFactory());}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 这个template可以使用spring方式进行配置。
  • 当使用一个Message<?>参数的时候,需要为这个消息的头部,提供topic,partition,key信息。

    • KafkaHeaders.TOPIC
    • KafkaHeaders.PARTITION_ID
    • KafkaHeaders.MESSAGE_KEY
    • KafkaHeaders.TIMESTAMP 
      这些信息将被装在到数据里面。
  • 你可以配置给KafkaTemplate配置个ProducerListener来监听异步的回调结果,用这种方式替代使用Future

public interface ProducerListener<K, V> {    void onSuccess(String topic, Integer partition, K key, V value, RecordMetadata recordMetadata);    void onError(String topic, Integer partition, K key, V value, Exception exception);    boolean isInterestedInSuccess();}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • template默认配置LoggingProducerListener这个类作为监听类用来记录错误,但是当发送成功时候,却不做任何操作。
  • 为了方便使用,提供了抽象类ProducerListenerAdapter,你可以只实现它一两个方法,它为isInterestedInSuccess方法返回false
  • 观察template类的发送方法,发现它返回一个ListenableFuture<SendResult>。你可以使用一个监听类来注册一个回调,用来接收异步发送返回的结果。
ListenableFuture<SendResult<Integer, String>> future = template.send("foo");future.addCallback(new ListenableFutureCallback<SendResult<Integer, String>>() {    @Override    public void onSuccess(SendResult<Integer, String> result) {        ...    }    @Override    public void onFailure(Throwable ex) {        ...    }});
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • SendResult这个类有两个属性,ProducerRecordRecordMetadata;具体参考kafka的API
  • 如果你想要阻塞发送线程(批量发送),为了等待结果,你可以调用future的get()方法;另外,在等待的过程中,你可能想要调用flush()方法,为了简便,template类提供了一个autoFlush,这个属性将会在每次send的时候,立即去flush掉sending thread.不过autoFlush将会明显降低性能(就是说最好不要一条条的发送,要批量发送)。 
原创粉丝点击