Roaming Kafka the client API of the actual combat chapter

Source: Internet
Author: User

Kafka Producer APIs

There are two types of legacy Procuder APIs: Kafka.producer.SyncProducer and Kafka.producer.async.AsyncProducer. They all implement the same interface:

[Java]View Plaincopy
  1. Class Producer {
  2. / * Send the message to the specified partition * /
  3. public void Send (kafka.javaapi.producer.producerdata<k,v> producerdata);
  4. / * Bulk send A batch of messages */
  5. public void Send (java.util.list<kafka.javaapi.producer.producerdata<k,v>> producerdata);
  6. / * Close producer * /
  7. public void Close ();
  8. }

The new Producer API provides the following features:

  1. Multiple messages can be cached to the local queue and then asynchronously batched to the broker, which can be producer.type=async做到。 specified by some parameters by the size of the parameter cache: queue.time and batch.size . A Background thread (( kafka.producer.async.ProducerSendThread ) pulls the data from the queue and kafka.producer.EventHandler sends the message to the broker, or through parameter event.handler定制 handler, registers the processor at different stages of the producer-side processing of the data, such as a log trace of the process, or perform some monitoring. Simply implement the kafka.producer.async.CallbackHandler interface and configure it in callback.handler .
  2. Write your own encoder to serialize the message, just implement the following interface. The default encoder is kafka.serializer.DefaultEncoder . [Java] view plaincopy
    1. Interface Encoder<t> {
    2. Public Message tomessage (T data);
    3. }
  3. Provides zookeeper-based broker auto-sensing capability, which can be zk.connect implemented by parameters. If you do not use zookeeper, you can also use broker.list parameters to specify a static brokers list, so that messages will be sent randomly to a broker, and once the selected broker fails, the message will fail to send.
  4. Through the partition function kafka.producer.Partitioner类对消息分区 . [Java] view plaincopy
    1. Interface Partitioner<t> {
    2. int partition (T key, int numpartitions);
    3. }
    The partition function has two parameters: key and the number of available partitions, select a partition from the partition list and return the ID. The default partitioning policy is hash(key)%numPartitions . If key is null, select one randomly. Partitioning functions can be customized by parameters partitioner.class .

A complete example of the new API is as follows:

[Java]View Plaincopy
  1. Import java.util.*;
  2. Import Kafka.javaapi.producer.Producer;
  3. Import Kafka.producer.KeyedMessage;
  4. Import Kafka.producer.ProducerConfig;
  5. Public class Testproducer {
  6. public static void Main (string[] args) {
  7. Long events = Long.parselong (args[0]);
  8. Random rnd = new Random ();
  9. Properties props = new properties ();
  10. Props.put ("Metadata.broker.list", "broker1:9092,broker2:9092");
  11. Props.put ("Serializer.class", "Kafka.serializer.StringEncoder");
  12. Props.put ("Partitioner.class", "Example.producer.SimplePartitioner");
  13. Props.put ("Request.required.acks", "1");
  14. Producerconfig config = new producerconfig (props);
  15. producer<string, string> Producer = new producer<string, string> (config);
  16. For (long nevents = 0; nevents < events; nevents++) {
  17. Long runtime = new Date (). GetTime ();
  18. String IP = "192.168. "   2. "+ rnd.nextint (255);
  19. String msg = runtime + ", www.example.com," + IP;
  20. keyedmessage<string, string> data = new keyedmessage<string, string> ("page_visits", IP, msg);
  21. Producer.send (data);
  22. }
  23. Producer.close ();
  24. }
  25. }


The following is the partition function used:

[Java]View Plaincopy
  1. Import Kafka.producer.Partitioner;
  2. Import kafka.utils.VerifiableProperties;
  3. Public class Simplepartitioner implements partitioner<string> {
  4. Public Simplepartitioner (verifiableproperties props) {
  5. }
  6. public int partition (String key, int a_numpartitions) {
  7. int partition = 0;
  8. int offset = key.lastindexof ('. ');
  9. if (offset > 0) {
  10. Partition = Integer.parseint (key.substring (offset+1))% A_numpartitions;
  11. }
  12. return partition;
  13. }
  14. }



KafKa Consumer APIs

There are two levels of the Consumer API. The lower level is kept connected with a specified broker and closes the connection after receiving the message, which is stateless, with offset for each read message.

The high-level API hides the details of the brokers connection and communicates with the server without having to care about the server-side architecture. You can also maintain your own consumption status, and you can specify subscription-specific topic, such as a whitelist blacklist or regular expression, by some criteria.

Low-level API

[Java]View Plaincopy
  1. Class Simpleconsumer {
  2. / * Send a read request to a broker and get the message set * /
  3. Public Bytebuffermessageset Fetch (fetchrequest request);
  4. / * Send a read request to a broker and get a corresponding set * /
  5. Public multifetchresponse Multifetch (list<fetchrequest> fetches);
  6. /** 
  7. * Get offsets before the specified time
  8. * The return value is a offsets list, sorted in reverse order
  9. * @param time: times, milliseconds,
  10. * If specified as offsetrequest$. module$. Latiest_time (), get the latest offset.
  11. * If specified as offsetrequest$. module$. Earliest_time (), get the oldest offset.
  12. */
  13. public long[] Getoffsetsbefore (String topic, int partition, long time, int maxnumoffsets);
  14. }

Low-level APIs are the basis for high-level API implementations and are for scenarios where there is a special need to maintain the consumption state, such as offline consumer such as Hadoop consumer.

High Level API

[Java]View Plaincopy
  1. /* Create connection */
  2. Consumerconnector connector = consumer.create (consumerconfig);
  3. Interface Consumerconnector {
  4. /** 
  5. * This method can get a list of streams, each of which is an iteration of messageandmetadata, with Messageandmetadata to get messages and other meta-data (currently topic)
  6. * Input:a Map of <topic, #streams >
  7. * Output:a Map of <topic, List of message streams>
  8. */
  9. Public map<string,list<kafkastream>> Createmessagestreams (map<string,int> topicCountMap);
  10. /**     
  11. * You can also get a list of streams that contain an iteration of the message that conforms to Topicfiler,
  12. * A topicfilter is a regular expression that encapsulates a whitelist or blacklist.
  13. */
  14. Public list<kafkastream> Createmessagestreamsbyfilter (
  15. Topicfilter topicfilter, int numstreams);
  16. / * Submit the current consumption to offset * /
  17. Public commitoffsets ()
  18. / * Close connection * /
  19. Public shutdown ()
  20. }

The API revolves around an iterator implemented by Kafkastream, each of which represents a series of messages converging from one or more partitions and brokers, each of which is handled by a single thread, so the client can specify a few streams by parameter when it is created. A stream is a merge of multiple brokers on multiple partitions, but messages for each partition flow only to one stream.

Each call to Createmessagestreams will register consumer on topic so that load balancing between consumer and brokers is adjusted. The API encourages each invocation to create more topic streams to reduce this adjustment. The Createmessagestreamsbyfilter method registers the listener to perceive the new filter-compliant tipic.

Roaming Kafka the client API of the actual combat chapter

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.