1. 程式人生 > >二、Kafka基礎實戰:消費者和生產者實例

二、Kafka基礎實戰:消費者和生產者實例

消費者 12.1 實戰 tof star inter 傳遞 默認 參數調優

一、Kafka消費者編程模型

1.分區消費模型

技術分享圖片

分區消費偽代碼描述
main()
   獲取分區的size
   for index =0 to size
     create thread(or process) consumer(Index)


第index個線程(進程)
consumer(index)
    創建到kafka broker的連接: KafkaClient(host,port)
    指定消費參數構建consumer: SimpleConsumer(topic, partitions)
    設置消費offset : consumer.seek(offset,0)
    while  True
      消費指定topic第index個分區的數據
      處理
     記錄當前消息offset
     提交當前offset(可選)

2.組(Group)消費模型

技術分享圖片 按組(Group)消費偽代碼描述
main()
   設置需要創建的流數N
   for index =0 to N
     create thread  consumer(Index)

第index個線程
consumer(index)
    創建到kafka broker的連接: KafkaClient(host,port)
    指定消費參數構建consumer: SimpleConsumer(topic, partitions)
    設置從頭消費還是從最新的消費(smallest或largest)
    
while True 從指定topic的第index個流取數據 處理 (offset會自動提交到zookeeper,無需我們操作)
Consumer分配算法 技術分享圖片

3.兩種消費模型對比

分區消費模型更加靈活但是:
(1)需要自己處理各種異常情況;
(2)需要自己管理offset(以實現消息傳遞的其他語義);
分組消費模型更加簡單,但是不靈活:
(1)不需要自己處理異常情況,不需要自己管理offset;
(2)只能實現kafka默認的最少一次消息傳遞語義;

消息傳遞三種語義: (1)至多一次; (2)最少一次; (3)恰好一次;

二、Kafka消費者的Python和Java客戶端實現

1.Python客戶端實例講解

•需要的軟件環境: 已搭建好的kafka集群、Linux服務器一臺、Python2.7.6 、 kafka-Python軟件包 •分區消費模型的Python實現; main.py
# -*- coding: utf-8 -*-
"""
This module provide kafka partition and group consumer demo example.

"""


import logging, time
import partition_consumer


def main():

threads = []
partition = 3
for index in range(partition):
threads.append(partition_consumer.Consumer(index))

for t in threads:
t.start()

time.sleep(50000)

if __name__ == __main__:
#logging.basicConfig(
# format=‘%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s‘,
# level=logging.INFO
# )
main()

partition_consumer.py

# -*- coding: utf-8 -*-
"""
This module provide kafka partition partition consumer demo example.


"""

import threading
from kafka.client import KafkaClient
from kafka.consumer import SimpleConsumer

class Consumer(threading.Thread):
    daemon = True
    def __init__(self,partition_index):
        threading.Thread.__init__(self)
        self.part = [partition_index]
        self.__offset = 0


    def run(self):
        client = KafkaClient("10.206.216.13:19092,10.206.212.14:19092,10.206.209.25:19092")
        consumer = SimpleConsumer(client, "test-group", "jiketest",auto_commit=False,partitions=self.part)

        consumer.seek(0,0)

        while True:
            message = consumer.get_message(True,60)
            self.__offset = message.offset
            print message.message.value

•組(Group)消費模型的Python實現;

main.py

# -*- coding: utf-8 -*-
"""
This module provide kafka partition and group consumer demo example.


"""

import logging, time
import group_consumer


def main():

    conusmer_thread = group_consumer.Consumer()
    conusmer_thread.start()

    time.sleep(500000)

if __name__ == __main__:
    #logging.basicConfig(
    #    format=‘%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s‘,
    #    level=logging.INFO
    #    )
    main()

group_consumer.py

# -*- coding: utf-8 -*-
"""
This module provide kafka partition group consumer demo example.

"""

import threading
from kafka.client import KafkaClient
from kafka.consumer import SimpleConsumer

