java - 必须为此操作提供 PartitionKey 值

标签 java azure-cosmosdb azure-cosmosdb-sqlapi

我正在尝试从 Azure Cosmos Db 集合中检索文档。我遇到了一个错误

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.UnsupportedOperationException: PartitionKey value must be supplied for this operation.] with root cause
java.lang.UnsupportedOperationException: PartitionKey value must be supplied for this operation.

我试图在线查找如何为函数 findById() 提供分区键值,但似乎“Azure Spring data cosmosdb”没有为函数提供分区键的选项对于java实现

orderTransactionRepository.findById("id").get

最佳答案

搜索了 test code spring-data-cosmos主页中提到的分区集合的findById方法.

@Test
    public void testFindByIdForPartitionedCollection() {
        final List<Address> addresses = repository.findByPostalCode(TestConstants.POSTAL_CODE);

        assertThat(addresses.size()).isEqualTo(2);
        assertThat(addresses.get(0).getPostalCode().equals(TestConstants.POSTAL_CODE));
        assertThat(addresses.get(1).getPostalCode().equals(TestConstants.POSTAL_CODE));
    }

您可以找到语句 here :

对于Partitioned collection,如果想通过findById(id)查询记录,会抛出异常。

// Incorrect for partitioned collection, exception will be thrown
   Address result = repository.findById(id);  // Caution: Works for non-partitioned collection

相反,您可以使用自定义查询按 ID 字段名称查询记录。

// Correct, postalCode is the ID field in Address domain
   @Repository
   public interface AddressRepository extends DocumentDbRepository<Address, String> {
      List<Address> findByPostalCode(String postalCode);
   }

   // Query
   List<Address> result = repository.findByPostalCode(postalCode);

另一种方式我发现你仍然可以使用spring-data-cosmos包中的Document DB normal sdk,你只需要简单地封装该方法即可。请引用这个sample code .


只是总结一下,这基本上是由于 Spring 数据共享更改了 querylookupstrategy 正在实现的接口(interface)名称。你需要回到以前版本的 cosmos-db 即 2.0.5!这是说明问题的链接 github.com/Microsoft/spring-data-cosmosdb/issues/304

关于java - 必须为此操作提供 PartitionKey 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54640748/

相关文章:

azure - 如何使用Azure.Data.Tables列出所有表?

sql - 如何在cosmos DB中实现复杂的SQL语句,例如JOIN IN和GROUP BY

java - 输出图像加速

Azure Cosmos DB : HTTP 400 in Application Insights

c# - DocumentDB 分页与排序

azure - 如何获取在 Azure Cosmos DB 中执行存储过程时消耗的 RU 数量?

azure - 从 cosmos DB 中提取分区键

Java 不工作且机器人不发送输入

java - 我想为桌面应用程序进行 HTTP 调用(可以从命令行调用)

java - CXF servlet 的目的是什么