使用 REST 将批处理事件发送到 Azure 事件中心
Sending batch events to Azure event hub using REST
我可以使用 NodeMCU 将事件发送到事件中心。我使用下面的代码
https://www.hackster.io/stepanb/proof-of-concept-nodemcu-arduino-and-azure-event-hub-a33043
但是我想发送批量事件。我为此引用了此页面
https://docs.microsoft.com/en-us/rest/api/eventhub/send-batch-events
这是 NodeMCU 串行监视器发送单个事件的快照
根据 link 我们必须将 "content type" 更改为
Content-Type: application/vnd.microsoft.servicebus.json
和
格式的负载
data= "[{'Temperature':25.25 , 'Deviceid':esp3} , {'Temperature':30.30 , 'Deviceid':esp3}]";
这是批处理事件串行监视器的快照
我错过了什么吗?为了接收批处理事件,我需要对流分析进行任何更改。我是 Azure 和 Whosebug 的新手。
这是发送单个事件的代码
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <String.h>
#include "sha256.h"
#include "Base64.h"
// START: Azure Evet Hub settings
const char* KEY = "dhGE6MbbRLe6IPZs6dOHd3byQlEJ8YzqnW+uBAT7T/Q="; // main event hub key
const char* KEY_NAME = "RootManageSharedAccessKey"; // key name
const char* HOST = "rishieventhub2.servicebus.windows.net"; // event hub name
const char* END_POINT = "/rishidata/messages"; // name of eventhub created inside event hub
// END: Azure Evet Hub settings
// START: WiFi settings
const char* SSID = "Nokia";
const char* PASSWORD = "rishikesh";
// END: WiFi settings
String request;
String data;
String fullSas;
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
// START: Naive URL Encode
String url = "https://" + (String)HOST + (String)END_POINT;
url.replace(":", "%3A");
url.replace("/", "%2F");
Serial.println(url);
// END: Naive URL Encode
// START: Create SAS
// https://azure.microsoft.com/en-us/documentation/articles/service-bus-sas-overview/
// Where to get secods since the epoch: local service, SNTP, RTC
int expire = 1711104241;
String stringToSign = url + "\n" + expire;
// START: Create signature
Sha256.initHmac((const uint8_t*)KEY, 44);
Sha256.print(stringToSign);
char* sign = (char*) Sha256.resultHmac();
int signLen = 32;
// END: Create signature
// START: Get base64 of signature
int encodedSignLen = base64_enc_len(signLen);
char encodedSign[encodedSignLen];
base64_encode(encodedSign, sign, signLen);
String encodedSas = (String) encodedSign;
// Naive URL encode
encodedSas.replace("=", "%3D");
Serial.println(encodedSas);
// END: Get base64 of signature
// SharedAccessSignature
fullSas = "sr=" + url + "&sig="+ encodedSas + "&se=" + expire +"&skn=" + KEY_NAME;
// END: create SAS
// START: Wifi connection
Serial.print("connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// END: Wifi connection
}
int temp=15;
void loop() {
WiFiClientSecure client;
if (!client.connect(HOST, 443)) {
Serial.println("connection failed");
return;
}
//data= "[{'Temperature':25.25 , 'Deviceid':'esp3'} , {'Temperature':30.30 , 'Deviceid':'esp3'}]"; // for batch events
data = "{'Temperature':25.25 , 'Deviceid':'esp3'}"; // for single events
request = String("POST ")+"https://rishieventhub2.servicebus.windows.net" + END_POINT + "?timeout=60&api-version=2014-01" + " HTTP/1.1\r\n" +
"Host: " + HOST + "\r\n" +
"Authorization: SharedAccessSignature " + fullSas + "\r\n" +
"Content-Type: application/atom+xml;type=entry;charset=utf-8\r\n" +
"Content-Length: " + data.length() + "\r\n\r\n" +
data;
Serial.println(request);
client.print(request);
delay(100);
发送单个事件的代码有效,我能够将值存储在 table 存储中,我可以看到使用 azure table 存储。
这是我尝试发送批处理事件的代码
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <String.h>
#include "sha256.h"
#include "Base64.h"
// START: Azure Evet Hub settings
const char* KEY = "dhGE6MbbRLe6IPZs6dOHd3byQlEJ8YzqnW+uBAT7T/Q="; // main event hub key
const char* KEY_NAME = "RootManageSharedAccessKey"; // key name
const char* HOST = "rishieventhub2.servicebus.windows.net"; // event hub name
const char* END_POINT = "/rishidata/messages"; // name of eventhub created inside event hub
// END: Azure Evet Hub settings
// START: WiFi settings
const char* SSID = "Nokia";
const char* PASSWORD = "rishikesh";
// END: WiFi settings
String request;
String data;
String fullSas;
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
// START: Naive URL Encode
String url = "https://" + (String)HOST + (String)END_POINT;
url.replace(":", "%3A");
url.replace("/", "%2F");
Serial.println(url);
// END: Naive URL Encode
// START: Create SAS
// https://azure.microsoft.com/en-us/documentation/articles/service-bus-sas-overview/
// Where to get secods since the epoch: local service, SNTP, RTC
int expire = 1711104241;
String stringToSign = url + "\n" + expire;
// START: Create signature
Sha256.initHmac((const uint8_t*)KEY, 44);
Sha256.print(stringToSign);
char* sign = (char*) Sha256.resultHmac();
int signLen = 32;
// END: Create signature
// START: Get base64 of signature
int encodedSignLen = base64_enc_len(signLen);
char encodedSign[encodedSignLen];
base64_encode(encodedSign, sign, signLen);
String encodedSas = (String) encodedSign;
// Naive URL encode
encodedSas.replace("=", "%3D");
Serial.println(encodedSas);
// END: Get base64 of signature
// SharedAccessSignature
fullSas = "sr=" + url + "&sig="+ encodedSas + "&se=" + expire +"&skn=" + KEY_NAME;
// END: create SAS
// START: Wifi connection
Serial.print("connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// END: Wifi connection
}
int temp=15;
void loop() {
WiFiClientSecure client;
if (!client.connect(HOST, 443)) {
Serial.println("connection failed");
return;
}
data= "[{'Temperature':25.25 , 'Deviceid':'esp3'} , {'Temperature':30.30 , 'Deviceid':'esp3'}]"; // for batch events
// data = "{'Temperature':25.25 , 'Deviceid':'esp3'}"; // for single events
request = String("POST ")+"https://rishieventhub2.servicebus.windows.net" + END_POINT + "?timeout=60&api-version=2014-01" + " HTTP/1.1\r\n" +
"Host: " + HOST + "\r\n" +
"Authorization: SharedAccessSignature " + fullSas + "\r\n" +
"Content-Type: application/vnd.microsoft.servicebus.json" +"\r\n" +
"Content-Length: " + data.length() + "\r\n\r\n" +
data;
Serial.println(request);
client.print(request);
delay(100);
}
以上代码适用于发送批量事件。需要更改的一件事是,在流分析中,我们需要不同的分区键键和输出行键(table 存储)。至于发送单个事件,分区和行键可以相同。
我可以使用 NodeMCU 将事件发送到事件中心。我使用下面的代码
https://www.hackster.io/stepanb/proof-of-concept-nodemcu-arduino-and-azure-event-hub-a33043
但是我想发送批量事件。我为此引用了此页面
https://docs.microsoft.com/en-us/rest/api/eventhub/send-batch-events
这是 NodeMCU 串行监视器发送单个事件的快照
根据 link 我们必须将 "content type" 更改为
Content-Type: application/vnd.microsoft.servicebus.json
和
格式的负载data= "[{'Temperature':25.25 , 'Deviceid':esp3} , {'Temperature':30.30 , 'Deviceid':esp3}]";
这是批处理事件串行监视器的快照
我错过了什么吗?为了接收批处理事件,我需要对流分析进行任何更改。我是 Azure 和 Whosebug 的新手。
这是发送单个事件的代码
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <String.h>
#include "sha256.h"
#include "Base64.h"
// START: Azure Evet Hub settings
const char* KEY = "dhGE6MbbRLe6IPZs6dOHd3byQlEJ8YzqnW+uBAT7T/Q="; // main event hub key
const char* KEY_NAME = "RootManageSharedAccessKey"; // key name
const char* HOST = "rishieventhub2.servicebus.windows.net"; // event hub name
const char* END_POINT = "/rishidata/messages"; // name of eventhub created inside event hub
// END: Azure Evet Hub settings
// START: WiFi settings
const char* SSID = "Nokia";
const char* PASSWORD = "rishikesh";
// END: WiFi settings
String request;
String data;
String fullSas;
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
// START: Naive URL Encode
String url = "https://" + (String)HOST + (String)END_POINT;
url.replace(":", "%3A");
url.replace("/", "%2F");
Serial.println(url);
// END: Naive URL Encode
// START: Create SAS
// https://azure.microsoft.com/en-us/documentation/articles/service-bus-sas-overview/
// Where to get secods since the epoch: local service, SNTP, RTC
int expire = 1711104241;
String stringToSign = url + "\n" + expire;
// START: Create signature
Sha256.initHmac((const uint8_t*)KEY, 44);
Sha256.print(stringToSign);
char* sign = (char*) Sha256.resultHmac();
int signLen = 32;
// END: Create signature
// START: Get base64 of signature
int encodedSignLen = base64_enc_len(signLen);
char encodedSign[encodedSignLen];
base64_encode(encodedSign, sign, signLen);
String encodedSas = (String) encodedSign;
// Naive URL encode
encodedSas.replace("=", "%3D");
Serial.println(encodedSas);
// END: Get base64 of signature
// SharedAccessSignature
fullSas = "sr=" + url + "&sig="+ encodedSas + "&se=" + expire +"&skn=" + KEY_NAME;
// END: create SAS
// START: Wifi connection
Serial.print("connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// END: Wifi connection
}
int temp=15;
void loop() {
WiFiClientSecure client;
if (!client.connect(HOST, 443)) {
Serial.println("connection failed");
return;
}
//data= "[{'Temperature':25.25 , 'Deviceid':'esp3'} , {'Temperature':30.30 , 'Deviceid':'esp3'}]"; // for batch events
data = "{'Temperature':25.25 , 'Deviceid':'esp3'}"; // for single events
request = String("POST ")+"https://rishieventhub2.servicebus.windows.net" + END_POINT + "?timeout=60&api-version=2014-01" + " HTTP/1.1\r\n" +
"Host: " + HOST + "\r\n" +
"Authorization: SharedAccessSignature " + fullSas + "\r\n" +
"Content-Type: application/atom+xml;type=entry;charset=utf-8\r\n" +
"Content-Length: " + data.length() + "\r\n\r\n" +
data;
Serial.println(request);
client.print(request);
delay(100);
发送单个事件的代码有效,我能够将值存储在 table 存储中,我可以看到使用 azure table 存储。 这是我尝试发送批处理事件的代码
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <String.h>
#include "sha256.h"
#include "Base64.h"
// START: Azure Evet Hub settings
const char* KEY = "dhGE6MbbRLe6IPZs6dOHd3byQlEJ8YzqnW+uBAT7T/Q="; // main event hub key
const char* KEY_NAME = "RootManageSharedAccessKey"; // key name
const char* HOST = "rishieventhub2.servicebus.windows.net"; // event hub name
const char* END_POINT = "/rishidata/messages"; // name of eventhub created inside event hub
// END: Azure Evet Hub settings
// START: WiFi settings
const char* SSID = "Nokia";
const char* PASSWORD = "rishikesh";
// END: WiFi settings
String request;
String data;
String fullSas;
WiFiClientSecure client;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
// START: Naive URL Encode
String url = "https://" + (String)HOST + (String)END_POINT;
url.replace(":", "%3A");
url.replace("/", "%2F");
Serial.println(url);
// END: Naive URL Encode
// START: Create SAS
// https://azure.microsoft.com/en-us/documentation/articles/service-bus-sas-overview/
// Where to get secods since the epoch: local service, SNTP, RTC
int expire = 1711104241;
String stringToSign = url + "\n" + expire;
// START: Create signature
Sha256.initHmac((const uint8_t*)KEY, 44);
Sha256.print(stringToSign);
char* sign = (char*) Sha256.resultHmac();
int signLen = 32;
// END: Create signature
// START: Get base64 of signature
int encodedSignLen = base64_enc_len(signLen);
char encodedSign[encodedSignLen];
base64_encode(encodedSign, sign, signLen);
String encodedSas = (String) encodedSign;
// Naive URL encode
encodedSas.replace("=", "%3D");
Serial.println(encodedSas);
// END: Get base64 of signature
// SharedAccessSignature
fullSas = "sr=" + url + "&sig="+ encodedSas + "&se=" + expire +"&skn=" + KEY_NAME;
// END: create SAS
// START: Wifi connection
Serial.print("connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// END: Wifi connection
}
int temp=15;
void loop() {
WiFiClientSecure client;
if (!client.connect(HOST, 443)) {
Serial.println("connection failed");
return;
}
data= "[{'Temperature':25.25 , 'Deviceid':'esp3'} , {'Temperature':30.30 , 'Deviceid':'esp3'}]"; // for batch events
// data = "{'Temperature':25.25 , 'Deviceid':'esp3'}"; // for single events
request = String("POST ")+"https://rishieventhub2.servicebus.windows.net" + END_POINT + "?timeout=60&api-version=2014-01" + " HTTP/1.1\r\n" +
"Host: " + HOST + "\r\n" +
"Authorization: SharedAccessSignature " + fullSas + "\r\n" +
"Content-Type: application/vnd.microsoft.servicebus.json" +"\r\n" +
"Content-Length: " + data.length() + "\r\n\r\n" +
data;
Serial.println(request);
client.print(request);
delay(100);
}
以上代码适用于发送批量事件。需要更改的一件事是,在流分析中,我们需要不同的分区键键和输出行键(table 存储)。至于发送单个事件,分区和行键可以相同。