如何在Azure Iot设备孪生查询中实现分页

问题描述 投票:0回答:3

https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-query-language的 Azure 文档表示“Azure IoT SDK 支持大型结果分页”但我找不到任何关于如何做到这一点的示例或参考。

有人有想法吗?

azure-iot-hub azure-iot-sdk
3个回答
5
投票
它基于 REST API POST 调用和标头,例如

x-ms-max-item-countX-Ms-Continuation ,请参阅以下屏幕片段:

query-firstpage

query-nextpage

query-lastpage

正如您所看到的,上面的最后一张图片没有返回延续标题,因此此页面是最后一张。


3
投票
使用

适用于 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 { //… } });
使用 

适用于 .NET 的 Azure IoT 服务 SDK

安装 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 时出错。


0
投票
我发现以下方法对我有用。它使用从

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 )
    
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.