EPH接收Event Hub Message
简介:
使用Python SDK,基于EPH方式接收Azure Event Hub中存储的message,EventProcessorHost()中使用Azure Storage存储offerset等信息。目前版本的SDK对中国区的支持还不是太好,使用起来需要结合中国区的特点指定具体的endpoint。
示例程序:
import asyncio
from azure.eventprocessorhost import AbstractEventProcessor, EPHOptions
from azure.eventprocessorhost import AzureStorageCheckpointLeaseManager
from azure.eventprocessorhost import EventHubConfig
from azure.eventprocessorhost import EventProcessorHost
from azure.storage.blob import BlockBlobService
class EventProcessor(AbstractEventProcessor):
"""
Example Implmentation of AbstractEventProcessor
"""
def __init__(self, params=None):
"""
Init Event processor
"""
self._msg_counter = 0
async def open_async(self, context):
"""
Called by processor host to initialize the event processor.
"""
print(context.partition_id)
async def close_async(self, context, reason):
"""
Called by processor host to indicate that the event processor is being stopped.
(Params) Context:Information about the partition
"""
print("Reason: " + reason)
print("context.partition_id: " + context.partition_id)
print("context.offset: " + context.offset)
print("context.sequence_number: {}".format(context.sequence_number))
async def process_events_async(self, context, messages):
"""
Called by the processor host when a batch of events has arrived.
This is where the real work of the event processor is done.
(Params) Context: Information about the partition, Messages: The events to be processed.
"""
print(context.partition_id)
print(messages)
print("-----")
await context.checkpoint_async()
async def process_error_async(self, context, error):
"""
Called when the underlying client experiences an error while receiving.
EventProcessorHost will take care of recovering from the error and
continuing to pump messages,so no action is required from
(Params) Context: Information about the partition, Error: The error that occured.
"""
print(repr(error))
# 注意如果消息较多,请增加sleep的时间,防止程序提前终止
async def wait_and_close(host):
"""
Run EventProcessorHost for 6 minutes then shutdown.
"""
await asyncio.sleep(360)
await host.close_async()
try:
loop = asyncio.get_event_loop()
# Storage Account Credentials
STORAGE_ACCOUNT_NAME = "yuvmtestdiag489"
STORAGE_KEY = "OgNFVB93P2oWCKcJaNOt4kuW0lf911D11v6NiHcDC6R0SdiqUJYlP8UdW7U4p0UzSnreII+7BnS9Cy1VsMSqGg=="
LEASE_CONTAINER_NAME = "leases5"
# Eventhub config and storage manager
EH_CONFIG = EventHubConfig('cznamespace', 'yutaoeventhubtest', 'RootManageSharedAccessKey',
'wnwcU+VjKLGgNUwiecwAyHb9gAVPDIC+RvXu2JcDINc=', consumer_group="$default")
# 注意务必对key进行urlencode编码
ADDRESS = ("amqps://"
"RootManageSharedAccessKey"
":"
"wnwcU%2bVjKLGgNUwiecwAyHb9gAVPDIC%211vXu2JcDINc%3d"
"@"
"cznamespace.servicebus.chinacloudapi.cn"
"/"
"yutaoeventhubtest")
EH_CONFIG.client_address = ADDRESS
STORAGE_MANAGER = AzureStorageCheckpointLeaseManager(STORAGE_ACCOUNT_NAME,
STORAGE_KEY,
LEASE_CONTAINER_NAME,
storage_blob_prefix="blob.core.chinacloudapi.cn")
eph_options = EPHOptions()
#Event loop and host
LOOP = asyncio.get_event_loop()
HOST = EventProcessorHost(EventProcessor, EH_CONFIG, STORAGE_MANAGER, eph_options=eph_options,
ep_params=["param1", "param2"], loop=LOOP)
# 重新设置一遍防止SDK擦除了之前的配置
STORAGE_MANAGER.storage_client = BlockBlobService(account_name=STORAGE_ACCOUNT_NAME,
account_key=STORAGE_KEY,
endpoint_suffix="core.chinacloudapi.cn")
tasks = asyncio.gather(
HOST.open_async(),
wait_and_close(HOST))
loop.run_until_complete(tasks)
except KeyboardInterrupt:
# Canceling pending tasks and stopping the loop
for task in asyncio.Task.all_tasks():
task.cancel()
loop.run_forever()
tasks.exception()
finally:
loop.stop()
注意: 连接信息已经失效,仅供格式参考。
测试结果:
1
[<azure.eventhub.EventData object at 0x00000223AE556EF0>, <azure.eventhub.EventData object at 0x00000223AE556F98>, <azure.eventhub.EventData object at 0x00000223AE4DBA20>, <azure.eventhub.EventData object at 0x00000223AE4DB400>, <azure.eventhub.EventData object at 0x00000223AE4DBC88>, <azure.eventhub.EventData object at 0x00000223AE4DBAC8>, <azure.eventhub.EventData object at 0x00000223AE4FD7F0>, <azure.eventhub.EventData object at 0x00000223AE4FD6D8>, <azure.eventhub.EventData object at 0x00000223AE4FDBE0>, <azure.eventhub.EventData object at 0x00000223AE4FD048>]
-----
1
[<azure.eventhub.EventData object at 0x00000223AE4FD278>, <azure.eventhub.EventData object at 0x00000223AE4FDB70>, <azure.eventhub.EventData object at 0x00000223AE4FD668>, <azure.eventhub.EventData object at 0x00000223AE4FDAC8>, <azure.eventhub.EventData object at 0x00000223AE4FDB38>, <azure.eventhub.EventData object at 0x00000223AE4FDF60>, <azure.eventhub.EventData object at 0x00000223AE57AB38>, <azure.eventhub.EventData object at 0x00000223AE57A470>, <azure.eventhub.EventData object at 0x00000223AE57A748>]
-----
Reason: Shutdown
context.partition_id: 0
context.offset: 978600
context.sequence_number: 17559
Reason: Shutdown
context.partition_id: 1
context.offset: 978544
context.sequence_number: 17558
Process finished with exit code 0
参考示例:
azure-event-hubs-python/examples/eph.py
eph.py stop when the message not all received
EPH接收Event Hub Message的更多相关文章
- Azure Event Hub 技术研究系列3-Event Hub接收事件
上篇博文中,我们通过编程的方式介绍了如何将事件消息发送到Azure Event Hub: Azure Event Hub 技术研究系列2-发送事件到Event Hub 本篇文章中,我们继续:从Even ...
- Event Hub小白入门指南
Event Hub事件中心 本文的目的在于用最白的大白话,让你从“完全不懂”开始,理解什么是分布式大数据流平台Event Hub,并且理解它的关键概念,并且初步理解其收发数据API. 定义,Event ...
- 【Azure 事件中心】Azure Event Hub 新功能尝试 -- 异地灾难恢复 (Geo-Disaster Recovery)
问题描述 关于Event Hub(事件中心)的灾备方案,大多数就是新建另外一个备用的Event Hub,当主Event Hub出现不可用的情况时,就需要切换到备Event Hub上. 而在切换的过程中 ...
- 【Azure 事件中心】 org.slf4j.Logger 收集 Event Hub SDK(Java) 输出日志并以文件形式保存
问题描述 在使用Azure Event Hub的SDK时候,常规情况下,发现示例代码中并没有SDK内部的日志输出.因为在Java项目中,没有添加 SLF4J 依赖,已致于在启动时候有如下提示: SLF ...
- Azure Event Hub 技术研究系列2-发送事件到Event Hub
上篇博文中,我们介绍了Azure Event Hub的一些基本概念和架构: Azure Event Hub 技术研究系列1-Event Hub入门篇 本篇文章中,我们继续深入研究,了解Azure Ev ...
- Azure IoT Hub和Event Hub相关的技术系列-索引篇
Azure IoT Hub和Event Hub相关的技术系列,最近已经整理了不少了,统一做一个索引链接,置顶. Azure IoT 技术研究系列1-入门篇 Azure IoT 技术研究系列2-设备注册 ...
- Azure Event hub usage
1. create event hub on azure watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/40 ...
- 【事件中心 Azure Event Hub】Event Hub Java SDK的消费端出现不消费某一个分区中数据的情况,出现IdleTimerExpired错误消息记录
问题情形 使用Java SDK编写的Event Hub消费端应用,随机性遇见了某个分区没有消费消息的情况,在检查日志时候,有发现IdelTimeExpired的错误记录.在重启应用后,连接EventH ...
- 【事件中心 Azure Event Hub】关于EventHub中出现Error时候的一些问题(偶发错误,EventHub后台升级,用户端错误,Retry机制的重要性)
请问对偶发的定义是多少频率? 针对偶发的定义,主要是看发生的时间非常短,次数极少(如 10次以内),并且发生的时候EventHub其他分区或其他连接都是正常接收和发送数据.所以对于频率是没有明确的定义 ...
随机推荐
- xunsearch: 开启后台服务,索引……随笔记录
重启后台服务: cd $prefix ; bin/xs-ctl.sh restart 索引: # 导入 MySQL 数据库的 dbname.tbl_post 表到 demo 项目中,并且平滑重建 ut ...
- json数据格式了解
json数据格式介绍: https://en.wikipedia.org/wiki/JSON JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式 json语法 ...
- URAL 2072 Kirill the Gardener 3 (单调DP)
[题目链接] http://acm.timus.ru/problem.aspx?space=1&num=2072 [题目大意] 一个园丁要给一排花浇水,每个花都有一个标号,必须要先浇标号小的, ...
- 【AC自动机】【状压dp】hdu2825 Wireless Password
f(i,j,S)表示当前字符串总长度为i,dp到AC自动机第j个结点,单词集合为S时的方案数. 要注意有点卡常数,注意代码里的注释. #include<cstdio> #include&l ...
- 【费用流】bzoj2661 [BeiJing wc2012]连连看
将每个数拆点,互相连边,然后满足条件的数对之间互相连边,跑最大费用流,答案是流量和费用分别除以2. 一定要i->j.j->i都连上,否则可能会出现一个数在一边被选择了,在另一边的另一个匹配 ...
- 【欧拉函数】BZOJ2190-[SDOI2012]longge的数学问题
[题目大意] 求出∑gcd(i, N)(1<=i <=N). [思路] 对于x=ak,y=bk,若gcd(a,b)=1则必有gcd(x,y)=1.枚举N的所有因数,∑gcd(i, N)=∑ ...
- hdu Ignatius and the Princess II
Ignatius and the Princess II Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Ja ...
- 利用hsdis和JITWatch查看分析HotSpot JIT compiler生成的汇编代码
http://blog.csdn.net/hengyunabc/article/details/26898657
- CSS3: box-shadow 阴影
box-shadow是给元素块添加周边阴影效果 语法: 对象选择器 {box-shadow:X轴偏移量 Y轴偏移量阴影 模糊半径 阴影扩展半径 阴影颜色 [投影方式] } box-shadow: h- ...
- SqlServer 执行计划及Sql查询优化初探
网上的SQL优化的文章实在是很多,说实在的,我也曾经到处找这样的文章,什么不要使用IN了,什么OR了,什么AND了,很多很多,还有很多人拿出仅几S甚至几MS的时间差的例子来证明着什么(有点可笑),让许 ...