【问题标题】:Is it possible to add filter to azure blobs是否可以将过滤器添加到 azure blob
【发布时间】:2021-03-07 05:46:26
【问题描述】:

我正在尝试根据我的过滤器检索 blob,因为我在 iot-hub 中创建了一个设备,该设备正在接收遥测数据并将其作为 blob 路由到存储帐户。 现在我想使用 Nodejs 检索 blob。

有没有可能我可以编写一个 API 来根据过滤器过滤掉我的 blob 而无需遍历整个 blob 容器?

【问题讨论】:

    标签: node.js azure azure-blob-storage azure-iot-hub azure-node-sdk


    【解决方案1】:

    默认情况下,Azure storage routing 在所选容器内创建具有约定 {iothub}/{partition}/{YYYY}/{MM}/{DD}/{HH}/{mm} 的 blob。因此,您有一个可预测的 blob 前缀,可以在查询过滤器中使用(稍后会详细介绍)。这里要注意的一件事{partition}partition 消息的零索引分区ID 被摄取。例如,如果您在创建 IoT 中心实例时选择了 4 个分区(默认),则分区 ID 将为 0、1、2 和 3。

    现在进入按过滤器查询部分。通常,您很可能希望根据时间范围列出 blob(并进一步阅读内容),因为这在您的冷路径分析中非常实用。不幸的是,您将无法按设备 ID 过滤 blob,因为同一个 blob 可能包含来自多个设备的消息。因此,假设您的冷路径分析将处理具有滑动时间范围的批处理(很可能是一些连续的工作),下面是使用@azure/storage-blob 包的示例查询(当然过度简化,请仔细阅读内联 cmets) (v12 JavaScript SDK)。您应该检查API reference 是否需要即兴创作。

    const blobServiceClient = BlobServiceClient.fromConnectionString('myStorageConnectionString');
    const containerClient = blobServiceClient.getContainerClient('myContainer');
    
    // Add logic here to select time range. 
    // For simplicity I am selecting a hardcoded time range of 2020-1-1 5:45 pm to 2020-1-1 5:46 pm 
    // (just 1 minute range)
    
    // assuming IoT hub has 4 partitions
    for (partition = 0; partition < 4; partition++) {
      // note below the prefix is picking up all blobs written at 17:45:00 to 17:45:59.xxx
      let iter = containerClient.listBlobsByHierarchy("/", { prefix: `myIotHub/${partition}/2020/01/01/17/45` });
      let entity = await iter.next();
      while (!entity.done) {
        let item = entity.value;
        if (item.kind === "prefix") {
          console.log(`\tBlobPrefix: ${item.name}`);
        } else {
          // At this point you might want to to read the blob content here. For simplicity I am just printing few blob properties below
          console.log(`\tBlobItem: name - ${item.name}, last modified - ${item.properties.lastModified}`);
        }
        entity = await iter.next();
      }
    }
    
    

    【讨论】:

    • 那么就是说没有办法通过服务器端的任何API来动态过滤数据??
    • 由于遍历大量 blob 需要花费大量时间,而且我也不确定它是哪个 blob,即我将在其上执行我的功能的相关设备数据。其次,有多个具有嵌套层次结构的设备,在此基础上,将无法获取设备信息,但它在 blob 中非常耗时。有什么办法可以直接取出相关数据。
    • Blob 存储用于存储文件(非结构化数据)。如果你想要查询分析,要么选择不同类型的数据存储(如 sql 或 no-sql),要么在 blob/datalake 存储之上使用一些分析引擎(如数据块、突触等)。更多的是基于您的要求的架构/设计决策。
    • 在 iot-hub 中使用设备孪生可以实现吗?
    • 非常感谢您耐心地回答我的问题。我是天蓝色的新手,但仍然不确定很多功能。我会看到我的问题的解决方法,但你的回答对我帮助很大。
    猜你喜欢
    • 1970-01-01
    • 2019-03-03
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2020-11-29
    • 2017-09-21
    • 1970-01-01
    相关资源
    最近更新 更多