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

Azure iot hub 如何在Azure Iot设备双查询中实现分页,azure-iot-hub,azure-iot-sdk,Azure Iot Hub,Azure Iot Sdk,位于的azure文档说“azure IoT SDK支持大结果分页”,但我找不到任何关于如何进行分页的示例或参考 有人有想法吗?它基于REST API POST调用和标题,如x-ms-max-item-count和x-ms-Continuation,请参见以下屏幕片段: 正如您所见,上面最后一张图片没有返回延续标题,因此此页是最后一页 此外,还可以查看使用服务SDK的分页示例。 使用 要获取第一页,请执行以下操作: query.next(function (err, devices, respon

位于的azure文档说“azure IoT SDK支持大结果分页”,但我找不到任何关于如何进行分页的示例或参考


有人有想法吗?

它基于REST API POST调用和标题,如x-ms-max-item-count和x-ms-Continuation,请参见以下屏幕片段:

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

此外,还可以查看使用服务SDK的分页示例。 使用

要获取第一页,请执行以下操作:

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

非常感谢!我在上看到了这个示例,但它需要使用相同的查询对象,并且不适用于断开连接的状态,比如web请求之间。我可以在请求之间保留X-Ms-Continuation令牌,您的答案非常有效!如果我想跳过一页,我该怎么办?例如,如果我想要第四个页面而不检索以前的页面?没有用于跳过页面的内置机制,您必须始终获取下一个延续令牌。但是,您可以尝试修改用于跳过设备的延续令牌。请注意,继续标记=base64编码文本,例如skip=0&total=10&last=Device12,其中skip始终为0,请尝试更改它。continue令牌的必需参数为skip和total。跳过前10个设备的示例:skip=10&total=0,因此在base64编码之后,x-ms-continuation:c2tpcD0xMCZ0b3RhbD0w
    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));