【问题标题】:How to implement paging in Azure Iot device twin queries如何在 Azure IoT 设备孪生查询中实现分页
【发布时间】:2017-07-19 01:43:10
【问题描述】:

https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-query-language 的 azure 文档说“Azure IoT SDK 支持分页大型结果”,但我找不到任何示例或参考资料。

有人有想法吗?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢!我在docs.microsoft.com/en-us/azure/iot-hub/… 看到了这个示例,但它需要使用相同的查询对象,并且不适用于断开连接的状态,例如在 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
    【解决方案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 时出错。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-01
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-03
      相关资源
      最近更新 更多