【问题标题】:Query on the basis of Timestamp Azure Storage Table/Azure Cosmos DB根据时间戳查询 Azure Storage Table/Azure Cosmos DB
【发布时间】:2019-05-13 17:27:16
【问题描述】:

我正在尝试使用适用于 node.js 的 azure table storage SDK 基于时间戳查询 Azure Cosmos DB 表中的实体

我在 DB 中的时间戳如下

在代码中我正在查询创建这样的过滤器

const filter = storage.TableQuery.dateFilter(
    "Timestamp",
    storage.TableUtilities.QueryComparisons.GREATER_THAN,
    new Date(Date.UTC(2019, 0, 1))
);

query = new TableQuery().where(filter);

但是没有为我返回任何结果。

我google了一下,也发现了下面的堆栈溢出问题

nodejs query azure storage table according to timestamp

答案中的两种解决方案都不适合我。

【问题讨论】:

    标签: node.js azure timestamp azure-cosmosdb azure-table-storage


    【解决方案1】:

    这是我的示例代码,如下所示,您可以参考再试一次。希望对您有所帮助。

    我通过命令 npm i azure-storage 为 Node.js 安装了 Azure Storage SDK

    var azure = require('azure-storage');
    var tableService = azure.createTableService('<your storage account>','<your storage key>');
    

    有两种方法可以构建查询对象。

    1. 直接使用where函数与过滤模板字符串和值。

      var query = new azure.TableQuery().where('Timestamp ge ?', new Date(Date.UTC(2019, 0, 1)));
      
    2. 先构建过滤表达式,然后生成查询对象。

      var filter = azure.TableQuery.dateFilter('Timestamp', azure.TableUtilities.QueryComparisons.GREATER_THAN, new Date(Date.UTC(2019, 0, 1)));
      var query = new azure.TableQuery().where(filter);
      

    最后,查询表实体。

    console.log(query);
    tableService.queryEntities('<your table name>', query, null, function(error, result, response) {
      if(!error) {
        // query was successful
        console.log(result);
      }
      console.log(error);
    });
    

    【讨论】:

    • 你能分享你的 azure-storage npm 包的版本吗?我的是~2.10.3,不确定你是否有任何早期版本,新版本有一些重大变化
    • @UmairFarooq azure-storage@2.10.3 和你的一样,我通过 npm list 显示。
    • 我与这个答案的唯一区别是 createTableService 的重载。您的答案在 createTableService 中有存储帐户名称和访问密钥,而不是在我的情况下是 cosmos db table api 帐户的连接字符串。当我转到存储帐户(与我的 cosmos db 具有相同的资源组)并转到存储资源管理器时,有一组新的表,而我要查询的 cosmos db 表不在这里。我创建了一个表,添加了一条记录并尝试了上面的示例,现在它正在从那里查询数据。你能指导我为什么它不能与 cosmos db table api 一起使用吗?
    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 2017-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多