class Consumer(threading.Thread):
    daemon = True

    def run(self):
        client = KafkaClient("10.206.216.13:19092,10.206.212.14:19092,10.206.209.25:19092")
        consumer = SimpleConsumer(client, "test-group", "jiketest")

        for message in consumer:
            print(message.message.value)

2.Python客戶端參數調優

•fetch_size_bytes: 從服務器獲取單包大小; •buffer_size: kafka客戶端緩沖區大小; •Group:分組消費時分組名 •auto_commit: offset是否自動提交;

3.Java客戶端實例講解

•需要的軟件環境: 已搭建好的kafka集群、Linux服務器一臺、Apache Maven 3.2.3、 kafka 0.8.1 •分區消費模型的Java實現;
package kafka.consumer.partition;

import kafka.api.FetchRequest;
import kafka.api.FetchRequestBuilder;
import kafka.api.PartitionOffsetRequestInfo;
import kafka.common.ErrorMapping;
import kafka.common.TopicAndPartition;
import kafka.javaapi.*;
import kafka.javaapi.consumer.SimpleConsumer;
import kafka.message.MessageAndOffset;
 
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PartitionConsumerTest {
    public static void main(String args[]) {
        PartitionConsumerTest example = new PartitionConsumerTest();
        long maxReads = Long.MAX_VALUE;
        String topic = "jiketest";
        if(args.length < 1){
            System.out.println("Please assign partition number.");
        }
        
        List<String> seeds = new ArrayList<String>();
        String hosts="10.206.216.13,10.206.212.14,10.206.209.25";
        String[] hostArr = hosts.split(",");
        for(int index = 0;index < hostArr.length;index++){
            seeds.add(hostArr[index].trim());
        }
        
        int port = 19092;
         
        int partLen = Integer.parseInt(args[0]);
        for(int index=0;index < partLen;index++){
            try {
                example.run(maxReads, topic, index/*partition*/, seeds, port);
            } catch (Exception e) {
                System.out.println("Oops:" + e);
                 e.printStackTrace();
            }
        }
    }
    
    private List<String> m_replicaBrokers = new ArrayList<String>();
     
        public PartitionConsumerTest() {
            m_replicaBrokers = new ArrayList<String>();
        }
     
        public void run(long a_maxReads, String a_topic, int a_partition, List<String> a_seedBrokers, int a_port) throws Exception {
            // find the meta data about the topic and partition we are interested in
            //
            PartitionMetadata metadata = findLeader(a_seedBrokers, a_port, a_topic, a_partition);
            if (metadata == null) {
                System.out.println("Can‘t find metadata for Topic and Partition. Exiting");
                return;
            }
            if (metadata.leader() == null) {
                System.out.println("Can‘t find Leader for Topic and Partition. Exiting");
                return;
            }
            String leadBroker = metadata.leader().host();
            String clientName = "Client_" + a_topic + "_" + a_partition;
     
            SimpleConsumer consumer = new SimpleConsumer(leadBroker, a_port, 100000, 64 * 1024, clientName);
            long readOffset = getLastOffset(consumer,a_topic, a_partition, kafka.api.OffsetRequest.EarliestTime(), clientName);
     
            int numErrors = 0;
            while (a_maxReads > 0) {
                if (consumer == null) {
                    consumer = new SimpleConsumer(leadBroker, a_port, 100000, 64 * 1024, clientName);
                }
                FetchRequest req = new FetchRequestBuilder()
                        .clientId(clientName)
                        .addFetch(a_topic, a_partition, readOffset, 100000) // Note: this fetchSize of 100000 might need to be increased if large batches are written to Kafka
                        .build();
                FetchResponse fetchResponse = consumer.fetch(req);
     
                if (fetchResponse.hasError()) {
                    numErrors++;
                    // Something went wrong!
                    short code = fetchResponse.errorCode(a_topic, a_partition);
                    System.out.println("Error fetching data from the Broker:" + leadBroker + " Reason: " + code);
                    if (numErrors > 5) break;
                    if (code == ErrorMapping.OffsetOutOfRangeCode())  {
                        // We asked for an invalid offset. For simple case ask for the last element to reset
                        readOffset = getLastOffset(consumer,a_topic, a_partition, kafka.api.OffsetRequest.LatestTime(), clientName);
                        continue;
                    }
                    consumer.close();
                    consumer = null;
                    leadBroker = findNewLeader(leadBroker, a_topic, a_partition, a_port);
                    continue;
                }
                numErrors = 0;
     
                long numRead = 0;
                for (MessageAndOffset messageAndOffset : fetchResponse.messageSet(a_topic, a_partition)) {
                    long currentOffset = messageAndOffset.offset();
                    if (currentOffset < readOffset) {
                        System.out.println("Found an old offset: " + currentOffset + " Expecting: " + readOffset);
                        continue;
                    }
                    readOffset = messageAndOffset.nextOffset();
                    ByteBuffer payload = messageAndOffset.message().payload();
     
                    byte[] bytes = new byte[payload.limit()];
                    payload.get(bytes);
                    System.out.println(String.valueOf(messageAndOffset.offset()) + ": " + new String(bytes, "UTF-8"));
                    numRead++;
                    a_maxReads--;
                }
     
                if (numRead == 0) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ie) {
                    }
                }
            }
            if (consumer != null) consumer.close();
        }
     
        public static long getLastOffset(SimpleConsumer consumer, String topic, int partition,
                                         long whichTime, String clientName) {
            TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
            Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
            requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
            kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(
                    requestInfo, kafka.api.OffsetRequest.CurrentVersion(), clientName);
            OffsetResponse response = consumer.getOffsetsBefore(request);
     
            if (response.hasError()) {
                System.out.println("Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic, partition) );
                return 0;
            }
            long[] offsets = response.offsets(topic, partition);
            return offsets[0];
        }
     
        private String findNewLeader(String a_oldLeader, String a_topic, int a_partition, int a_port) throws Exception {
            for (int i = 0; i < 3; i++) {
                boolean goToSleep = false;
                PartitionMetadata metadata = findLeader(m_replicaBrokers, a_port, a_topic, a_partition);
                if (metadata == null) {
                    goToSleep = true;
                } else if (metadata.leader() == null) {
                    goToSleep = true;
                } else if (a_oldLeader.equalsIgnoreCase(metadata.leader().host()) && i == 0) {
                    // first time through if the leader hasn‘t changed give ZooKeeper a second to recover
                    // second time, assume the broker did recover before failover, or it was a non-Broker issue
                    //
                    goToSleep = true;
                } else {
                    return metadata.leader().host();
                }
                if (goToSleep) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException ie) {
                    }
                }
            }
            System.out.println("Unable to find new leader after Broker failure. Exiting");
            throw new Exception("Unable to find new leader after Broker failure. Exiting");
        }
     
        private PartitionMetadata findLeader(List<String> a_seedBrokers, int a_port, String a_topic, int a_partition) {
            PartitionMetadata returnMetaData = null;
            loop:
            for (String seed : a_seedBrokers) {
                SimpleConsumer consumer = null;
                try {
                    consumer = new SimpleConsumer(seed, a_port, 100000, 64 * 1024, "leaderLookup");
                    List<String> topics = Collections.singletonList(a_topic);
                    TopicMetadataRequest req = new TopicMetadataRequest(topics);
                    kafka.javaapi.TopicMetadataResponse resp = consumer.send(req);
     
                    List<TopicMetadata> metaData = resp.topicsMetadata();
                    for (TopicMetadata item : metaData) {
                        for (PartitionMetadata part : item.partitionsMetadata()) {
                            if (part.partitionId() == a_partition) {
                                returnMetaData = part;
                                break loop;
                            }
                        }
                    }
                } catch (Exception e) {
                    System.out.println("Error communicating with Broker [" + seed + "] to find Leader for [" + a_topic
                            + ", " + a_partition + "] Reason: " + e);
                } finally {
                    if (consumer != null) consumer.close();
                }
            }
            if (returnMetaData != null) {
                m_replicaBrokers.clear();
                for (kafka.cluster.Broker replica : returnMetaData.replicas()) {
                    m_replicaBrokers.add(replica.host());
                }
            }
            return returnMetaData;
        }
}
•組(Group)消費模型的Java實現;
package kafka.consumer.group;

import kafka.consumer.ConsumerIterator;
import kafka.consumer.KafkaStream;
 
public class ConsumerTest implements Runnable {
    private KafkaStream m_stream;
    private int m_threadNumber;
 
    public ConsumerTest(KafkaStream a_stream, int a_threadNumber) {
        m_threadNumber = a_threadNumber;
        m_stream = a_stream;
    }
 
    public void run() {
        ConsumerIterator<byte[], byte[]> it = m_stream.iterator();
        while (it.hasNext()){
            System.out.println("Thread " + m_threadNumber + ": " + new String(it.next().message()));
            
        }
        System.out.println("Shutting down Thread: " + m_threadNumber);
    }
}
package kafka.consumer.group;

import kafka.consumer.ConsumerConfig;
import kafka.consumer.KafkaStream;
import kafka.javaapi.consumer.ConsumerConnector;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class GroupConsumerTest extends Thread {
    private final ConsumerConnector consumer;
    private final String topic;
    private  ExecutorService executor;
    
    public GroupConsumerTest(String a_zookeeper, String a_groupId, String a_topic){
        consumer = kafka.consumer.Consumer.createJavaConsumerConnector(
                createConsumerConfig(a_zookeeper, a_groupId));
        this.topic = a_topic;
    }
    
    public void shutdown() {
        if (consumer != null) consumer.shutdown();
        if (executor != null) executor.shutdown();
        try {
            if (!executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS)) {
                System.out.println("Timed out waiting for consumer threads to shut down, exiting uncleanly");
            }
        } catch (InterruptedException e) {
            System.out.println("Interrupted during shutdown, exiting uncleanly");
        }
   }
 
    public void run(int a_numThreads) {
        Map<String, Integer> topicCountMap = new HashMap<String, Integer>();
        topicCountMap.put(topic, new Integer(a_numThreads));
        Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCountMap);
        List<KafkaStream<byte[], byte[]>> streams = consumerMap.get(topic);
 
        // now launch all the threads
        //
        executor = Executors.newFixedThreadPool(a_numThreads);
 
        // now create an object to consume the messages
        //
        int threadNumber = 0;
        for (final KafkaStream stream : streams) {
            executor.submit(new ConsumerTest(stream, threadNumber));
            threadNumber++;
        }
    }
    private static ConsumerConfig createConsumerConfig(String a_zookeeper, String a_groupId) {
        Properties props = new Properties();
        props.put("zookeeper.connect", a_zookeeper);
        props.put("group.id", a_groupId);
        props.put("zookeeper.session.timeout.ms", "40000");
        props.put("zookeeper.sync.time.ms", "2000");
        props.put("auto.commit.interval.ms", "1000");
 
        return new ConsumerConfig(props);
    }
    
    public static void main(String[] args) {
        if(args.length < 1){
            System.out.println("Please assign partition number.");
        }
        
        String zooKeeper = "10.206.216.13:12181,10.206.212.14:12181,10.206.209.25:12181";
        String groupId = "jikegrouptest";
        String topic = "jiketest";
        int threads = Integer.parseInt(args[0]);
 
        GroupConsumerTest example = new GroupConsumerTest(zooKeeper, groupId, topic);
        example.run(threads);
 
        try {
            Thread.sleep(Long.MAX_VALUE);
        } catch (InterruptedException ie) {
 
        }
        example.shutdown();
    }
}

4.Java客戶端參數調優

•fetchSize: 從服務器獲取單包大小; •bufferSize: kafka客戶端緩沖區大小; •group.id: 分組消費時分組名

三、Kafka生產者編程模型

1.同步生產模型

技術分享圖片

2.異步生產模型

技術分享圖片

3.兩種生產模型偽代碼描述

main()
  創建到kafka broker的連接:KafkaClient(host,port)
  選擇或者自定義生產者負載均衡算法 partitioner
  設置生產者參數
  根據負載均衡算法和設置的生產者參數構造Producer對象

  while True
    getMessage:從上遊獲得一條消息
    按照kafka要求的消息格式構造kafka消息
     根據分區算法得到分區
    發送消息
    處理異常

4.兩種生產模型對比

同步生產模型: (1)低消息丟失率; (2)高消息重復率(由於網絡原因,回復確認未收到); (3)高延遲 異步生產模型: (1)低延遲; (2)高發送性能; (3)高消息丟失率(無確認機制,發送端隊列滿)

四、Kafka生產者的Python和Java客戶端實現

1.Python客戶端實例講解

•需要的軟件環境: 已搭建好的kafka集群、Linux服務器一臺、Python2.7.6 、 kafka-Python軟件包 main.py
# -*- coding: utf-8 -*-
"""
This module provide kafka sync and async producer demo example.


"""

