如何在 Azure Iot 设备孪生查询中实现分页
How to implement paging in Azure Iot device twin queries
https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-query-language 上的 azure 文档说“Azure IoT SDK 支持对大结果进行分页”,但我找不到有关如何操作的任何示例或参考.
有人知道吗?
它基于 REST API POST 调用和 header,例如 x-ms-max-item-count 和 X-Ms-Continuation ,请参阅以下屏幕片段:
如您所见,上面的最后一张图片 return 不是续集 header,因此这一页是最后一页。
- 此外,查看 https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-query-language 使用服务 SDK 的分页示例。
使用Azure IoT device SDK for Node.js
var Registry = require('azure-iothub').Registry;
var connectionString = '{iothub connection string}';
var registry = Registry.fromConnectionString(connectionString);
var pageSize = 10;
var query = registry.createQuery("SELECT * FROM devices", pageSize);
获取第一页:
query.next(function (err, devices, response) {
if (err) {
console.error('Failed to query devices: ' + err.message);
} else {
var continuationToken = response.headers["x-ms-continuation"]; // Example: "c2tpcD0wJnRvdGFsPTEwJmxhc3Q9ZGV2aWNlMTA="
var pageToken = new Buffer(continuationToken, 'base64').toString('ascii'); // Example: "skip=0&total=10&last=device10"
//Optionally, you may persist the token and use it for the next pages
}
});
要获取下一页,
query.next(continuationToken , function (err, devices, response) {…} //previous token
获取第四页
var pageNumber = 3; // zero based
var pageToken = "skip=" + pageNumber * pageSize + "&total=" + pageSize; // "skip=30&total=10"
var continuationToken = new Buffer(pageToken).toString('base64'); //"c2tpcD0zMCZ0b3RhbD0xMA=="
query.next(continuationToken, function (err, devices, response) {
if (err) {
console.error('Failed to query devices: ' + err.message);
} else {
//…
}
});
使用Azure IoT service SDK for .NET
安装Microsoft.Azure.Devices nuget 包
string connectionString = "{iot hub connection string}";
int pageSize = 10;
var registryManager = RegistryManager.CreateFromConnectionString(connectionString);
var query = registryManager.CreateQuery("SELECT * FROM devices", pageSize);
Console.WriteLine("First page");
var firstPage = query.GetNextAsTwinAsync();
var response = (QueryResponse<Microsoft.Azure.Devices.Shared.Twin>)firstPage.Result;
var continuationToken1 = response.ContinuationToken;
response.ToList().ForEach(d => Console.WriteLine(d.DeviceId));
Console.WriteLine("Next page");
var nextPage = query.GetNextAsTwinAsync(new QueryOptions() { ContinuationToken = continuationToken1 });
nextPage.Result.ToList().ForEach(d => Console.WriteLine(d.DeviceId));
Console.WriteLine("Fourth page");
var pageNumber = 3; // zero based
var pageToken = "skip=" + pageNumber * pageSize + "&total=" + pageSize; // "skip=30&total=10"
var continuationToken3 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(pageToken)); //"c2tpcD0zMCZ0b3RhbD0xMA=="
var fourthPage = query.GetNextAsTwinAsync(new QueryOptions() { ContinuationToken = continuationToken3 });
fourthPage.Result.ToList().ForEach(d => Console.WriteLine(d.DeviceId));
注意:我不知道为什么,但我在使用 .NET Core 时遇到 "missing API 2!" 错误。
https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-query-language 上的 azure 文档说“Azure IoT SDK 支持对大结果进行分页”,但我找不到有关如何操作的任何示例或参考.
有人知道吗?
它基于 REST API POST 调用和 header,例如 x-ms-max-item-count 和 X-Ms-Continuation ,请参阅以下屏幕片段:
如您所见,上面的最后一张图片 return 不是续集 header,因此这一页是最后一页。
- 此外,查看 https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-query-language 使用服务 SDK 的分页示例。
使用Azure IoT device SDK for Node.js
var Registry = require('azure-iothub').Registry;
var connectionString = '{iothub connection string}';
var registry = Registry.fromConnectionString(connectionString);
var pageSize = 10;
var query = registry.createQuery("SELECT * FROM devices", pageSize);
获取第一页:
query.next(function (err, devices, response) {
if (err) {
console.error('Failed to query devices: ' + err.message);
} else {
var continuationToken = response.headers["x-ms-continuation"]; // Example: "c2tpcD0wJnRvdGFsPTEwJmxhc3Q9ZGV2aWNlMTA="
var pageToken = new Buffer(continuationToken, 'base64').toString('ascii'); // Example: "skip=0&total=10&last=device10"
//Optionally, you may persist the token and use it for the next pages
}
});
要获取下一页,
query.next(continuationToken , function (err, devices, response) {…} //previous token
获取第四页
var pageNumber = 3; // zero based
var pageToken = "skip=" + pageNumber * pageSize + "&total=" + pageSize; // "skip=30&total=10"
var continuationToken = new Buffer(pageToken).toString('base64'); //"c2tpcD0zMCZ0b3RhbD0xMA=="
query.next(continuationToken, function (err, devices, response) {
if (err) {
console.error('Failed to query devices: ' + err.message);
} else {
//…
}
});
使用Azure IoT service SDK for .NET
安装Microsoft.Azure.Devices nuget 包
string connectionString = "{iot hub connection string}";
int pageSize = 10;
var registryManager = RegistryManager.CreateFromConnectionString(connectionString);
var query = registryManager.CreateQuery("SELECT * FROM devices", pageSize);
Console.WriteLine("First page");
var firstPage = query.GetNextAsTwinAsync();
var response = (QueryResponse<Microsoft.Azure.Devices.Shared.Twin>)firstPage.Result;
var continuationToken1 = response.ContinuationToken;
response.ToList().ForEach(d => Console.WriteLine(d.DeviceId));
Console.WriteLine("Next page");
var nextPage = query.GetNextAsTwinAsync(new QueryOptions() { ContinuationToken = continuationToken1 });
nextPage.Result.ToList().ForEach(d => Console.WriteLine(d.DeviceId));
Console.WriteLine("Fourth page");
var pageNumber = 3; // zero based
var pageToken = "skip=" + pageNumber * pageSize + "&total=" + pageSize; // "skip=30&total=10"
var continuationToken3 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(pageToken)); //"c2tpcD0zMCZ0b3RhbD0xMA=="
var fourthPage = query.GetNextAsTwinAsync(new QueryOptions() { ContinuationToken = continuationToken3 });
fourthPage.Result.ToList().ForEach(d => Console.WriteLine(d.DeviceId));
注意:我不知道为什么,但我在使用 .NET Core 时遇到 "missing API 2!" 错误。