大數據技術之_10_Kafka學習_Kafka概述+Kafka集群部署+Kafka工作流程分析+Kafka API實戰+Kafka Producer攔截器+Kafka Streams


第1章 Kafka概述1.1 消息隊列1.2 為什么需要消息隊列1.3 什么是Kafka1.4 Kafka架構第2章 Kafka集群部署2.1 環境准備2.1.1 集群規划2.1.2 jar包下載2.2 Kafka集群部署2.3 Kafka命令行操作第3章 Kafka工作流程分析3.1 Kafka 生產過程分析3.1.1 寫入方式3.1.2 分區(Partition)3.1.3 副本(Replication)3.1.4 寫入流程3.2 Broker 保存消息3.2.1 存儲方式3.2.2 存儲策略3.2.3 Zookeeper存儲結構3.3 Kafka 消費過程分析3.3.1 高級API3.3.2 低級API3.3.3 消費者組3.3.4 消費方式3.3.5 消費者組案例第4章 Kafka API實戰4.1 環境准備4.2 Kafka生產者Java API4.2.1 創建生產者(過時的API)4.2.2 創建生產者(新的API)4.2.3 創建生產者帶回調函數(新的API)4.2.4 自定義分區生產者4.3 Kafka消費者Java API4.3.1 高級API4.3.2 低級API第5章 Kafka Producer攔截器(interceptor)5.1 攔截器原理5.2 攔截器案例第6章 Kafka Streams6.1 概述6.1.1 Kafka Streams6.1.2 Kafka Streams 特點6.1.3 為什么要有 Kafka Stream?6.2 Kafka Stream 數據清洗案例第7章 擴展知識7.1 Kafka 與 Flume 比較7.2 Flume 與 kafka 集成7.3 Kafka配置信息7.3.1 Broker 配置信息7.3.2 Producer 配置信息7.3.3 Consumer 配置信息7.4 如何查看 Kafka 集群維護的 offset 信息


第1章 Kafka概述

1.1 消息隊列

1)點對點模式(一對一,消費者主動拉取數據,消息收到后消息清除)
  點對點模型通常是一個基於拉取或者輪詢的消息傳送模型,這種模型從隊列中請求信息,而不是將消息推送到客戶端。這個模型的特點是發送到隊列的消息被一個且只有一個接收者接收處理,即使有多個消息監聽者也是如此。

2)發布/訂閱模式(一對多,數據生產后,推送給所有訂閱者)
  發布訂閱模型則是一個基於推送的消息傳送模型。發布訂閱模型可以有多種不同的訂閱者,臨時訂閱者只在主動監聽主題時才接收消息,而持久訂閱者則監聽主題的所有消息,即使當前訂閱者不可用,處於離線狀態。

1.2 為什么需要消息隊列

1)解耦:
  允許你獨立的擴展或修改兩邊的處理過程,只要確保它們遵守同樣的接口約束。

2)冗余:
  消息隊列把數據進行持久化直到它們已經被完全處理,通過這一方式規避了數據丟失風險。許多消息隊列所采用的"插入-獲取-刪除"范式中,在把一個消息從隊列中刪除之前,需要你的處理系統明確的指出該消息已經被處理完畢,從而確保你的數據被安全的保存直到你使用完畢。

3)擴展性:
  因為消息隊列解耦了你的處理過程,所以增大消息入隊和處理的頻率是很容易的,只要另外增加處理過程即可。

4)靈活性 & 峰值處理能力:
  在訪問量劇增的情況下,應用仍然需要繼續發揮作用,但是這樣的突發流量並不常見。如果為以能處理這類峰值訪問為標准來投入資源隨時待命無疑是巨大的浪費。使用消息隊列能夠使關鍵組件頂住突發的訪問壓力,而不會因為突發的超負荷的請求而完全崩潰。

5)可恢復性:
  系統的一部分組件失效時,不會影響到整個系統。消息隊列降低了進程間的耦合度,所以即使一個處理消息的進程掛掉,加入隊列中的消息仍然可以在系統恢復后被處理。

6)順序保證:
  在大多使用場景下,數據處理的順序都很重要。大部分消息隊列本來就是排序的,並且能保證數據會按照特定的順序來處理。(Kafka保證一個Partition內的消息的有序性)

7)緩沖:
  有助於控制和優化數據流經過系統的速度,解決生產消息和消費消息的處理速度不一致的情況。

8)異步通信:
  很多時候,用戶不想也不需要立即處理消息。消息隊列提供了異步處理機制,允許用戶把一個消息放入隊列,但並不立即處理它。想向隊列中放入多少消息就放多少,然后在需要的時候再去處理它們。

1.3 什么是Kafka

  Kafka 是最初由 Linkedin 公司開發,是一個分布式、支持分區的(partition)、多副本的(replica),基於 zookeeper 協調的分布式消息系統,它的最大的特性就是可以實時的處理大量數據以滿足各種需求場景:比如基於hadoop的批處理系統、低延遲的實時系統、storm/Spark流式處理引擎,web/nginx日志、訪問日志,消息服務等等,用scala語言編寫,Linkedin 於 2010 年貢獻給了 Apache 基金會並成為頂級開源項目。
  在流式計算中,Kafka 一般用來緩存數據,Storm通過消費Kafka的數據進行計算。
  Kafka 是基於點對點模式的消息隊列。

  1)Apache Kafka是一個開源消息系統由 Scala 寫成。是由 Apache 軟件基金會開發的一個開源消息系統項目。

  2)Kafka 最初是由LinkedIn公司開發,並於 2011 年初開源。2012 年 10月從Apache Incubator畢業,並成為頂級開源項目。該項目的目標是為處理實時數據提供一個統一、高通量、低等待的平台。

  3)Kafka 是一個分布式消息隊列。Kafka 對消息保存時根據 Topic 進行歸類,發送消息者稱為 Producer,消息接受者稱為Consumer,此外 kafka 集群有多個 kafka 實例組成,每個實例(server)稱為 broker。真正存儲數據的地方叫做 Topic。

  4)無論是 kafka 集群,還是 Consumer 都依賴於Zookeeper集群保存一些meta信息,來保證系統可用性。

1.4 Kafka架構

Kafka整體架構圖


Kafka整體架構圖詳解

  1)Producer :消息生產者,就是向 kafka broker 發消息的客戶端。

  2)Consumer :消息消費者,向 kafka broker 取消息的客戶端。

  3)Topic :可以理解為一個隊列。

  4)Consumer Group(CG):這是 kafka 用來實現一個 topic 消息的廣播(發給所有的 consumer)和單播(發給任意一個 consumer)的手段。一個 topic 可以有多個 CG。topic 的消息會復制(不是真的復制,是概念上的)到所有的 CG,但每個 partion 只會把消息發給該 CG 中的一個 consumer。如果需要實現廣播,只要每 consumer 有一個獨立的 CG 就可以了。要實現單播只要所有的 consumer 在同一個CG。用CG還可以將 consumer 進行自由的分組而不需要多次發送消息到不同的 topic

  5)Broker :一台 kafka 服務器就是一個 broker。一個集群由多個 broker 組成。一個 broker 可以容納多個 topic。

  6)Partition:為了實現擴展性,一個非常大的 topic 可以分布到多個 broker(即服務器)上,一個 topic 可以分為多個 partition,每個 partition 是一個有序的隊列。partition 中的每條消息都會被分配一個有序的id(offset)。將消息發給 consumer,kafka 只保證按一個 partition 中的消息的順序,不保證一個 topic 的整體(多個 partition 間)的順序。

  7)Offset:kafka 的存儲文件都是按照 offset.kafka 來命名,用 offset 做名字的好處是方便查找。例如你想找位於 2049 的位置,只要找到 2048.kafka 的文件即可。當然 the first offset 就是 00000000000.kafka。

  8)分區對於 Kafka 集群的好處是:實現負載均衡。分區對於消費者來說,可以提高並發度,提高效率。在公司中應用的時候,針對於某一個 Topic,它有幾個分區(n個),我們就對應的建一個有幾個消費者的消費者組(m個)。即:n大於或者等於m,最好是n=m。當n>m時,就意味着某一個消費者會消費多個分區的數據。不僅如此,一個消費者還可以消費多個 Topic 數據。

