1

I wish to send a JSON file to Azure event hub using the sample code given in Microsoft website. The code looks to be incorrect and not seeing the below uploaded file in event hub.

Could someone help on how to send the actual JSON file?

import asyncio

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


async def run():
    producer = EventHubProducerClient.from_connection_string(
        conn_str="foo",
        eventhub_name="boo")
    async with producer:
        event_data_batch = await producer.create_batch()
        event_data_batch.add(EventData(JSONFilepath))
        await producer.send_batch(event_data_batch)


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

Note: I am not facing error while running the program.

Learner
  • 135
  • 1
  • 3
  • 12
  • 1
    Do you have a link to that sample code? – Peter Bons Sep 09 '20 at 16:57
  • [Answer](https://stackoverflow.com/questions/63105581/how-can-i-get-json-data-into-azure-time-series-insights-through-an-azure-eventhu/63137189#63137189) - I had written this answer to send json strings & json objects to event hub. Pls let me know if this is what you are looking for or looking for something else on the same lines. – Satya V Sep 09 '20 at 17:13
  • Also, in case the above did not help, pls share the link to the sample code. – Satya V Sep 09 '20 at 17:16
  • Yes, that would be helpful. – Learner Sep 09 '20 at 17:18

1 Answers1

3

A code snippet to send JSON Objects & JSON String to event Hub

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())
Satya V
  • 3,811
  • 1
  • 6
  • 9
  • Thanks Satya. Can you guide me as to how to send .zip file too if you don't mind? – Learner Sep 09 '20 at 17:25
  • You could send zip files to Event Hubs by sending the file contents as byte array but it is not recommended because the maximum size of event data is 256KB. `event_data = EventData(b"Bytes data")` To convert a file to bytes array, you could refer to [this](https://stackoverflow.com/questions/54872560/zip-file-to-bytes-python-3) – Satya V Sep 09 '20 at 17:40
  • Just to add the maximum size mention is for Standard. for other limits pls refer this : https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-quotas – Satya V Sep 09 '20 at 17:49
  • In your example program, For zip, Instead of JSON file, zipped file has to be converted into bytes and rest of the program stays as is, right? – Learner Sep 09 '20 at 18:30