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

时间:2017-07-19 01:43:10

标签: azure-iot-hub azure-iot-sdk

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

有没有人有想法?

2 个答案:

答案 0 :(得分:3)

它基于REST API POST调用和标题,例如 x-ms-max-item-count X-Ms-Continuation ,请参阅以下屏幕截图:

query-firstpage

query-nextpage

query-lastpage

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

答案 1 :(得分:2)

使用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));

注意:我不知道为什么,但我“缺少API 2!”我使用.NET Core时出错。