第2章 Kafka集群部署

2.1 環境准備

2.1.1 集群規划

hadoop102                    hadoop103               hadoop104
zk                            zk                      zk
kafka                        kafka                   kafka

2.1.2 jar包下載

  http://kafka.apache.org/downloads.html

2.2 Kafka集群部署

1)解壓安裝包

[atguigu@hadoop102 software]$ tar -zxvf kafka_2.11-0.11.0.2.tgz -C /opt/module/

2)修改解壓后的文件名稱

[atguigu@hadoop102 module]$ mv kafka_2.11-0.11.0.2/ kafka

3)在/opt/module/kafka目錄下創建logs文件夾

[atguigu@hadoop102 kafka]$ mkdir logs

4)修改配置文件

[atguigu@hadoop102 kafka]$ cd config/
[atguigu@hadoop102 config]$ vim server.properties

輸入以下內容:

#broker的全局唯一編號,不能重復
broker.id=0

#刪除topic功能使能
delete.topic.enable=true

#處理網絡請求的線程數量
num.network.threads=3

#用來處理磁盤IO的線程數量
num.io.threads=8

#發送套接字的緩沖區大小
socket.send.buffer.bytes=102400

#接收套接字的緩沖區大小
socket.receive.buffer.bytes=102400

#請求套接字的緩沖區大小
socket.request.max.bytes=104857600

#kafka運行日志存放的路徑    
log.dirs=/opt/module/kafka/logs

#topic在當前broker上的分區個數
num.partitions=1

#用來恢復和清理data下數據的線程數量
num.recovery.threads.per.data.dir=1

#segment文件保留的最長時間,超時將被刪除(單位小時)
log.retention.hours=168

#配置連接Zookeeper集群地址
zookeeper.connect=hadoop102:2181,hadoop103:2181,hadoop104:2181

5)配置環境變量

[atguigu@hadoop102 module]$ sudo vim /etc/profile

#KAFKA_HOME
export KAFKA_HOME=/opt/module/kafka
export PATH=$PATH:$KAFKA_HOME/bin

[atguigu@hadoop102 module]$ source /etc/profile

6)分發安裝包

[atguigu@hadoop102 module]$ xsync kafka/

注意:分發之后記得配置其他機器的環境變量。
7)分別在hadoop103和hadoop104上修改配置文件/opt/module/kafka/config/server.properties中的broker.id=1、broker.id=2
注:broker.id不得重復。
8)啟動Kafka集群
依次在hadoop102、hadoop103、hadoop104節點上啟動kafka

[atguigu@hadoop102 kafka]$ bin/kafka-server-start.sh config/server.properties &
[atguigu@hadoop103 kafka]$ bin/kafka-server-start.sh config/server.properties &
[atguigu@hadoop104 kafka]$ bin/kafka-server-start.sh config/server.properties &

啟動Kafka是一個阻塞進程,會打印我們操作kafka的日志,我們可以把窗口放到后台,在命令后面加一個與&符號,將該阻塞進程放到后台。

寫群起Kafka集群腳本的時候,需要使用-daemon命令,具體如下:
[atguigu@hadoop102 kafka]$ bin/kafka-server-start.sh -daemon config/server.properties
[atguigu@hadoop103 kafka]$ bin/kafka-server-start.sh -daemon config/server.properties
[atguigu@hadoop104 kafka]$ bin/kafka-server-start.sh -daemon config/server.properties

-daemon 表示守護進程,會將日志打印在后台。

9)關閉Kafka集群

[atguigu@hadoop102 kafka]$ bin/kafka-server-stop.sh stop
[atguigu@hadoop103 kafka]$ bin/kafka-server-stop.sh stop
[atguigu@hadoop104 kafka]$ bin/kafka-server-stop.sh stop

寫群起Kafka集群腳本的時候,需要使用-daemon命令,具體如下:
[atguigu@hadoop102 kafka]$ bin/kafka-server-stop.sh -daemon
[atguigu@hadoop103 kafka]$ bin/kafka-server-stop.sh -daemon
[atguigu@hadoop104 kafka]$ bin/kafka-server-stop.sh -daemon

2.3 Kafka命令行操作

0)補充知識

jps         查看當前進程
jps -l      查看當前進程所屬主類

`注意:`當有很多進程都是同一個名字,我們該如何區分?
`答:`每一次啟動一個進程后,我們將該進程與對應的進程ID寫入一個文檔中。如果某一個進程出現問題或者某一個框架出現問題,便於我們kill掉相應的進程。不至於關閉整個系統。(生產環境下一般不允許關閉或重啟整個系統!)

1)查看當前服務器中的所有topic

[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 --list

2)創建topic

[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 \
--create --replication-factor 3 --partitions 1 --topic first

bin/kafka-topics.sh --zookeeper hadoop102:2181 \
--create --replication-factor 2 --partitions 3 --topic second

選項說明:
  --topic 定義topic名
  --replication-factor 定義副本數(注:副本數不能大於節點數,否則會報錯!
  --partitions 定義分區數

3)刪除topic

[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 \
--delete --topic first

注意:需要server.properties中設置delete.topic.enable=true否則只是標記刪除或者直接重啟。

4)發送消息(生產者連接的是kafka集群默認的端口號是:9092)

[atguigu@hadoop102 kafka]$ bin/kafka-console-producer.sh \
--broker-list hadoop102:9092 --topic first
>hello world
>atguigu  atguigu

`注意:`生產者連接的是kafka集群。

5)消費消息

老版本
[atguigu@hadoop103 kafka]$ bin/kafka-console-consumer.sh \
--zookeeper hadoop102:2181 --from-beginning --topic first

或者

新版本
[atguigu@hadoop103 kafka]$ bin/kafka-console-consumer.sh \
--bootstrap-server hadoop102:9092 --from-beginning --topic first

`注意:`消費者會將自己的 offset 文件保存在 zookeeper(低版本的 kafka)。所以消費者連接的是 zookeeper。

--from-beginning:會把first主題中以往所有的數據都讀取出來。根據業務場景選擇是否增加該配置。如果不加該配置,那么消費者消費的消息將是最新的消息(不包括以往的所有數據)。

6)查看某個topic的詳情

[atguigu@hadoop102 kafka]$ bin/kafka-topics.sh --zookeeper hadoop102:2181 \
--describe --topic first
Topic:first    PartitionCount:1    ReplicationFactor:3 Configs:
    Topic: first    Partition: 0    Leader: 2   Replicas: 2,0,1 Isr: 2,0,1

Isr的作用:當 leader 掛掉后,選舉新 leader 時使用的。Isr 的排序規則是:與 leader 的相似度,越高越在前,越在前越有可能成為新 leader。

7)警告問題解釋

[atguigu@hadoop103 kafka]$ bin/kafka-console-consumer.sh \
> --zookeeper hadoop102:2181 --from-beginning --topic first
Using the ConsoleConsumer with old consumer is deprecated and will be removed in a future major release. Consider using the new consumer by passing [bootstrap-server] instead of [zookeeper].

在高版本的kafka中,消費者會將自己的 offset文件 保存在 kafka 集群的本地,不交給 zookeeper 維護了!如下圖所示:


這樣做的好處是:提高了效率,減少了網絡傳輸。

第3章 Kafka工作流程分析

3.1 Kafka 生產過程分析

3.1.1 寫入方式

  producer 采用推(push)模式將消息發布到 broker,每條消息都被追加(append)到分區(patition)中,屬於順序寫磁盤(順序寫磁盤效率比隨機寫內存要高,保障kafka吞吐率)。

3.1.2 分區(Partition)

  消息發送時都被發送到一個 topic,其本質就是一個目錄,而topic是由一些 Partition Logs(分區日志)組成,其組織結構如下圖所示:

  我們可以看到,每個 Partition 中的消息都是有序的,生產的消息被不斷追加到 Partition log 上,其中的每一個消息都被賦予了一個唯一的 offset值
1)分區的原因
  (1)方便在集群中擴展,每個 Partition 可以通過調整以適應它所在的機器,而一個topic又可以有多個 Partition 組成,因此整個集群就可以適應任意大小的數據了。
  (2)可以提高並發,因為可以以 Partition 為單位讀寫了。

