Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Update sample to use latest version of Python Event Hubs package#24

Merged
robinsh merged 5 commits intoAzure-Samples:masterfrom
YijunXieMS:master
Jun 16, 2020
Merged

Update sample to use latest version of Python Event Hubs package#24
robinsh merged 5 commits intoAzure-Samples:masterfrom
YijunXieMS:master

Conversation

@YijunXieMS
Copy link
Copy Markdown
Contributor

Purpose

  • Provide quick start guide example code for receiving events from the eventhub-compatible endpoint of IoT Hub d2c messages

Does this introduce a breaking change?

[ ] Yes
[x] No

Pull Request Type

What kind of change does this Pull Request introduce?

[ ] Bugfix
[ ] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[x] Other: Uses latest version of the python `azure-eventhub` package that has newer APIs

How to Test

  • Get the code
git clone https://github.com/Azure-Samples/azure-iot-samples-python.git
cd azure-iot-samples-python
git checkout [branch-name]
cd iot-hub/Quickstarts/read-d2c-messages
pip install -r requirements.txt
  • Test the code
Add your event hub compatible endpoint information by running Azure CLI, and run the code
Send data to your IotHub via any means you are familiar with in another program

Then run the python files.

What to Check

Verify that the following are valid

  • Command line should print content of received iot hub d2c messages.

Other Information

@ramya-rao-a
Copy link
Copy Markdown
Contributor

cc @elhorton, @jebrando, @JimacoMS3, @robinsh, @philmea

This is similar to what was done to the Node sample in Azure-Samples/azure-iot-samples-node#39


# If you have access to the Event Hub-compatible connection string from the Azure portal, then
# you can skip the Azure CLI commands above, and assign the connection string directly here.
CONNECTION_STR = f'Endpoint=sb://{EVENTHUB_COMPATIBLE_ENDPOINT}/;SharedAccessKeyName=service;SharedAccessKey={IOTHUB_SAS_KEY};EntityPath={EVENTHUB_COMPATIBLE_PATH}';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the leading 'sb://' here. It's inlcuded in the EVENTHUB_COMPATIBLE_ENDPOINT. Also, I don't think you need/want the terminating semi-colon. Same things for the sync sample

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the trailing ';'. I used VS code to create this sample. I realized it didn't save automatically like Pycharm so my last small fixes were not saved and pushed.
Corrected now.

await client.receive_batch(
on_event_batch=on_event_batch,
on_error=on_error,
starting_position="-1", # "-1" is from the beginning of the partition.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get rid of the starting_position parameter? This spits out a bunch of telemetry that could have happened before you actually ran the SimulatedDevice sample. I think getting rid of the parameter, just starts from the current time -- I think. Same thing for the sync sample.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed. So it will only receive new messages.


# If you have access to the Event Hub-compatible connection string from the Azure portal, then
# you can skip the Azure CLI commands above, and assign the connection string directly here.
CONNECTION_STR = f'Endpoint=sb://{EVENTHUB_COMPATIBLE_ENDPOINT}/;SharedAccessKeyName=service;SharedAccessKey={IOTHUB_SAS_KEY};EntityPath={EVENTHUB_COMPATIBLE_PATH}';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment for async

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

client.receive_batch(
on_event_batch=on_event_batch,
on_error=on_error,
starting_position="-1", # "-1" is from the beginning of the partition.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment for async wrt starting_position.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

# Event Hub-compatible endpoint
# az iot hub show --query properties.eventHubEndpoints.events.endpoint --name {your IoT Hub name}
EVENTHUB_COMPATIBLE_ENDPOINT = "{your Event Hubs compatible endpoint}"
EVENTHUB_COMPATIBLE_ENDPOINT = "iothub-ns-yijun-ioth-3130889-4398262da5.servicebus.windows.net"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to yank your hard-coded constants :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is embarrassing. I used them to test the script.
Not saved file before git commit in VS code again.
I regenerated the key on portal as well since it's compromised.
Thanks for pointing this out.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realized the resource was already deleted on github.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done the same thing roughly a million times :)

@JimacoMS3
Copy link
Copy Markdown
Contributor

doc updates in this PR: https://github.com/MicrosoftDocs/azure-docs-pr/pull/115889

(Note: screenshot needs updating.)

@JimacoMS3
Copy link
Copy Markdown
Contributor

@YijunXieMS Hi Yijun -- one last request here. Is it possible to have a graceful way to exit these samples (without adding a ton of code) ? Right now, ctrl-c generates exception output. (if that's something that's common across the EH samples, then don't worry about it). The other EH platform samples exit more gracefully.

Thanks,

Jimaco

@YijunXieMS
Copy link
Copy Markdown
Contributor Author

@YijunXieMS Hi Yijun -- one last request here. Is it possible to have a graceful way to exit these samples (without adding a ton of code) ? Right now, ctrl-c generates exception output. (if that's something that's common across the EH samples, then don't worry about it). The other EH platform samples exit more gracefully.

Thanks,

Jimaco

Hi Jimaco, yes I can add some code for ctrl-c within this week.

@JimacoMS3
Copy link
Copy Markdown
Contributor

Awesome Yi -- let me know when you have it ready.

@JimacoMS3
Copy link
Copy Markdown
Contributor

@YijunXieMS Thanks Yijun! Ctrl-c works great. Just one last nit (if you don't have time for this - just let me know). Can please add a line between groups of readings. I think it just makes it more easy to parse. i.e.

Capture

This really is a nit. Like I said, if you don't have time - just let me know.

@YijunXieMS
Copy link
Copy Markdown
Contributor Author

@JimacoMS3 Added that extra line. Let me know if anything else.

@JimacoMS3
Copy link
Copy Markdown
Contributor

Thanks Yijun!

@JimacoMS3
Copy link
Copy Markdown
Contributor

#sign-off

@JimacoMS3
Copy link
Copy Markdown
Contributor

@robinsh - just signed-off please merge

@robinsh robinsh merged commit 64467e9 into Azure-Samples:master Jun 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants