https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-query-language的 Azure 文档表示“Azure IoT SDK 支持大型结果分页”但我找不到任何关于如何做到这一点的示例或参考。
有人有想法吗?
x-ms-max-item-count 和 X-Ms-Continuation ,请参阅以下屏幕片段:
正如您所看到的,上面的最后一张图片没有返回延续标题,因此此页面是最后一张。
适用于 Node.js 的 Azure IoT 设备 SDK
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 {
//…
}
});
使用 安装 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));
注意:我不知道为什么,但我得到“缺少 API 2!”我使用 .NET Core 时出错。
continuationToken
对象获取
query
并将其传递给
next
或
nextAsTwin
调用。获取最后一页后,
query.continuationToken
设置为
undefined
,以便您可以将其用作终止条件。
const query = registry.createQuery("SELECT * FROM ...", pageSize: 10000)
do {
/* eslint-disable no-await-in-loop */
let {result} = await query.next(query.continuationToken);
your.code(here)
/* eslint-enable no-await-in-loop */
} while( query.query.continuationToken !== undefined )