2)分區的原則
  (1)指定了 patition,則直接使用。
  (2)未指定 patition 但指定 key,通過對 key 的 value 進行 hash 出一個 patition。
  (3)patition 和 key 都未指定,使用輪詢選出一個 patition。

DefaultPartitioner類
public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
    List<PartitionInfo> partitions = cluster.partitionsForTopic(topic);
    int numPartitions = partitions.size();
    if (keyBytes == null) {
        int nextValue = nextValue(topic);
        List<PartitionInfo> availablePartitions = cluster.availablePartitionsForTopic(topic);
        if (availablePartitions.size() > 0) {
            int part = Utils.toPositive(nextValue) % availablePartitions.size();
            return availablePartitions.get(part).partition();
        } else {
            // no partitions are available, give a non-available partition
            return Utils.toPositive(nextValue) % numPartitions;
        }
    } else {
        // hash the keyBytes to choose a partition
        return Utils.toPositive(Utils.murmur2(keyBytes)) % numPartitions;
    }
}

3.1.3 副本(Replication)

  同一個 partition 可能會有多個 replication(對應 server.properties 配置中的 default.replication.factor=N)。沒有 replication 的情況下,一旦 broker 宕機,其上所有 patition 的數據都不可被消費,同時 producer 也不能再將數據存於其上的 partition。引入 replication 之后,同一個 partition 可能會有多個 replication,而這時需要在這些 replication 之間選出一個 leader,producer 和 consumer 只與這個 leader 交互,其它 replication 作為 follower 從leader 中復制數據。

3.1.4 寫入流程

producer寫入消息流程如下:

  1)producer 先從 zookeeper 的 "/brokers/…/state"節點找到該 partition 的 leader
  2)producer 將消息發送給該 leader
  3)leader 將消息寫入本地 log
  4)followers 從 leader pull 消息,寫入本地 log 后向 leader 發送 ACK
  5)leader 收到所有ISR中的 replication 的 ACK 后,增加 HW(high watermark,最后 commit 的offset)並向 producer 發送 ACK
  注意:要特別注意ACK應答模式!

3.2 Broker 保存消息

3.2.1 存儲方式

  物理上把 topic 分成一個或多個 patition(對應 server.properties 中的num.partitions=3配置),每個 patition 物理上對應一個文件夾(該文件夾存儲該 patition 的所有消息和索引文件),如下:

[atguigu@hadoop102 logs]$ cd first-0/
[atguigu@hadoop102 first-0]$ ll
總用量 16
-rw-rw-r--. 1 atguigu atguigu   0 3月   4 19:34 00000000000000000000.index
-rw-rw-r--. 1 atguigu atguigu 225 3月   4 18:27 00000000000000000000.log
-rw-rw-r--. 1 atguigu atguigu  12 3月   4 19:34 00000000000000000000.timeindex
-rw-rw-r--. 1 atguigu atguigu  10 3月   4 19:34 00000000000000000003.snapshot
-rw-rw-r--. 1 atguigu atguigu   8 3月   4 18:24 leader-epoch-checkpoint
[atguigu@hadoop102 first-0]$ 

3.2.2 存儲策略

  無論消息是否被消費,kafka 都會保留所有消息。有兩種策略可以刪除舊數據:
    1)基於時間:log.retention.hours=168 (單位是小時,168小時即7天)
    2)基於大小:log.retention.bytes=1073741824
  需要注意的是,因為 Kafka 讀取特定消息的時間復雜度為O(1),即與文件大小無關,所以這里刪除過期文件與提高 Kafka 性能無關。

3.2.3 Zookeeper存儲結構


注意:producer 不在zk中注冊,消費者在zk中注冊。

3.3 Kafka 消費過程分析

  kafka提供了兩套 consumer API:高級 Consumer API 和低級 Consumer API。

3.3.1 高級API

1)高級API優點
  高級 API 寫起來簡單。
  不需要自行去管理 offset,系統通過 zookeeper 自行管理。
  不需要管理分區、副本等情況,系統自動管理。
  消費者斷線會自動根據上一次記錄在 zookeeper 中的 offset 去接着獲取數據(默認設置1分鍾更新一下 zookeeper 中存的 offset)。
  可以使用 group 來區分對同一個 topic 的不同程序的訪問分離開來(不同的 group 記錄不同的 offset,這樣不同程序讀取同一個 topic 才不會因為 offset 互相影響)。

2)高級API缺點
  不能自行控制offset(對於某些特殊需求來說)。
  不能細化控制如分區、副本、zk等。

3.3.2 低級API

1)低級 API 優點
  能夠讓開發者自己控制 offset,想從哪里讀取就從哪里讀取。
  自行控制連接分區,對分區自定義進行負載均衡。
  對 zookeeper 的依賴性降低(如:offset 不一定非要靠zk存儲,自行存儲 offset 即可,比如存在文件或者內存中)。

2)低級API缺點
  太過復雜,需要自行控制 offset,連接哪個分區,找到分區 leader 等。

3.3.3 消費者組


  消費者是以 consumer group 消費者組的方式工作,由一個或者多個消費者組成一個組,共同消費一個 topic。每個分區在同一時間只能由 group 中的一個消費者讀取,但是多個 group 可以同時消費這個 partition。在圖中,有一個由三個消費者組成的 group,有一個消費者讀取主題中的兩個分區,另外兩個分別讀取一個分區。某個消費者讀取某個分區,也可以叫做某個消費者是某個分區的擁有者。
  在這種情況下,消費者可以通過 水平擴展的方式同時讀取大量的消息。另外,如果一個消費者失敗了,那么其他的 group 成員會自動負載均衡讀取之前失敗的消費者讀取的分區。

3.3.4 消費方式

  consumer采用 pull(拉)模式從 broker 中讀取數據。
  push(推)模式很難適應消費速率不同的消費者,因為消息發送速率是由 broker 決定的。它的目標是盡可能以最快速度傳遞消息,但是這樣很容易造成 consumer 來不及處理消息,典型的表現就是拒絕服務以及網絡擁塞。而 pull 模式則可以根據 consumer 的消費能力以適當的速率消費消息。
  對於 Kafka 而言,pull 模式更合適,它可簡化 broker 的設計,consumer 可自主控制消費消息的速率,同時 consumer 可以自己控制消費方式--即可批量消費也可逐條消費,同時還能選擇不同的提交方式從而實現不同的傳輸語義
  pull 模式不足之處是,如果kafka沒有數據,消費者可能會陷入循環中,一直等待數據到達。為了避免這種情況,我們在我們的拉請求中有參數,允許消費者請求在等待數據到達的“長輪詢”中進行阻塞(並且可選地等待給定的字節數,以確保傳輸大小)。

3.3.5 消費者組案例

1)需求:測試同一個消費者組中的消費者,同一時刻只能有一個消費者消費。
2)案例實操:
(1)在hadoop102、hadoop103上修改/opt/module/kafka/config/consumer.properties配置文件中的group.id屬性為任意組名。

[atguigu@hadoop103 config]$ vim consumer.properties

group.id=atguigu

(2)在hadoop102、hadoop103上分別啟動消費者

[atguigu@hadoop102 kafka]$ bin/kafka-console-consumer.sh \
--zookeeper hadoop102:2181 --topic first --consumer.config config/consumer.properties

[atguigu@hadoop103 kafka]$ bin/kafka-console-consumer.sh \
--zookeeper hadoop102:2181 --topic first --consumer.config config/consumer.properties

(3)在hadoop104上啟動生產者

[atguigu@hadoop104 kafka]$ bin/kafka-console-producer.sh \
--broker-list hadoop102:9092 --topic first
>hello world

(4)查看hadoop102和hadoop103的接收者。
  結論:同一時刻只有一個消費者接收到消息。

第4章 Kafka API實戰

4.1 環境准備

1)啟動zk集群和kafka集群,在kafka集群中打開一個消費者

[atguigu@hadoop102 kafka]$ bin/kafka-console-consumer.sh \
--zookeeper hadoop102:2181 --topic first

2)導入pom依賴

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>0.11.0.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka -->
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.12</artifactId>
        <version>0.11.0.2</version>
    </dependency>
</dependencies>

4.2 Kafka生產者Java API

4.2.1 創建生產者(過時的API)

