kafka文档(3)----0.8.2-kafka API(java版本)

来源:互联网 发布:h3c路由器开放端口 编辑:程序博客网 时间:2024/05/16 05:47

原文地址: http://kafka.apache.org/documentation.html#api


    Apache Kafka包含新的java客户端,这些新的的客户端将取代现存的Scala客户端,但是为了兼容性,它们仍将存在一段时间。可以通过一些单独的jar包调用这些客户端,这些包的依赖性都比较小,同时老的Scala客户端仍会存在。





一、Producer   API

  我们鼓励所有新开发都使用新的java版本producer。这个客户端是经过生产环境测试的,并且一般情况下会比先前的Scala客户端要更快而且具有更多的特性。你可以通过添加对客户端jar包的依赖来调用这个客户端,如下所示,使用maven配置:

    

        <dependency>    <groupId>org.apache.kafka</groupId>    <artifactId>kafka-clients</artifactId>    <version>0.10.0.0</version></dependency>
可以通过javadoc文件查看如何使用producer。
二、Consumer API
在0.9.0发布版本中,增加了新的java版本的consumer,用来替代已有的high-level的基于zookeeper的consumer,以及low-level的consumer APIs。
这个客户端认为是beta版本。为了保证用户获得平稳的升级,我们会继续维护0.8版本的consumer客户端,此版本客户端会在0.9版本的kafka集群中
依然生效。下面的章节中,我们会介绍老的0.8版本的consumer APIs(包括high-level的ConusmerConnector以及low-level SimpleConsumer)以及
新的Java版本的consumer API。
1、Old High  Level Consumer  API
class   Consumer{
  /**
   *  Create a ConsumerConnector:创建consumer connector
   *
   *  @param config at the minimum, need to specify the groupid of the consumer and the zookeeper connection string zookeeper.connect.config参数作用:需要置顶consumer的groupid以及zookeeper连接字符串zookeeper.connect
   */
    public static kafka.javaapi.consumer.ConsumerConnector  createJavaConsumerConnector(ConsumerConfig  config);
  }
  /**
   *  V: type of the message: 消息类型
   *  K: type of the optional key assciated with the message: 消息携带的可选关键字类型
   */
   public interface kafka.javaapi.consumer.ConsumerConnector {
     /**
      *  Create a list of message streams of type T for each topic.:为每个topic创建T类型的消息流的列表
      * 
      *  @param topicCountMap a map of (topic, #streams) pair   : topic与streams的键值对
      *  @param decoder a decoder that converts from Message to T  : 转换Message到T的解码器
      *  @return  a map of (topic, list of KafakStream) pairs.   : topic与KafkaStream列表的键值对
      *           The number of items in the list is #streams . Each stream supports
      *           an iterator over message/metadata pairs .:列表中项目的数量是#streams。每个stream都支持基于message/metadata 对的迭代器
      */
      public <K,V> Map<String, List<KafkaStream<K,V> > >
        createMessageStreams( Map<String, Integer> topicCountMap, Decoder<K> keyDecoder, Decoder<V> valueDecoder);
    /**     *  Create a list of message streams of type T for each topic, using the default decoder.为每个topic创建T类型的消息列表。使用默认解码器     */
     public Map<String, List<KafkaStream<byte[], byte[]>>> createMessageStreams(Map<String, Integer> topicCountMap);
    /**   *  Create a list of message streams for topics matching a wildcard.为匹配wildcard的topics创建消息流的列表   *   *  @param topicFilter a TopicFilter that specifies which topics to   *                    subscribe to (encapsulates a whitelist or a blacklist).指定将要订阅的topics的TopicFilter(封装了whitelist或者黑名单)   *  @param numStreams the number of message streams to return.将要返回的流的数量   *  @param keyDecoder a decoder that decodes the message key  可以解码关键字key的解码器   *  @param valueDecoder a decoder that decodes the message itself  可以解码消息本身的解码器   *  @return a list of KafkaStream. Each stream supports an   *          iterator over its MessageAndMetadata elements.  返回KafkaStream的列表。每个流都支持基于MessagesAndMetadata 元素的迭代器。   */
 public <K,V> List<KafkaStream<K,V>>    createMessageStreamsByFilter(TopicFilter topicFilter, int numStreams, Decoder<K> keyDecoder, Decoder<V> valueDecoder);
    /**   *  Create a list of message streams for topics matching a wildcard, using the default decoder.使用默认解码器,为匹配wildcard的topics创建消息流列表   */  public List<KafkaStream<byte[], byte[]>> createMessageStreamsByFilter(TopicFilter topicFilter, int numStreams);  /**   *  Create a list of message streams for topics matching a wildcard, using the default decoder, with one stream.使用默认解码器,为匹配wildcard的topics创建消息流列表   */  public List<KafkaStream<byte[], byte[]>> createMessageStreamsByFilter(TopicFilter topicFilter);  /**   *  Commit the offsets of all topic/partitions connected by this connector.通过connector提交所有topic/partitions的offsets   */  public void commitOffsets();  /**   *  Shut down the connector: 关闭connector   */  public void shutdown();}
     
