Azure / azure-iot-sdk-node

A Node.js SDK for connecting devices to Microsoft Azure IoT services
https://docs.microsoft.com/en-us/azure/iot-hub/
Other
262 stars 227 forks source link

how to send messages to specific partition #74

Closed willin closed 7 years ago

willin commented 7 years ago

Context

Description of the issue:

how to send messages to specific partition

Code sample exhibiting the issue:

C#: https://docs.microsoft.com/en-us/dotnet/api/microsoft.servicebus.messaging.eventhubclient.createpartitionedsender?view=azureservicebus-4.1.1

https://github.com/Azure/azure-iot-sdk-node/blob/master/device/samples/simple_sample_device.js#L45

      var windSpeed = 10 + (Math.random() * 4); // range: [10, 14]
      var temperature = 20 + (Math.random() * 10); // range: [20, 30]
      var humidity = 60 + (Math.random() * 20); // range: [60, 80]
      var data = JSON.stringify({ deviceId: 'myFirstDevice', windSpeed: windSpeed, temperature: temperature, humidity: humidity });
      var message = new Message(data);
      message.properties.add('temperatureAlert', (temperature > 28) ? 'true' : 'false');
      // to partition no.1, will this work?
      message.properties.add('x-opt-partition-key', 'pk:1');
      console.log('Sending message: ' + message.getData());
      client.sendEvent(message, printResultFor('send'));
pierreca commented 7 years ago

This is not a feature we have implemented yet and it's unlikely we will get to it soon.

Right now all messages from a specific device always end up on the same partition so there is some predictability though, so I don't know about your scenario but that might help?

Here's the hash function to know which partition is going to be used based on the device id:

https://github.com/Azure/azure-iot-sdk-csharp/blob/a5108f60f343dee867a2243e5a59c4483f0c5a8b/service/Microsoft.Azure.Devices/Common/EventHubPartitionKeyResolver.cs

Feel free to add more context or let us know if that'll work for you.

willin commented 7 years ago

how about event-hubs createSender? @pierreca

https://github.com/Azure/azure-event-hubs-node/blob/master/send_receive/lib/sender.js#L50

willin commented 7 years ago
// to partition no.1, will this work?
message.properties.add('x-opt-partition-key', 'pk:1');

means this won't work?

pierreca commented 7 years ago

Adding x-opt-partition-key to a message sent to IoT Hub will not work.

v1shva commented 5 years ago

Is there any other way to route messages to a specific partition?

neha-s-deshpande commented 4 years ago

In python, can we check to which partition device to cloud message is going?

AiHaibara commented 8 months ago

it seems make the perf test become more complex. and if send batch to iothub, does this can control random for different partition or same partition to process?