package com.atguigu.kafka.producer;

import kafka.producer.KeyedMessage;

import java.util.Properties;

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

/**
 * @author chenmingjun
 * @date 2019-03-05 11:39
 */

public class OldProducer {

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {

        Properties props = new Properties();
        props.put("metadata.broker.list""hadoop102:9092");
        props.put("request.required.acks""1");
        props.put("serializer.class""kafka.serializer.StringEncoder");

        Producer<Integer, String> producer = new Producer<Integer, String>(new ProducerConfig(props));

        KeyedMessage<Integer, String> message = new KeyedMessage<Integer, String>("first""hello world");

        producer.send(message);
    }
}

4.2.2 創建生產者(新的API)

package com.atguigu.kafka.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @author chenmingjun
 * @date 2019-03-05 11:42
 */

public class NewProducer {
    public static void main(String[] args) {

        Properties props = new Properties();

        // Kafka服務端的主機名和端口號
        props.put("bootstrap.servers""hadoop102:9092");

        // 等待所有副本節點的應答(應答級別)all等價於-1
        props.put("acks""all");
        // props.put(ProducerConfig.ACKS_CONFIG, "all"); // 二者等價

        // 消息發送最大嘗試次數
        props.put("retries"0);

        // 一批消息處理大小
        props.put("batch.size"16384);

        // 請求延時
        props.put("linger.ms"1);

        // 發送緩存區內存大小(32M)
        props.put("buffer.memory"33554432);

        // key序列化
        props.put("key.serializer""org.apache.kafka.common.serialization.StringSerializer");

        // value序列化
        props.put("value.serializer""org.apache.kafka.common.serialization.StringSerializer");

        // 創建生產者對象
        Producer<String, String> producer = new KafkaProducer<String, String>(props);
        // 測試循環發送數據
        for (int i = 0; i < 50; i++) {
            producer.send(new ProducerRecord<String, String>("first", Integer.toString(i), "hello world-" + i));
        }

        // 關閉資源
        producer.close();
    }
}

4.2.3 創建生產者帶回調函數(新的API)

package com.atguigu.kafka.producer;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;
import java.util.concurrent.Future;

/**
 * @author chenmingjun
 * @date 2019-03-05 14:19
 */

public class CallBackNewProducer {
    public static void main(String[] args) {

        Properties props;
        props = new Properties();

        // Kafka服務端的主機名和端口號
        props.put("bootstrap.servers""hadoop102:9092");

        // 等待所有副本節點的應答(應答級別)all等價於-1
        props.put("acks""all");
        // props.put(ProducerConfig.ACKS_CONFIG, "all"); // 二者等價

        // 消息發送最大嘗試次數
        props.put("retries"0);

        // 一批消息處理大小
        props.put("batch.size"16384);

        // 請求延時
        props.put("linger.ms"1);

        // 發送緩存區內存大小(32M)
        props.put("buffer.memory"33554432);

        // key序列化
        props.put("key.serializer""org.apache.kafka.common.serialization.StringSerializer");

        // value序列化
        props.put("value.serializer""org.apache.kafka.common.serialization.StringSerializer");

        // 創建生產者對象
        Producer<String, String> producer = new KafkaProducer<String, String>(props);
        // 測試循環發送數據
        for (int i = 0; i < 50; i++) {

            producer.send(new ProducerRecord<String, String>("second""hello world-" + i), new Callback() {

                @Override
                public void onCompletion(RecordMetadata metadata, Exception exception) {

                    if (metadata != null) {

                        System.err.println(metadata.partition() + "---" + metadata.offset());
                    }
                }
            });
        }

        // 關閉資源
        producer.close();
    }
}

4.2.4 自定義分區生產者

0)需求:將所有數據存儲到topic的第0號分區上。
1)定義一個類實現Partitioner接口,重寫里面的方法(過時API)

package com.atguigu.kafka.producer;

import kafka.producer.Partitioner;

/**
 * @author chenmingjun
 * @date 2019-03-05 14:47
 */

public class PartitionerOldProducer implements Partitioner {

    public PartitionerOldProducer() {
        super();
    }

    @Override
    public int partition(Object key, int numPartitions) {
        // 控制分區
        return 0;
    }
}

2)自定義分區(新API)

package com.atguigu.kafka.producer;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * @author chenmingjun
 * @date 2019-03-05 14:51
 */

public class PartitionerNewProducer implements Partitioner {
    @Override
    public void configure(Map<String, ?> configs) {

    }

    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        // 控制分區
        return 0;
    }

    @Override
    public void close() {

    }
}

3)在代碼中調用

package com.atguigu.kafka.producer;

import org.apache.kafka.clients.producer.*;

import java.util.Properties;
import java.util.concurrent.Future;

/**
 * @author chenmingjun
 * @date 2019-03-05 14:19
 */

public class CallBackNewProducer {
    public static void main(String[] args) {

        Properties props;
        props = new Properties();

        // Kafka服務端的主機名和端口號
        props.put("bootstrap.servers""hadoop102:9092");

        // 等待所有副本節點的應答(應答級別)all等價於-1
        props.put("acks""all");
        // props.put(ProducerConfig.ACKS_CONFIG, "all"); // 二者等價

        // 消息發送最大嘗試次數
        props.put("retries"0);

        // 一批消息處理大小
        props.put("batch.size"16384);

        // 請求延時
        props.put("linger.ms"1);

        // 發送緩存區內存大小(32M)
        props.put("buffer.memory"33554432);

        // key序列化
        props.put("key.serializer""org.apache.kafka.common.serialization.StringSerializer");

        // value序列化
        props.put("value.serializer""org.apache.kafka.common.serialization.StringSerializer");

        // 關聯自定義分區
        props.put("partitioner.class""com.atguigu.kafka.producer.PartitionerNewProducer");

        // 創建生產者對象
        Producer<String, String> producer = new KafkaProducer<String, String>(props);
        // 測試循環發送數據
        for (int i = 0; i < 50; i++) {

            producer.send(new ProducerRecord<String, String>("second""hello world-" + i), new Callback() {

                public void onCompletion(RecordMetadata metadata, Exception exception) {

                    if (metadata != null) {

                        System.err.println(metadata.partition() + "---" + metadata.offset());
                    }
                }
            });
        }

        // 關閉資源
        producer.close();
    }
}

4)測試
(1)在hadoop102上監控/opt/module/kafka/logs/目錄下second主題2個分區的log日志動態變化情況

[atguigu@hadoop102 second-0]$ tail -f 00000000000000000000.log
[atguigu@hadoop102 second-1]$ tail -f 00000000000000000000.log

(2)發現數據都存儲到指定的分區了。

4.3 Kafka消費者Java API

4.3.1 高級API

0)在控制台創建發送者

[atguigu@hadoop104 kafka]$ bin/kafka-console-producer.sh \
--broker-list hadoop102:9092 --topic second
>hello world

1)創建消費者(過時API)

package com.atguigu.kafka.consumer;

import kafka.consumer.Consumer;
import kafka.consumer.ConsumerConfig;
import kafka.consumer.ConsumerIterator;
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;

/**
 * @author chenmingjun
 * @date 2019-03-05 15:04
 */

public class OldConsumer {

    @SuppressWarnings("deprecation")
    public static void main(String[] args) {

        Properties props = new Properties();

        props.put("zookeeper.connect""hadoop102:2181");
        props.put("group.id""g1");
        props.put("zookeeper.session.timeout.ms""500");
        props.put("zookeeper.sync.time.ms""250");
        props.put("auto.commit.interval.ms""1000");

        // 創建消費者連接器
        ConsumerConnector consumer = Consumer.createJavaConsumerConnector(new ConsumerConfig(props));

        HashMap<String, Integer> topicCount = new HashMap<String, Integer>();
        topicCount.put("first"1);

        Map<String, List<KafkaStream<byte[], byte[]>>> consumerMap = consumer.createMessageStreams(topicCount);

        KafkaStream<byte[], byte[]> stream = consumerMap.get("first").get(0);

        ConsumerIterator<byte[], byte[]> it = stream.iterator();

        while (it.hasNext()) {
            System.out.println(new String(it.next().message()));
        }
    }
}

2)官方提供案例(自動維護消費情況)(新API)

