0

https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-query-language上的 azure 文档说“<em>Azure IoT SDK 支持大结果的分页”,但我找不到有关如何执行此操作的任何示例或参考。

有人有想法吗?

4

2 回答 2

4

它基于 REST API POST 调用和标头,例如x-ms-max-item-countX-Ms-Continuation,请参阅以下屏幕片段:

查询首页

查询下一页

查询最后一页

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

于 2017-07-19T05:54:40.497 回答
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 时出错。

于 2017-07-20T17:09:31.077 回答