import logging, time

from async.ASyncProducer import ASyncProducer
from sync.SyncProducer import SyncProducer


def main():
    threads = [
        ASyncProducer(),
        SyncProducer()
    ]

    for t in threads:
        t.start()

    time.sleep(5000)

if __name__ == "__main__":
    #logging.basicConfig(
    #    format=‘%(asctime)s.%(msecs)s:%(name)s:%(thread)d:%(levelname)s:%(process)d:%(message)s‘,
    #    level=logging.INFO
    #    )
    main()
•同步生產模型的Python實現 SyncProducer.py
# -*- coding: utf-8 -*-
"""
This module provide kafka partition and group consumer demo example.

"""

import threading, time

from kafka.client import KafkaClient
from kafka.producer import SimpleProducer
from kafka.partitioner import HashedPartitioner

class SyncProducer(threading.Thread):
    daemon = True

    def run(self):
        client = KafkaClient("10.206.216.13:19092,10.206.212.14:19092,10.206.209.25:1909")
        producer = SimpleProducer(client)
        #producer = KeyedProducer(client,partitioner=HashedPartitioner)

        while True:
            producer.send_messages(jiketest, "test")
            producer.send_messages(jiketest, "test")

            time.sleep(1)
•異步生產模型的Python實現 ASyncProducer.py
# -*- coding: utf-8 -*-
"""
This module provide kafka partition and group consumer demo example.

"""

import threading, time

from kafka.client import KafkaClient
from kafka.producer import SimpleProducer

class ASyncProducer(threading.Thread):
    daemon = True

    def run(self):
        client = KafkaClient("10.206.216.13:19092,10.206.212.14:19092,10.206.209.25:19092")
        producer = SimpleProducer(client,async=True)

        while True:
            producer.send_messages(jiketest, "test")
            producer.send_messages(jiketest, "test")

            time.sleep(1)

2.Python客戶端參數調優

•req_acks:發送失敗重試次數; •ack_timeout: 未接到確認,認為發送失敗的時間; •async : 是否異步發送; •batch_send_every_n: 異步發送時,累計最大消息數; •batch_send_every_t:異步發送時,累計最大時間;

3.Java客戶端實例講解

•需要的軟件環境: 已搭建好的kafka集群、Linux服務器一臺、Apache Maven 3.2.3、 kafka 0.8.1 SimplePartitioner.java
package kafka.producer.partiton;

import kafka.producer.Partitioner;
import kafka.utils.VerifiableProperties;
 