你可以根据这个例子学习怎样使用high level consumer api。


2、Old Simple Consumer  API
class kafka.javaapi.consumer.SimpleConsumer {  /**   *  Fetch a set of messages from a topic.从topis抓取消息序列   *   *  @param request specifies the topic name, topic partition, starting byte offset, maximum bytes to be fetched.指定topic 名字,topic partition,开始的字节offset,抓取的最大字节数   *  @return a set of fetched messages   */  public FetchResponse fetch(kafka.javaapi.FetchRequest request);  /**   *  Fetch metadata for a sequence of topics.抓取一系列topics的metadata   *   *  @param request specifies the versionId, clientId, sequence of topics.指定versionId,clientId,topics   *  @return metadata for each topic in the request.返回此要求中每个topic的元素据   */  public kafka.javaapi.TopicMetadataResponse send(kafka.javaapi.TopicMetadataRequest request);  /**   *  Get a list of valid offsets (up to maxSize) before the given time.在给定的时间内返回正确偏移的列表   *   *  @param request a [[kafka.javaapi.OffsetRequest]] object.    *  @return a [[kafka.javaapi.OffsetResponse]] object.   */  public kafak.javaapi.OffsetResponse getOffsetsBefore(OffsetRequest request);  /**   * Close the SimpleConsumer.关闭   */  public void close();}
对大多数应用来说, high  level consumer  Api已经足够了,一些应用要求的一些特征还没有出现high level consumer接口(例如,
当重启consumer时,设置初始offset)。他们可以使用low level SimpleConsumer  Api。逻辑可能会有些复杂,你可以根据这个例子学习一下。
3、New Consumer API
新consumer API统一了标准,原来存在于0.8版本的high-level以及low-level consumer APIs之间差异不存在了。你可以通过使用下面maven配置方式,
指明客户端依赖的jar包,这样就可以使用新的consumer API。
<dependency>    <groupId>org.apache.kafka</groupId>    <artifactId>kafka-clients</artifactId>    <version>0.10.0.0</version></dependency>
Examples showing how to use the consumer are given in the javadocs.
三、Streams API
在0.10.0 release版本中,我们增加了新的客户端调用库Kafka Streams,用来支持流式处理应用。Kafka Streams库认为是
alpha版本质量的,同时它的公共调用APIs在将来有可能会修改。你可以像下面maven配置模式一样,指明Kafka Streams的
依赖关系来调用Kafka Streams。
<dependency>    <groupId>org.apache.kafka</groupId>    <artifactId>kafka-streams</artifactId>    <version>0.10.0.0</version></dependency>

在Javadocs中展示了如何调用这个库(注意这些类都是不稳定的,表明以后的版本中可能会修改)。
                                             
1 0
原创粉丝点击