Producer API

2. API

Apache Kafka includes new java clients (in the org.apache.kafka.clients package). These are meant to supplant the older Scala clients, but for compatibility they will co-exist for some time. These clients are available in a separate jar with minimal dependencies, while the old Scala clients remain packaged with the server.

2.1 Producer API

We encourage all new development to use the new Java producer. This client is production tested and generally both faster and more fully featured than the previous Scala client. You can use this client by adding a dependency on the client jar using the following example maven co-ordinates (you can change the version numbers with new releases):

  1. <dependency>
  2. <groupId>org.apache.kafka</groupId>
  3. <artifactId>kafka-clients</artifactId>
  4. <version>0.10.0.0</version>
  5. </dependency>

Examples showing how to use the producer are given in the javadocs.

For those interested in the legacy Scala producer api, information can be found here.

2.2 Consumer API

As of the 0.9.0 release we have added a new Java consumer to replace our existing high-level ZooKeeper-based consumer and low-level consumer APIs. This client is considered beta quality. To ensure a smooth upgrade path for users, we still maintain the old 0.8 consumer clients that continue to work on an 0.9 Kafka cluster. In the following sections we introduce both the old 0.8 consumer APIs (both high-level ConsumerConnector and low-level SimpleConsumer) and the new Java consumer API respectively.

2.2.1 Old High Level Consumer API

  1. class Consumer {
  2. /**
  3. * Create a ConsumerConnector
  4. *
  5. * @param config at the minimum, need to specify the groupid of the consumer and the zookeeper
  6. * connection string zookeeper.connect.
  7. */
  8. public static kafka.javaapi.consumer.ConsumerConnector createJavaConsumerConnector(ConsumerConfig config);
  9. }
  10. /**
  11. * V: type of the message
  12. * K: type of the optional key associated with the message
  13. */
  14. public interface kafka.javaapi.consumer.ConsumerConnector {
  15. /**
  16. * Create a list of message streams of type T for each topic.
  17. *
  18. * @param topicCountMap a map of (topic, #streams) pair
  19. * @param decoder a decoder that converts from Message to T
  20. * @return a map of (topic, list of KafkaStream) pairs.
  21. * The number of items in the list is #streams. Each stream supports
  22. * an iterator over message/metadata pairs.
  23. */
  24. public <K,V> Map<String, List<KafkaStream<K,V>>>
  25. createMessageStreams(Map<String, Integer> topicCountMap, Decoder<K> keyDecoder, Decoder<V> valueDecoder);
  26. /**
  27. * Create a list of message streams of type T for each topic, using the default decoder.
  28. */
  29. public Map<String, List<KafkaStream<byte[], byte[]>>> createMessageStreams(Map<String, Integer> topicCountMap);
  30. /**
  31. * Create a list of message streams for topics matching a wildcard.
  32. *
  33. * @param topicFilter a TopicFilter that specifies which topics to
  34. * subscribe to (encapsulates a whitelist or a blacklist).
  35. * @param numStreams the number of message streams to return.
  36. * @param keyDecoder a decoder that decodes the message key
  37. * @param valueDecoder a decoder that decodes the message itself
  38. * @return a list of KafkaStream. Each stream supports an
  39. * iterator over its MessageAndMetadata elements.
  40. */
  41. public <K,V> List<KafkaStream<K,V>>
  42. createMessageStreamsByFilter(TopicFilter topicFilter, int numStreams, Decoder<K> keyDecoder, Decoder<V> valueDecoder);
  43. /**
  44. * Create a list of message streams for topics matching a wildcard, using the default decoder.
  45. */
  46. public List<KafkaStream<byte[], byte[]>> createMessageStreamsByFilter(TopicFilter topicFilter, int numStreams);
  47. /**
  48. * Create a list of message streams for topics matching a wildcard, using the default decoder, with one stream.
  49. */
  50. public List<KafkaStream<byte[], byte[]>> createMessageStreamsByFilter(TopicFilter topicFilter);
  51. /**
  52. * Commit the offsets of all topic/partitions connected by this connector.
  53. */
  54. public void commitOffsets();
  55. /**
  56. * Shut down the connector
  57. */
  58. public void shutdown();
  59. }

You can follow this example to learn how to use the high level consumer api.

2.2.2 Old Simple Consumer API

  1. class kafka.javaapi.consumer.SimpleConsumer {
  2. /**
  3. * Fetch a set of messages from a topic.
  4. *
  5. * @param request specifies the topic name, topic partition, starting byte offset, maximum bytes to be fetched.
  6. * @return a set of fetched messages
  7. */
  8. public FetchResponse fetch(kafka.javaapi.FetchRequest request);
  9. /**
  10. * Fetch metadata for a sequence of topics.
  11. *
  12. * @param request specifies the versionId, clientId, sequence of topics.
  13. * @return metadata for each topic in the request.
  14. */
  15. public kafka.javaapi.TopicMetadataResponse send(kafka.javaapi.TopicMetadataRequest request);
  16. /**
  17. * Get a list of valid offsets (up to maxSize) before the given time.
  18. *
  19. * @param request a [[kafka.javaapi.OffsetRequest]] object.
  20. * @return a [[kafka.javaapi.OffsetResponse]] object.
  21. */
  22. public kafka.javaapi.OffsetResponse getOffsetsBefore(OffsetRequest request);
  23. /**
  24. * Close the SimpleConsumer.
  25. */
  26. public void close();
  27. }

For most applications, the high level consumer Api is good enough. Some applications want features not exposed to the high level consumer yet (e.g., set initial offset when restarting the consumer). They can instead use our low level SimpleConsumer Api. The logic will be a bit more complicated and you can follow the example in here.

2.2.3 New Consumer API

This new unified consumer API removes the distinction between the 0.8 high-level and low-level consumer APIs. You can use this client by adding a dependency on the client jar using the following example maven co-ordinates (you can change the version numbers with new releases):

  1. <dependency>
  2. <groupId>org.apache.kafka</groupId>
  3. <artifactId>kafka-clients</artifactId>
  4. <version>0.10.0.0</version>
  5. </dependency>

Examples showing how to use the consumer are given in the javadocs.

2.3 Streams API

As of the 0.10.0 release we have added a new client library named Kafka Streams to let users implement their stream processing applications with data stored in Kafka topics. Kafka Streams is considered alpha quality and its public APIs are likely to change in future releases. You can use Kafka Streams by adding a dependency on the streams jar using the following example maven co-ordinates (you can change the version numbers with new releases):

  1. <dependency>
  2. <groupId>org.apache.kafka</groupId>
  3. <artifactId>kafka-streams</artifactId>
  4. <version>0.10.0.0</version>
  5. </dependency>

Examples showing how to use this library are given in the javadocs (note those classes annotated with@InterfaceStability.Unstable, indicating their public APIs may change without backward-compatibility in future releases).

results matching ""

No results matching ""