public class SimplePartitioner implements Partitioner {
    public SimplePartitioner (VerifiableProperties props) {
 
    }
 
    public int partition(Object key, int a_numPartitions) {
        int partition = 0;
        String stringKey = (String) key;
        int offset = stringKey.lastIndexOf(‘.‘);
        if (offset > 0) {
           partition = Integer.parseInt( stringKey.substring(offset+1)) % a_numPartitions;
        }
       return partition;
  }
 
}
•同步模型的Java實現
package kafka.producer.sync;
import java.util.*;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;

public class SyncProduce {
    public static void main(String[] args) {
        long events = Long.MAX_VALUE;
        Random rnd = new Random();
 
        Properties props = new Properties();
        props.put("metadata.broker.list", "10.206.216.13:19092,10.206.212.14:19092,10.206.209.25:19092");
        props.put("serializer.class", "kafka.serializer.StringEncoder");
        //kafka.serializer.DefaultEncoder
        props.put("partitioner.class", "kafka.producer.partiton.SimplePartitioner");
        //kafka.producer.DefaultPartitioner: based on the hash of the key
        props.put("request.required.acks", "1");
        //0;  絕不等確認  1:   leader的一個副本收到這條消息,並發回確認 -1:   leader的所有副本都收到這條消息,並發回確認
 
        ProducerConfig config = new ProducerConfig(props);
 
        Producer<String, String> producer = new Producer<String, String>(config);
 
        for (long nEvents = 0; nEvents < events; nEvents++) { 
               long runtime = new Date().getTime();  
               String ip = "192.168.2." + rnd.nextInt(255); 
               String msg = runtime + ",www.example.com," + ip; 
               //eventKey必須有(即使自己的分區算法不會用到這個key,也不能設為null或者""),否者自己的分區算法根本得不到調用
               KeyedMessage<String, String> data = new KeyedMessage<String, String>("jiketest", ip, msg);
                                                               //             eventTopic, eventKey, eventBody
               producer.send(data);
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException ie) {
               }
        }
        producer.close();
    }
}
•異步模型的Java實現 ASyncProduce.java
package kafka.producer.async;

import java.util.*;

import kafka.javaapi.producer.Producer;
import kafka.producer.KeyedMessage;
import kafka.producer.ProducerConfig;


public class ASyncProduce {
    public static void main(String[] args) {
        long events = Long.MAX_VALUE;
        Random rnd = new Random();
 
        Properties props = new Properties();
        props.put("metadata.broker.list", "10.206.216.13:19092,10.206.212.14:19092,10.206.209.25:19092");
        props.put("serializer.class", "kafka.serializer.StringEncoder");
        //kafka.serializer.DefaultEncoder
        props.put("partitioner.class", "kafka.producer.partiton.SimplePartitioner");
        //kafka.producer.DefaultPartitioner: based on the hash of the key
        //props.put("request.required.acks", "1");
        props.put("producer.type", "async");
        //props.put("producer.type", "1");
        // 1: async 2: sync
 
        ProducerConfig config = new ProducerConfig(props);
 
        Producer<String, String> producer = new Producer<String, String>(config);
 
        for (long nEvents = 0; nEvents < events; nEvents++) { 
               long runtime = new Date().getTime();  
               String ip = "192.168.2." + rnd.nextInt(255); 
               String msg = runtime + ",www.example.com," + ip; 
               KeyedMessage<String, String> data = new KeyedMessage<String, String>("jiketest", ip, msg);
               producer.send(data);
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException ie) {
               }
        }
        producer.close();
    }
}

4.Java客戶端參數調優

•message.send.max.retries: 發送失敗重試次數; •retry.backoff.ms :未接到確認,認為發送失敗的時間; •producer.type: 同步發送或者異步發送; •batch.num.messages: 異步發送時,累計最大消息數; •queue.buffering.max.ms:異步發送時,累計最大時間;

二、Kafka基礎實戰:消費者和生產者實例