![](/img/trans.png)
[英]Why does Azure Eventhub python library throw KeyError: 'all-partitions' when it reaches the maximum size?
[英]Azure eventhub library for python
我正在使用 eventhub 來攝取很多事件。 我有多個消費者正在運行一個擴展組,從具有多個分區的 eventhub 讀取這些事件。 我正在使用 python 瀏覽 Azure SDK,但對使用什么感到困惑。 有 eventhubconsumerclient, eventprocessorHost ....
我想使用一個庫,我的多個消費者可以使用消費者組進行連接,分區是動態分配給每個消費者的,並且在存儲帳戶中進行檢查點,就像我使用 kafka 的方式一樣。
更新:
對於生產用途,我建議您應該使用穩定版的 event hub sdk。 您可以使用 eph,示例代碼在這里。
我可以使用預發布的 eventhub 5.0.0b6來使用消費者組以及設置檢查點。
但奇怪的是,在 blob 存儲中,我可以看到為 eventhub 創建的 2 個文件夾:檢查點和所有權文件夾。 在文件夾內,為分區創建了 blob,但 blob 是空的。 更奇怪的是,即使blob是空的,每次我從eventhub讀取時,它總是讀取最新的數據(意味着它永遠不會讀取已經在同一個消費者組中讀取的數據)。
您需要安裝azure-eventhub 5.0.0b6並使用pip install --pre azure-eventhub-checkpointstoreblob
安裝azure-eventhub-checkpointstoreblob 。 對於 blob 存儲,您應該安裝azure-storage-blob的最新版本 12.1.0 。
我遵循這個樣本。 在此示例中,它使用事件中心級連接字符串(不是事件中心命名空間級連接字符串)。 您需要通過導航創建事件中心級別的連接字符串到 azure 門戶 -> 您的 eventhub 命名空間 -> 您的事件中心實例 -> 共享訪問策略 -> 單擊“添加” -> 然后指定策略名稱,並選擇權限。 如果只是想接收數據,只能選擇Listen權限。 截圖如下:
創建策略后,您可以按照下面的屏幕截圖復制連接字符串:
然后你可以按照下面的代碼:
import os
from azure.eventhub import EventHubConsumerClient
from azure.eventhub.extensions.checkpointstoreblob import BlobCheckpointStore
CONNECTION_STR = 'Endpoint=sb://ivanehubns.servicebus.windows.net/;SharedAccessKeyName=saspolicy;SharedAccessKey=xxx;EntityPath=myeventhub'
STORAGE_CONNECTION_STR = 'DefaultEndpointsProtocol=https;AccountName=xx;AccountKey=xxx;EndpointSuffix=core.windows.net'
def on_event(partition_context, event):
# do something with event
print(event)
print('on event')
partition_context.update_checkpoint(event)
if __name__ == '__main__':
#the "a22" is the blob container name
checkpoint_store = BlobCheckpointStore.from_connection_string(STORAGE_CONNECTION_STR, "a22")
#the "$default" is the consumer group
client = EventHubConsumerClient.from_connection_string(
CONNECTION_STR, "$default", checkpoint_store=checkpoint_store)
try:
print('ok')
client.receive(on_event)
except KeyboardInterrupt:
client.close()
測試結果:
azure-eventhub v5 已於 2020 年 1 月 GAed,最新版本為 v5.2.0
它在 pypi 上可用: https ://pypi.org/project/azure-eventhub/
對於使用檢查點接收,請按照示例代碼:
import os
import logging
from azure.eventhub import EventHubConsumerClient
from azure.eventhub.extensions.checkpointstoreblob import BlobCheckpointStore
CONNECTION_STR = os.environ["EVENT_HUB_CONN_STR"]
EVENTHUB_NAME = os.environ['EVENT_HUB_NAME']
STORAGE_CONNECTION_STR = os.environ["AZURE_STORAGE_CONN_STR"]
BLOB_CONTAINER_NAME = "your-blob-container-name" # Please make sure the blob container resource exists.
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
def on_event_batch(partition_context, event_batch):
log.info("Partition {}, Received count: {}".format(partition_context.partition_id, len(event_batch)))
# put your code here
partition_context.update_checkpoint()
def receive_batch():
checkpoint_store = BlobCheckpointStore.from_connection_string(STORAGE_CONNECTION_STR, BLOB_CONTAINER_NAME)
client = EventHubConsumerClient.from_connection_string(
CONNECTION_STR,
consumer_group="$Default",
eventhub_name=EVENTHUB_NAME,
checkpoint_store=checkpoint_store,
)
with client:
client.receive_batch(
on_event_batch=on_event_batch,
max_batch_size=100,
starting_position="-1", # "-1" is from the beginning of the partition.
)
if __name__ == '__main__':
receive_batch()
還有一點值得注意的是,在 V5 中,我們使用 blob 的元數據來存儲檢查點和所有權信息,而不是將它們存儲為 v1 中的 blob 內容。 因此,在使用 v5 sdk 時,預計 blob 的內容為空。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.