package com.atguigu.kafka.consumer;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;

import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;

/**
 * @author chenmingjun
 * @date 2019-03-05 15:10
 */

public class NewConsumer {
    public static void main(String[] args) {

        Properties props = new Properties();

        // 定義kakfa 服務的地址,不需要將所有broker指定上
        props.put("bootstrap.servers""hadoop102:9092");

        // 指定consumer group
        props.put("group.id""test");
        // 如果想重復消費topic數據,有三種方式:1、新建一個組。2、使用低級API指定offset。3、使用高級API在不換組的情況下重復消費topic數據。
        // 1、當我們新建一個組的時候,需要加一個屬性,如下:
        // props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");

        // 是否自動確認offset
        props.put("enable.auto.commit""true");

        // 自動確認offset的時間間隔
        props.put("auto.commit.interval.ms""1000");

        // key的序列化類
        props.put("key.deserializer""org.apache.kafka.common.serialization.StringDeserializer");

        // value的序列化類
        props.put("value.deserializer""org.apache.kafka.common.serialization.StringDeserializer");

        // 定義consumer
        KafkaConsumer<String, String> consumer = new KafkaConsumer<String, String>(props);

        // 指定消費者訂閱的topic,可同時訂閱多個
        consumer.subscribe(Arrays.asList("first""second""third"));
        // 3、使用高級API在不換組的情況下重復消費topic數據。
        // consumer.assign(Collections.singletonList(new TopicPartition("second", 0)));
        // consumer.seek(new TopicPartition("second", 0), 2);

        while (true) {
            // 讀取數據,讀取超時時間為100ms
            ConsumerRecords<String, String> records = consumer.poll(100);

            for (ConsumerRecord<String, String> record : records)
                System.out.println(record.topic() + "---" + record.partition() + "---" + record.offset() + "---" +  record.value());
        }
    }
}

4.3.2 低級API

實現使用低級API讀取指定topic,指定partition,指定offset的數據。
1)消費者使用低級API 的主要步驟:


2)方法描述:

3)完整版代碼:
package com.atguigu.kafka.consumer;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import kafka.api.FetchRequest;
import kafka.api.FetchRequestBuilder;
import kafka.api.PartitionOffsetRequestInfo;
import kafka.cluster.BrokerEndPoint;
import kafka.common.ErrorMapping;
import kafka.common.TopicAndPartition;
import kafka.javaapi.FetchResponse;
import kafka.javaapi.OffsetResponse;
import kafka.javaapi.PartitionMetadata;
import kafka.javaapi.TopicMetadata;
import kafka.javaapi.TopicMetadataRequest;
import kafka.javaapi.consumer.SimpleConsumer;
import kafka.message.MessageAndOffset;

/**
 * 根據指定的topic、partition、offset來獲取信息
 */

public class SimpleExample {

    private List<String> m_replicaBrokers = new ArrayList<String>();

    public SimpleExample() {
        m_replicaBrokers = new ArrayList<String>();
    }

    public static void main(String args[]) {
        SimpleExample example = new SimpleExample();

        // 最大讀取消息數量
        long maxReads = Long.parseLong("3");

        // 要訂閱的topic
        String topic = "second";

        // 要查找的分區
        int partition = Integer.parseInt("0");

        // broker節點的ip,即連接kafka集群
        List<String> seeds = new ArrayList<String>();
        seeds.add("hadoop102");
        seeds.add("hadoop103");
        seeds.add("hadoop103");

        // 端口
        int port = Integer.parseInt("9092");

        try {
            example.run(maxReads, topic, partition, seeds, port);
        } catch (Exception e) {
            System.out.println("Oops:" + e);
            e.printStackTrace();
        }
    }

    public void run(long a_maxReads, String a_topic, int a_partition, List<String> a_seedBrokers, int a_port) throws Exception {
        // 獲取指定Topic partition的元數據
        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, 10000064 * 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, 10000064 * 1024, clientName);
            }

            // 創建獲取數據的對象
            FetchRequest req = new FetchRequestBuilder().clientId(clientName).addFetch(a_topic, a_partition, readOffset, 100000).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) {
                Thread.sleep(1000);
            }
        }

        System.out.println("Unable to find new leader after Broker failure. Exiting");
        throw new Exception("Unable to find new leader after Broker failure. Exiting");
    }

    /**
     * 尋找leader
     * @param a_seedBrokers
     * @param a_port
     * @param a_topic
     * @param a_partition
     * @return
     */

    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 {
                // 創建獲取分區leader的消費者對象
                consumer = new SimpleConsumer(seed, a_port, 10000064 * 1024"leaderLookup");

                // 創建獲取多個主題元數據信息的請求
                List<String> topics = Collections.singletonList(a_topic);
                TopicMetadataRequest req = new TopicMetadataRequest(topics);
                kafka.javaapi.TopicMetadataResponse resp = consumer.send(req);

                // 獲取多個主題的元數據信息
                List<TopicMetadata> topicMetadata = resp.topicsMetadata();
                for (TopicMetadata topic : topicMetadata) {
                    // 獲取多個分區的元數據信息
                    for (PartitionMetadata part : topic.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 (BrokerEndPoint replica : returnMetaData.replicas()) {
                m_replicaBrokers.add(replica.host());
            }
        }
        return returnMetaData;
    }
}

第5章 Kafka Producer攔截器(interceptor)

5.1 攔截器原理

  Producer 攔截器(interceptor)是在 Kafka 0.10 版本被引入的,主要用於實現 clients 端的定制化控制邏輯。
  對於 producer 而言,interceptor 使得用戶在消息發送前以及 producer 回調邏輯前有機會對消息做一些定制化需求,比如修改消息等。同時,producer 允許用戶指定多個 interceptor 按順序作用於同一條消息從而形成一個攔截鏈(interceptor chain)。Intercetpor 的實現接口是 org.apache.kafka.clients.producer.ProducerInterceptor,其定義的方法包括:
(1)configure(configs):
  獲取配置信息和初始化數據時調用。
(2)onSend(ProducerRecord):
  該方法封裝進 KafkaProducer.send 方法中,即它運行在用戶主線程中。Producer 確保在消息被序列化以及計算分區前調用該方法。用戶可以在該方法中對消息做任何操作,但最好保證不要修改消息所屬的 topic 和分區,否則會影響目標分區的計算。
(3)onAcknowledgement(RecordMetadata, Exception):
  該方法會在消息被應答或消息發送失敗時調用,並且通常都是在 producer 回調邏輯觸發之前。onAcknowledgement 運行在 producer 的IO線程中,因此不要在該方法中放入很重的邏輯,否則會拖慢 producer 的消息發送效率。
(4)close:
  關閉 interceptor,主要用於執行一些資源清理工作。
  如前所述,interceptor 可能被運行在多個線程中,因此在具體實現時用戶需要自行確保線程安全。另外倘若指定了多個 interceptor,則 producer 將按照指定順序調用它們,並僅僅是捕獲每個 interceptor 可能拋出的異常記錄到錯誤日志中而非再向上傳遞。這在使用過程中要特別留意。

5.2 攔截器案例

1)需求:
實現一個簡單的雙 interceptor 組成的攔截鏈。第一個 interceptor 會在消息發送前將時間戳信息加到消息 value 的最前部;第二個 interceptor 會在消息發送后更新成功發送消息數或失敗發送消息數。

2)案例實操
(1)增加時間戳攔截器

package com.atguigu.kafka.interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

/**
 * @author chenmingjun
 * @date 2019-03-05 18:56
 */

public class TimeInterceptor implements ProducerInterceptor<StringString{

    public void configure(Map<String, ?> configs) {

    }

    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> record) {
        // 創建一個新的record,把時間戳寫入消息體的最前部
        return new ProducerRecord(record.topic(), record.partition(), record.timestamp(), record.key(),
                System.currentTimeMillis() + "," + record.value().toString());
    }

    public void onAcknowledgement(RecordMetadata metadata, Exception exception) {

    }

    public void close() {

    }
}

(2)統計發送消息成功和發送失敗消息數,並在producer關閉時打印這兩個計數器

package com.atguigu.kafka.interceptor;

import org.apache.kafka.clients.producer.ProducerInterceptor;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.util.Map;

