引言:
目前使用golang接入kafka,於是便用到了github.com/Shopify/sarama,一個kafka的golang client library,但官方doc中尚無中文版本,於是本人在這裏捉襟見肘做一個簡單地翻譯,作爲自己的一個點滴記錄和查閱,如果能夠幫助到你一點點,我會非常高興的。另外,由於本人能力有限,若有不當之處,歡迎指正。
正文:
package sarama
Package sarama is a pure Go client library for dealing with Apache Kafka (versions 0.8 and later). It includes a high-level API for easily producing and consuming messages, and a low-level API for controlling bytes on the wire when the high-level API is insufficient. Usage examples for the high-level APIs are provided inline with their full documentation.
sarama 是一個爲了處理apache kafka的純Go (編寫的),客戶端庫。它包含用於輕鬆生成和消費消息的高級API,以及當高級API不足時用於控制傳輸bytes的低級API。本文提供完整使用高級API的示例。
To produce messages, use either the AsyncProducer or the SyncProducer. The AsyncProducer accepts messages on a channel and produces them asynchronously in the background as efficiently as possible; it is preferred in most cases. The SyncProducer provides a method which will block until Kafka acknowledges the message as produced. This can be useful but comes with two caveats: it will generally be less efficient, and the actual durability guarantees depend on the configured value of Producer.RequiredAcks
. There are configurations where a message acknowledged by the SyncProducer can still sometimes be lost.
要生成消息,請使用AsyncProducer或SyncProducer。AsyncProducer接收通道上的消息,並在後臺以異步方式儘可能高效地生成它們; 在大多數情況下首選此種方式。SyncProducer提供了一種阻塞的等待服務器,直到Kafka確認消息生成爲止的方法。這在某些情況下是有用的,但有兩個注意事項:它通常效率較低,實際的持久性保證取決於“Producer.RequiredAcks”的配置值。這是一個用於一個消息確認ack多久能夠丟失的配置。
To consume messages, use the Consumer. Note that Sarama’s Consumer implementation does not currently support automatic consumer-group rebalancing and offset tracking. For Zookeeper-based tracking (Kafka 0.8.2 and earlier), the https://github.com/wvanbergen/kafka library builds on Sarama to add this support. For Kafka-based tracking (Kafka 0.9 and later), the https://github.com/bsm/sarama-cluster library builds on Sarama to add this support.
要消費消息,請使用消費者。注意Sarama的Consumer實現目前不支持自動消費者組rebalancing和offset偏移量追蹤。對於基於Zookeeper的追蹤(Kafka 0.8.2及更早版本),https://github.com/wvanbergen/kafka庫基於Sarama添加此支持。對於基於kafka的追蹤(Kafka 0.9及更高版本),https://github.com/bsm/sarama-cluster庫基於Sarama添加此支持。
For lower-level needs, the Broker and Request/Response objects permit precise control over each connection and message sent on the wire; the Client provides higher-level metadata management that is shared between the producers and the consumer. The Request/Response objects and properties are mostly undocumented, as they line up exactly with the protocol fields documented by Kafka at https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
對於較低級別的需求,Broker和Request/Response對象允許精確控制每個連接和消息的發送; 此客戶端提供在生產者和消費者之間共享的更高級別的元數據管理。請求/響應對象和屬性大部分未記錄在文檔中,因爲它們與Kafka 記錄的協議字段完全一致,網址爲 https://cwiki.apache.org/confluence/display/KAFKA/A+Guide+To+The+Kafka+Protocol
Broker related metrics:
Broker 關聯指標 :
Name | Type | Description |
---|---|---|
incoming-byte-rate | meter | Bytes/second read off all brokers |
incoming-byte-rate-for-broker- | meter | Bytes/second read off a given broker |
outgoing-byte-rate | meter | Bytes/second written off all brokers |
outgoing-byte-rate-for-broker- | meter | Bytes/second written off a given broker |
request-rate | meter | Requests/second sent to all brokers |
request-rate-for-broker- | meter | Requests/second sent to a given broker |
request-size | histogram | Distribution of the request size in bytes for all brokers |
request-size-for-broker- | histogram | Distribution of the request size in bytes for a given broker |
request-latency-in-ms | histogram | Distribution of the request latency in ms for all brokers |
request-latency-in-ms-for-broker- | histogram | Distribution of the request latency in ms for a given broker |
response-rate | meter | Responses/second received from all brokers |
response-rate-for-broker- | meter | Responses/second received from a given broker |
response-size | histogram | Distribution of the response size in bytes for all brokers |
response-size-for-broker- | histogram | Distribution of the response size in bytes for a given broker |
Note that we do not gather specific metrics for seed brokers but they are part of the “all brokers” metrics.
請注意,我們不會收集seed brokers的詳細指標,但它們是“所有brokers”指標的一部分。
Producer related metrics:
生產者關聯指標:
Name | Type | Description |
---|---|---|
batch-size | histogram | Distribution of the number of bytes sent per partition per request for all topics |
batch-size-for-topic- | histogram | Distribution of the number of bytes sent per partition per request for a given topic |
record-send-rate | meter | Records/second sent to all topics |
record-send-rate-for-topic- | meter | Records/second sent to a given topic |
records-per-request | histogram | Distribution of the number of records sent per request for all topics |
records-per-request-for-topic- | histogram | Distribution of the number of records sent per request for a given topic |
compression-ratio | histogram | Distribution of the compression ratio times 100 of record batches for all topics |
compression-ratio-for-topic- | histogram | Distribution of the compression ratio times 100 of record batches for a given topic |
附錄
由於精力有限,這裏不包括API中的常量,變量,函數等詳細說明,詳情請參閱:
sarama github GoDoc