首页 > 解决方案 > 如何使用 Python 通过 Azure EventHub 将 JSON 数据导入 Azure 时序见解?

问题描述

我正在尝试使用 Python 获取一些示例 JSON 数据以显示在 Azure 时序见解 (TSI) 中,以便我可以在他们的探索性浏览器中可视化数据。

我已经完成了有关 Azure EventHubs 和时序见解设置的必要先决条件。这些包括:

  1. 在 Azure 门户中创建资源组
  2. 在资源组中创建事件中心命名空间
  3. 在该事件中心命名空间中创建事件中心实体
  4. 在事件中心实体中创建消费者组
  5. 我在资源组中设置了 Azure TSI 环境。
  6. 最后,我使用不同创建的源作为详细信息(资源组、事件中心命名空间、事件中心名称等)将事件源添加到 Azure TSI 环境。

除此之外,我按照以下文档测试了使用 python 成功地将事件消息发送到我的事件中心(但没有发送到 TSI 环境):https ://docs.microsoft.com/en-us/azure/event-hubs/get-started -python-send-v2并使用此代码(尽管填写了 con_str 并填写了 eventthub_name:

import asyncio
from azure.eventhub.aio import EventHubProducerClient
from azure.eventhub import EventData

async def run():
    # Create a producer client to send messages to the event hub.
    # Specify a connection string to your event hubs namespace and
        # the event hub name.
    producer = EventHubProducerClient.from_connection_string(conn_str="EVENT HUBS NAMESPACE - CONNECTION STRING", eventhub_name="EVENT HUB NAME")
    async with producer:
        # Create a batch.
        event_data_batch = await producer.create_batch()

        # Add events to the batch.
        event_data_batch.add(EventData('First event '))
        event_data_batch.add(EventData('Second event'))
        event_data_batch.add(EventData('Third event'))

        # Send the batch of events to the event hub.
        await producer.send_batch(event_data_batch)

loop = asyncio.get_event_loop()
loop.run_until_complete(run())

我还按照以下文档成功测试了将 Microsoft 的 Windmill Simulator 数据发送到我的 TSI 环境:https ://docs.microsoft.com/en-us/azure/time-series-insights/time-series-insights-send-events

我现在不知道如何使用 Python 将示例 JSON 数据实际获取到 Azure TSI 环境中。

任何帮助,将不胜感激。谢谢!

标签: pythonjsonazure

解决方案


正如本文所述您必须使用JSON 格式的字符串发送 EventData 的正文。

与您分享上述代码的修改片段:

import asyncio
import nest_asyncio
nest_asyncio.apply()
from azure.eventhub.aio import EventHubProducerClient
from azure.eventhub import EventData
import json

async def run():
    # Create a producer client to send messages to the event hub.
    # Specify a connection string to your event hubs namespace and
        # the event hub name.
    producer = EventHubProducerClient.from_connection_string("<>", eventhub_name="<>")
    async with producer:
        # Create a batch.
        event_data_batch = await producer.create_batch()

        # Add events to the batch.

        #Method 1 - You provide a JSON string 
        body1 = '{"id":"device2","timestamp":"2016-01-17T01:17:00Z"}' 
        event_data_batch.add(EventData(body1))

        #Method 2 - You get the JSON Object and convert to string
        json_obj = {"id":"device3","timestamp":"2016-01-18T01:17:00Z"}
        body2= json.dumps(json_obj)
        event_data_batch.add(EventData(body2))


        #This just sending the string which will not be captured by TSI
        event_data_batch.add(EventData('Third event'))

        # Send the batch of events to the event hub.
        await producer.send_batch(event_data_batch)


loop = asyncio.get_event_loop()
loop.run_until_complete(run())

输出 :

在此处输入图像描述


推荐阅读