/**
 * @author chenmingjun
 * @date 2019-03-05 18:59
 */

public class CounterInterceptor implements ProducerInterceptor<StringString{

    private int errorCounter = 0;
    private int successCounter = 0;

    public void configure(Map<String, ?> configs) {

    }

    public ProducerRecord<String, String> onSend(ProducerRecord<String, String> record) {
        return record;
    }

    public void onAcknowledgement(RecordMetadata metadata, Exception exception) {

        // 統計成功和失敗的次數
        if (exception == null) {
            successCounter++;
        } else {
            errorCounter++;
        }
    }

    public void close() {

        // 保存結果
        System.out.println("Successful sent: " + successCounter);
        System.out.println("Failed sent: " + errorCounter);
    }
}

(3)producer主程序

package com.atguigu.kafka.interceptor;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author chenmingjun
 * @date 2019-03-05 19:03
 */

public class InterceptorProducer {

    public static void main(String[] args) throws Exception {
        // 1 設置配置信息
        Properties props = new Properties();
        props.put("bootstrap.servers""hadoop102:9092");
        props.put("acks""all");
        props.put("retries"0);
        props.put("batch.size"16384);
        props.put("linger.ms"1);
        props.put("buffer.memory"33554432);
        props.put("key.serializer""org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer""org.apache.kafka.common.serialization.StringSerializer");

        // 2 構建攔截鏈
        List<String> interceptors = new ArrayList<String>();
        interceptors.add("com.atguigu.kafka.interceptor.TimeInterceptor");
        interceptors.add("com.atguigu.kafka.interceptor.CounterInterceptor");

        props.put(ProducerConfig.INTERCEPTOR_CLASSES_CONFIG, interceptors);

        // 創建生產者對象
        String topic = "second";
        Producer<String, String> producer = new KafkaProducer<String, String>(props);

        // 3 發送消息
        for (int i = 0; i < 10; i++) {

            ProducerRecord<String, String> record = new ProducerRecord<String, String>(topic, "message" + i);
            producer.send(record);
        }

        // 4 一定要關閉producer,這樣才會調用interceptor的close方法
        producer.close();
    }
}

3)測試
(1)在kafka上啟動消費者,然后運行客戶端java程序。

[atguigu@hadoop102 kafka]$ bin/kafka-console-consumer.sh \
--zookeeper hadoop102:2181 --from-beginning --topic second

1551784150698,message2
1551784150699,message5
1551784150701,message8
1551784150601,message0
1551784150699,message3
1551784150699,message6
1551784150701,message9
1551784150698,message1
1551784150699,message4
1551784150701,message7

(2)觀察java平台控制台輸出數據如下:

Successful sent: 10
Failed sent: 0

第6章 Kafka Streams

6.1 概述

6.1.1 Kafka Streams

  Kafka Streams。Apache Kafka開源項目的一個組成部分。是一個功能強大,易於使用的庫。用於在Kafka上構建高可分布式、拓展性,容錯的應用程序。

6.1.2 Kafka Streams 特點

1)功能強大
  高擴展性,彈性,容錯
2)輕量級
  無需專門的集群
  一個庫,而不是框架
3)完全集成
  100%的與Kafka 0.10.0版本兼容
  易於集成到現有的應用程序
4)實時性
  毫秒級延遲
  並非微批處理而spark是微處理框架
  窗口允許亂序數據
  允許遲到數據

6.1.3 為什么要有 Kafka Stream?

  當前已經有非常多的流式處理系統,最知名且應用最多的開源流式處理系統有Spark StreamingApache Storm。Apache Storm 發展多年,應用廣泛,提供記錄級別的處理能力,當前也支持 SQL on Stream。而 Spark Streaming 基於 Apache Spark,可以非常方便與圖計算,SQL處理等集成,功能強大,對於熟悉其它 Spark 應用開發的用戶而言使用門檻低。另外,目前主流的 Hadoop 發行版,如 Cloudera 和 Hortonworks,都集成 了Apache Storm 和 Apache Spark,使得部署更容易。
  既然 Apache Spark 與 Apache Storm 擁用如此多的優勢,那為何還需要 Kafka Stream 呢?主要有如下原因:

  第一,Spark 和 Storm 都是流式處理框架,而Kafka Stream提供的是一個基於Kafka的流式處理類庫。框架要求開發者按照特定的方式去開發邏輯部分,供框架調用。開發者很難了解框架的具體運行方式,從而使得調試成本高,並且使用受限。而 Kafka Stream 作為流式處理類庫,直接提供具體的類給開發者調用,整個應用的運行方式主要由開發者控制,方便使用和調試。

  第二,雖然 Cloudera 與 Hortonworks 方便了 Storm 和 Spark 的部署,但是這些框架的部署仍然相對復雜。而 Kafka Stream 作為類庫,可以非常方便的嵌入應用程序中,它對應用的打包和部署基本沒有任何要求

  第三,就流式處理系統而言,基本都支持 Kafka 作為數據源。例如 Storm 具有專門的 kafka-spout,而 Spark 也提供專門的 spark-streaming-kafka 模塊。事實上,Kafka 基本上是主流的流式處理系統的標准數據源。換言之,大部分流式系統中都已部署了 Kafka,此時使用 Kafka Stream 的成本非常低

  第四,使用 Storm 或 Spark Streaming 時,需要為框架本身的進程預留資源,如 Storm 的 supervisor 和 Spark on YARN 的 node manager。即使對於應用實例而言,框架本身也會占用部分資源,如 Spark Streaming 需要為 shuffle 和 storage 預留內存。但是 Kafka作為類庫不占用系統資源。

  第五,由於 Kafka 本身提供數據持久化,因此 Kafka Stream 提供滾動部署和滾動升級以及重新計算的能力。

  第六,由於 Kafka Consumer Rebalance 機制,Kafka Stream 可以在線動態調整並行度

6.2 Kafka Stream 數據清洗案例

0)需求:
  實時處理單詞帶有”>>>”前綴的內容。例如輸入”atguigu>>>ximenqing”,最終處理成“ximenqing”。
1)需求分析:


2)案例實操
(1)創建一個工程,並添加jar包或在pom文件中添加依賴
    <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-streams -->
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-streams</artifactId>
        <version>0.11.0.2</version>
    </dependency>

(2)創建主類

package com.atguigu.kafka.stream;

import org.apache.kafka.streams.KafkaStreams;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.processor.Processor;
import org.apache.kafka.streams.processor.ProcessorSupplier;
import org.apache.kafka.streams.processor.TopologyBuilder;

import java.util.Properties;

/**
 * @author chenmingjun
 * @date 2019-03-05 21:15
 */

public class KafkaStream {

    public static void main(String[] args) {

        // 定義輸入的topic
        String from = "first";

        // 定義輸出的topic
        String to = "second";

        // 設置參數
        Properties settings = new Properties();
        settings.put(StreamsConfig.APPLICATION_ID_CONFIG, "logFilter");
        settings.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "hadoop102:9092");

        StreamsConfig config = new StreamsConfig(settings);

        // 構建拓撲
        TopologyBuilder builder = new TopologyBuilder();

        builder.addSource("SOURCE", from).addProcessor("PROCESS"new ProcessorSupplier<byte[], byte[]>() {

                    @Override
                    public Processor<byte[], byte[]> get() {
                        // 具體分析處理
                        return new LogProcessor();
                    }
                }, "SOURCE")
                .addSink("SINK", to, "PROCESS");

        // 創建kafka stream
        KafkaStreams streams = new KafkaStreams(builder, config);
        streams.start();
    }
}

(3)具體業務處理

package com.atguigu.kafka.stream;

import org.apache.kafka.streams.processor.Processor;
import org.apache.kafka.streams.processor.ProcessorContext;

/**
 * @author chenmingjun
 * @date 2019-03-05 21:26
 */

public class LogProcessor implements Processor<byte[], byte[]> {

    private ProcessorContext context;

    @Override
    public void init(ProcessorContext context) {
        this.context = context;
    }

    @Override
    public void process(byte[] key, byte[] value) {

        String input = new String(value);

        // 如果包含“>>>”則只保留該標記后面的內容
        if (input.contains(">>>")) {
            input = input.split(">>>")[1].trim();

            // 輸出到下一個topic
            context.forward("logProcessor".getBytes(), input.getBytes());
        } else {
            context.forward("logProcessor".getBytes(), input.getBytes());
        }
    }

