【问题标题】:Query cosmosdb using partition key使用分区键查询 cosmosdb
【发布时间】:2017-05-23 13:48:46
【问题描述】:

我有一个以部门为 partitionKey 的员工 dto。

  1. 如何获取任意给定时间点的分区数?
  2. 如何使用 partitionKey 查询 cosmosdb(documentDB),无论是 query explorer 还是 java api 都可以?

我尝试了什么:

List<Document> documentList = documentClient.queryDocuments(getCollection().getSelfLink(),"SELECT * FROM root r WHERE r.partitionKey ='" + partitionKey+"'", null).getQueryIterable().toList();

我最终从 java api 得到 IllegalStateException 状态异常,并且查询导出器也没有给出任何输出。任何帮助都是非常可观的。

【问题讨论】:

    标签: java azure azure-cosmosdb


    【解决方案1】:

    部分答案:

    鉴于Partition Key 属性的名称是department,请将您的查询更改为:

    List<Document> documentList = 
        documentClient.queryDocuments(
             getCollection().getSelfLink(),
                  "SELECT * FROM root r WHERE r.department ='" + 
                  partitionKey + "'", null).getQueryIterable().toList();
    

    【讨论】:

    • 非常感谢@gaurav,有没有办法在任何给定时间点获取分区数?
    • 请看这个:stackoverflow.com/questions/43319308/…。如果有帮助,请告诉我,我会将其包含在答案中。
    • 我希望partitionKey 不是用户可控制的输入,因为如果来自用户,这很容易受到SQLi 的影响
    【解决方案2】:

    您也可以通过提供收藏链接来实现同样的目的

    List<Document> documentList = documentClient.queryDocuments("/dbs/<yourdbname>/colls/<collectionname>","SELECT * FROM root r WHERE r.department ='" + partitionKey+"'", null).getQueryIterable().toList();
    

    【讨论】:

      猜你喜欢
      • 2019-05-05
      • 1970-01-01
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      相关资源
      最近更新 更多