gpt4 book ai didi

node.js - 是否可以向 azure Blob 添加过滤器

转载 作者:行者123 更新时间:2023-12-03 05:31:35 26 4
gpt4 key购买 nike

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

是否有可能编写一个 API,根据过滤器过滤掉我的 Blob,而无需遍历整个 Blob 容器?

最佳答案

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

现在进入过滤查询部分。通常,您很可能希望根据时间范围列出 blob(并进一步阅读内容),因为这对于冷路径分析非常实用。遗憾的是,您无法按设备 ID 筛选 Blob,因为同一 Blob 可能包含来自多个设备的消息。因此,假设您的冷路径分析将在滑动时间范围内处理批处理(很可能是一些连续作业),下面是使用 @ 的示例查询(当然过于简单,请仔细阅读内联注释) azure/storage-blob 包(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();
}
}

关于node.js - 是否可以向 azure Blob 添加过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64986584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com