    @Override
    public void punctuate(long l) {

    }

    @Override
    public void close() {

    }
}

(4)運行程序
(5)在hadoop104上啟動生產者

[atguigu@hadoop104 kafka]$ bin/kafka-console-producer.sh \
--broker-list hadoop102:9092 --topic first

>hello>>>world
>h>>>atguigu
>hahaha

(6)在hadoop103上啟動消費者

[atguigu@hadoop103 kafka]$ bin/kafka-console-consumer.sh \
--zookeeper hadoop102:2181 --from-beginning --topic second

world
atguigu
hahaha

第7章 擴展知識

7.1 Kafka 與 Flume 比較

  在企業中必須要清楚流式數據采集框架 flume 和 kafka 的定位是什么:

  • flume:Cloudera 公司研發:

    • 適合多個生產者;(一個生產者對應一個 Agent 任務)
    • 適合下游數據消費者不多的情況;(多 channel 多 sink 會耗費很多內存)
    • 適合數據安全性要求不高的操作;(實際中更多使用 Memory Channel)
    • 適合與 Hadoop 生態圈對接的操作。(Cloudera 公司的特長)
  • kafka:Linkedin 公司研發:

    • 適合數據下游消費者眾多的情況;(開啟更多的消費者任務即可,與 Kafka 集群無關)
    • 適合數據安全性要求較高的操作,支持replication。(數據放在磁盤里)
  • 因此我們常用的一種模型是:

    • 線上數據 --> flume(適合采集tomcat日志) --> kafka(離線/實時) --> flume(根據情景增刪該流程) --> HDFS

7.2 Flume 與 kafka 集成

1)配置flume(flume-kafka.conf)

# define
a1.sources = r1
a1.sinks = k1
a1.channels = c1

# source
a1.sources.r1.type = exec
a1.sources.r1.command = tail -F -c +0 /opt/module/datas/flume.log
a1.sources.r1.shell = /bin/bash -c

# sink
a1.sinks.k1.type = org.apache.flume.sink.kafka.KafkaSink
a1.sinks.k1.kafka.bootstrap.servers = hadoop102:9092,hadoop103:9092,hadoop104:9092
a1.sinks.k1.kafka.topic = first
a1.sinks.k1.kafka.flumeBatchSize = 20
a1.sinks.k1.kafka.producer.acks = 1
a1.sinks.k1.kafka.producer.linger.ms = 1

# channel
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 100

# bind
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

2) 啟動kafka IDEA消費者
3) 進入flume根目錄下,啟動flume

[atguigu@hadoop102 flume]$ bin/flume-ng agent -n a1 -c conf/ -f job/flume-kafka.conf 

4) 向 /opt/module/datas/flume.log里追加數據,查看kafka消費者消費情況

[atguigu@hadoop102 datas]$$ echo hello > /opt/module/datas/flume.log

7.3 Kafka配置信息

7.3.1 Broker 配置信息

屬性 默認值 描述
broker.id   必填參數,broker的唯一標識
log.dirs /tmp/kafka-logs Kafka 數據存放的目錄。可以指定多個目錄,中間用逗號分隔,當新partition被創建的時會被存放到當前存放partition最少的目錄。
port 9092 BrokerServer接受客戶端連接的端口號
zookeeper.connect null Zookeeper的連接串,格式為:hostname1:port1,hostname2:port2,hostname3:port3。可以填一個或多個,為了提高可靠性,建議都填上。注意,此配置允許我們指定一個zookeeper路徑來存放此kafka集群的所有數據,為了與其他應用集群區分開,建議在此配置中指定本集群存放目錄,格式為:hostname1:port1,hostname2:port2,hostname3:port3/chroot/path 。需要注意的是,消費者的參數要和此參數一致。
message.max.bytes 1000000 服務器可以接收到的最大的消息大小。注意此參數要和consumer的maximum.message.size大小一致,否則會因為生產者生產的消息太大導致消費者無法消費。
num.io.threads 8 服務器用來執行讀寫請求的IO線程數,此參數的數量至少要等於服務器上磁盤的數量。
queued.max.requests 500 I/O線程可以處理請求的隊列大小,若實際請求數超過此大小,網絡線程將停止接收新的請求。
socket.send.buffer.bytes 100 * 1024 The SO_SNDBUFF buffer the server prefers for socket connections.
socket.receive.buffer.bytes 100 * 1024 The SO_RCVBUFF buffer the server prefers for socket connections.
socket.request.max.bytes 100 * 1024 * 1024 服務器允許請求的最大值, 用來防止內存溢出,其值應該小於 Java heap size.
num.partitions 1 默認partition數量,如果topic在創建時沒有指定partition數量,默認使用此值,建議改為5
log.segment.bytes 1024 * 1024 * 1024 Segment文件的大小,超過此值將會自動新建一個segment,此值可以被topic級別的參數覆蓋。
log.roll.{ms,hours} 24 * 7 hours 新建segment文件的時間,此值可以被topic級別的參數覆蓋。
log.retention.{ms,minutes,hours} 7 days Kafka segment log的保存周期,保存周期超過此時間日志就會被刪除。此參數可以被topic級別參數覆蓋。數據量大時,建議減小此值。
log.retention.bytes -1 每個partition的最大容量,若數據量超過此值,partition數據將會被刪除。注意這個參數控制的是每個partition而不是topic。此參數可以被log級別參數覆蓋。
log.retention.check.interval.ms 5 minutes 刪除策略的檢查周期
auto.create.topics.enable true 自動創建topic參數,建議此值設置為false,嚴格控制topic管理,防止生產者錯寫topic。
default.replication.factor 1 默認副本數量,建議改為2。
replica.lag.time.max.ms 10000 在此窗口時間內沒有收到follower的fetch請求,leader會將其從ISR(in-sync replicas)中移除。
replica.lag.max.messages 4000 如果replica節點落后leader節點此值大小的消息數量,leader節點就會將其從ISR中移除。
replica.socket.timeout.ms 30 * 1000 replica向leader發送請求的超時時間。
replica.socket.receive.buffer.bytes 64 * 1024 The socket receive buffer for network requests to the leader for replicating data.
replica.fetch.max.bytes 1024 * 1024 The number of byes of messages to attempt to fetch for each partition in the fetch requests the replicas send to the leader.
replica.fetch.wait.max.ms 500 The maximum amount of time to wait time for data to arrive on the leader in the fetch requests sent by the replicas to the leader.
num.replica.fetchers 1 Number of threads used to replicate messages from leaders. Increasing this value can increase the degree of I/O parallelism in the follower broker.
fetch.purgatory.purge.interval.requests 1000 The purge interval (in number of requests) of the fetch request purgatory.
zookeeper.session.timeout.ms 6000 ZooKeeper session 超時時間。如果在此時間內server沒有向zookeeper發送心跳,zookeeper就會認為此節點已掛掉。 此值太低導致節點容易被標記死亡;若太高,.會導致太遲發現節點死亡。
zookeeper.connection.timeout.ms 6000 客戶端連接zookeeper的超時時間。
zookeeper.sync.time.ms 2000 H ZK follower落后 ZK leader的時間。
controlled.shutdown.enable true 允許broker shutdown。如果啟用,broker在關閉自己之前會把它上面的所有leaders轉移到其它brokers上,建議啟用,增加集群穩定性。
auto.leader.rebalance.enable true If this is enabled the controller will automatically try to balance leadership for partitions among the brokers by periodically returning leadership to the “preferred” replica for each partition if it is available.
leader.imbalance.per.broker.percentage 10 The percentage of leader imbalance allowed per broker. The controller will rebalance leadership if this ratio goes above the configured value per broker.
leader.imbalance.check.interval.seconds 300 The frequency with which to check for leader imbalance.
offset.metadata.max.bytes 4096 The maximum amount of metadata to allow clients to save with their offsets.
connections.max.idle.ms 600000 Idle connections timeout: the server socket processor threads close the connections that idle more than this.
num.recovery.threads.per.data.dir 1 The number of threads per data directory to be used for log recovery at startup and flushing at shutdown.
unclean.leader.election.enable true Indicates whether to enable replicas not in the ISR set to be elected as leader as a last resort, even though doing so may result in data loss.
delete.topic.enable false 啟用deletetopic參數,建議設置為true。
offsets.topic.num.partitions 50 The number of partitions for the offset commit topic. Since changing this after deployment is currently unsupported, we recommend using a higher setting for production (e.g., 100-200).
offsets.topic.retention.minutes 1440 Offsets that are older than this age will be marked for deletion. The actual purge will occur when the log cleaner compacts the offsets topic.
offsets.retention.check.interval.ms 600000 The frequency at which the offset manager checks for stale offsets.
offsets.topic.replication.factor 3 The replication factor for the offset commit topic. A higher setting (e.g., three or four) is recommended in order to ensure higher availability. If the offsets topic is created when fewer brokers than the replication factor then the offsets topic will be created with fewer replicas.
offsets.topic.segment.bytes 104857600 Segment size for the offsets topic. Since it uses a compacted topic, this should be kept relatively low in order to facilitate faster log compaction and loads.
offsets.load.buffer.size 5242880 An offset load occurs when a broker becomes the offset manager for a set of consumer groups (i.e., when it becomes a leader for an offsets topic partition). This setting corresponds to the batch size (in bytes) to use when reading from the offsets segments when loading offsets into the offset manager’s cache.
offsets.commit.required.acks -1 The number of acknowledgements that are required before the offset commit can be accepted. This is similar to the producer’s acknowledgement setting. In general, the default should not be overridden.
offsets.commit.timeout.ms 5000 The offset commit will be delayed until this timeout or the required number of replicas have received the offset commit. This is similar to the producer request timeout.

7.3.2 Producer 配置信息

屬性 默認值 描述
metadata.broker.list   啟動時producer查詢brokers的列表,可以是集群中所有brokers的一個子集。注意,這個參數只是用來獲取topic的元信息用,producer會從元信息中挑選合適的broker並與之建立socket連接。格式是:host1:port1,host2:port2。
request.required.acks 0 參見3.2節介紹
request.timeout.ms 10000 Broker等待ack的超時時間,若等待時間超過此值,會返回客戶端錯誤信息。
producer.type sync 同步異步模式。async表示異步,sync表示同步。如果設置成異步模式,可以允許生產者以batch的形式push數據,這樣會極大的提高broker性能,推薦設置為異步。
serializer.class kafka.serializer.DefaultEncoder 序列號類,.默認序列化成 byte[] 。
key.serializer.class   Key的序列化類,默認同上。
partitioner.class kafka.producer.DefaultPartitioner Partition類,默認對key進行hash。
compression.codec none 指定producer消息的壓縮格式,可選參數為: “none”, “gzip” and “snappy”。關於壓縮參見4.1節
compressed.topics null 啟用壓縮的topic名稱。若上面參數選擇了一個壓縮格式,那么壓縮僅對本參數指定的topic有效,若本參數為空,則對所有topic有效。
message.send.max.retries 3 Producer發送失敗時重試次數。若網絡出現問題,可能會導致不斷重試。
retry.backoff.ms 100 Before each retry, the producer refreshes the metadata of relevant topics to see if a new leader has been elected. Since leader election takes a bit of time, this property specifies the amount of time that the producer waits before refreshing the metadata.
topic.metadata.refresh.interval.ms 600 * 1000 The producer generally refreshes the topic metadata from brokers when there is a failure (partition missing, leader not available…). It will also poll regularly (default: every 10min so 600000ms). If you set this to a negative value, metadata will only get refreshed on failure. If you set this to zero, the metadata will get refreshed after each message sent (not recommended). Important note: the refresh happen only AFTER the message is sent, so if the producer never sends a message the metadata is never refreshed
queue.buffering.max.ms 5000 啟用異步模式時,producer緩存消息的時間。比如我們設置成1000時,它會緩存1秒的數據再一次發送出去,這樣可以極大的增加broker吞吐量,但也會造成時效性的降低。
queue.buffering.max.messages 10000 采用異步模式時producer buffer 隊列里最大緩存的消息數量,如果超過這個數值,producer就會阻塞或者丟掉消息。
queue.enqueue.timeout.ms -1 當達到上面參數值時producer阻塞等待的時間。如果值設置為0,buffer隊列滿時producer不會阻塞,消息直接被丟掉。若值設置為-1,producer會被阻塞,不會丟消息。
batch.num.messages 200 采用異步模式時,一個batch緩存的消息數量。達到這個數量值時producer才會發送消息。
send.buffer.bytes 100 * 1024 Socket write buffer size
client.id “” The client id is a user-specified string sent in each request to help trace calls. It should logically identify the application making the request.

7.3.3 Consumer 配置信息

屬性 默認值 描述
group.id   Consumer的組ID,相同goup.id的consumer屬於同一個組。
zookeeper.connect   Consumer的zookeeper連接串,要和broker的配置一致。
consumer.id null 如果不設置會自動生成。
socket.timeout.ms 30 * 1000 網絡請求的socket超時時間。實際超時時間由max.fetch.wait + socket.timeout.ms 確定。
socket.receive.buffer.bytes 64 * 1024 The socket receive buffer for network requests.
fetch.message.max.bytes 1024 * 1024 查詢topic-partition時允許的最大消息大小。consumer會為每個partition緩存此大小的消息到內存,因此,這個參數可以控制consumer的內存使用量。這個值應該至少比server允許的最大消息大小大,以免producer發送的消息大於consumer允許的消息。
num.consumer.fetchers 1 The number fetcher threads used to fetch data.
auto.commit.enable true 如果此值設置為true,consumer會周期性的把當前消費的offset值保存到zookeeper。當consumer失敗重啟之后將會使用此值作為新開始消費的值。
auto.commit.interval.ms 60 * 1000 Consumer提交offset值到zookeeper的周期。
queued.max.message.chunks 2 用來被consumer消費的message chunks 數量, 每個chunk可以緩存fetch.message.max.bytes大小的數據量。
auto.commit.interval.ms 60 * 1000 Consumer提交offset值到zookeeper的周期。
queued.max.message.chunks 2 用來被consumer消費的message chunks 數量, 每個chunk可以緩存fetch.message.max.bytes大小的數據量。
fetch.min.bytes 1 The minimum amount of data the server should return for a fetch request. If insufficient data is available the request will wait for that much data to accumulate before answering the request.
fetch.wait.max.ms 100 The maximum amount of time the server will block before answering the fetch request if there isn’t sufficient data to immediately satisfy fetch.min.bytes.
rebalance.backoff.ms 2000 Backoff time between retries during rebalance.
refresh.leader.backoff.ms 200 Backoff time to wait before trying to determine the leader of a partition that has just lost its leader.
auto.offset.reset largest What to do when there is no initial offset in ZooKeeper or if an offset is out of range ;smallest : automatically reset the offset to the smallest offset; largest : automatically reset the offset to the largest offset;anything else: throw exception to the consumer
consumer.timeout.ms -1 若在指定時間內沒有消息消費,consumer將會拋出異常。
exclude.internal.topics true Whether messages from internal topics (such as offsets) should be exposed to the consumer.
zookeeper.session.timeout.ms 6000 ZooKeeper session timeout. If the consumer fails to heartbeat to ZooKeeper for this period of time it is considered dead and a rebalance will occur.
zookeeper.connection.timeout.ms 6000 The max time that the client waits while establishing a connection to zookeeper.
zookeeper.sync.time.ms 2000 How far a ZK follower can be behind a ZK leader

7.4 如何查看 Kafka 集群維護的 offset 信息

步驟:
(1)修改配置文件consumer.properties,增加一個屬性

[atguigu@hadoop102 config]$ pwd
/opt/module/kafka/config
[atguigu@hadoop102 config]$ vim consumer.properties

exclude.internal.topics=false

(2)分發配置好的文件

[atguigu@hadoop102 config]$ xsync consumer.properties

(3)執行新的消費者命令

bin/kafka-console-consumer.sh \
--zookeeper hadoop102:2181 --topic __consumer_offsets \
--formatter "kafka.coordinator.group.GroupMetadataManager\$OffsetsMessageFormatter" \
--consumer.config config/consumer.properties \
--from-beginning 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM