azure - 如何在 Spring Boot 中为 CosmosDb 设置分区键

标签 azure spring-boot spring-data azure-cosmosdb partitioning

答案可能看起来很明显,但我尝试了多种注释和配置的组合,但无法找到一种在所有情况下都有效的实现方法(例如执行 CRUD、DocumentDbRepository 和自定义方法的能力)。

注解示例:

@Data
@...
@Document(collection="items")
public class Item {
   @Id
   private String itemId;
   @PartitionKey
   private String storeNumber;
   ...
}

配置示例:

...
    public DocumentClient config() {
      DocumentClient client = new DocumentClient(
            uri, key, getConnectionPolicy(), ConsistencyLevel.Session);

        client.readDatabases(new FeedOptions()).getQueryIterator().forEachRemaining(database -> {
            System.out.println("Spring database link = " + database.getSelfLink());
        });

        /*
        DocumentCollection coll = new DocumentCollection();
        coll.setId("itemId");
// Corrected based on Sajeetharan's answer:
        PartitionKeyDefinition partitionKeyDefinition = new  PartitionKeyDefinition();
        Collection<String> paths = new ArrayList<>();
        partitionKeyDefinition.setPaths(paths);
        paths.add("storeNumber");
        coll.setPartitionKey(partitionKeyDefinition);
        coll.setResourceId("items");

        // OR

        RequestOptions options = new RequestOptions();
        options.setPartitionKey(new PartitionKey("clubNumber"));

        try {
            client.createCollection(String.format("/dbs/%s", DATABASE), coll, options);
        } catch (DocumentClientException e) {
            e.printStackTrace();
        }
        */

        return client;
    }
...

到目前为止,当我让 Microsoft 为我处理分区键并仅指定 @Id 字段和客户端配置时,我的代码就可以工作。但是,我想在这种情况下使用特定的分区键。上面的例子只是我为了让它发挥作用而尝试实现的不同组合的一小部分。尽管如此,我还是遇到了一些异常,例如:java.lang.UnsupportedOperationException:必须为此操作提供 PartitionKey 值,对于像 findById() 这样简单的操作。话虽如此,我希望能得到一些帮助来找到解决方案。

郑重声明,在 Azure UI 中删除集合后,给出的三个示例实际上都会在 Spring boot 中为我创建一个集合。在设置中,分区键显示为“/storeNumber”,这是我没有在 Azure UI 中手动设置的正确分区键。这让我不明白为什么编译器提示我没有提供分区键,尽管 Spring Boot 直接负责使用我在 Azure UI 中看到的(正确的)分区键生成集合。

我正在使用:

        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-documentdb</artifactId>
            <version>LATEST</version> 
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>spring-data-cosmosdb</artifactId>
            <version>2.1.1</version>
        </dependency> <!-- mainly needed for @Id -->

在我的 POM 中。

*** 我的问题已经解决,希望有人指导我应该做什么。如果将问题标记为重复或完全删除问题是合适的,我会很乐意这样做,但我确实认为这对其他人可能有用。与此同时,我将回答我自己的问题,因为给出的答案并没有直接解决它,尽管评论中提供的链接确实有很大帮助。

最佳答案

你必须这样设置,

 PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition();
 Collection<String> paths = new ArrayList<>();
 paths.add(partitionKey);
 partitionKeyDefinition.setPaths(paths);
 collection.setPartitionKey(partitionKeyDefinition);

关于azure - 如何在 Spring Boot 中为 CosmosDb 设置分区键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55267167/

相关文章:

java - 带有 JWT 的 Spring Security for REST API

java - 无法将 Spring Boot war 部署到 Elastic Beanstalk

java - 通过命令行选择application.properties文件

java - Spring Data MongoDB : Database name must only contain letters, 数字、下划线和破折号

spring-data - 从测试运行时如何在 Spring-Data 中启用异常翻译?

php - 尝试从 azure blob 下载文件,mime 类型有问题

azure - AKS 添加非 Azure 节点

azure - 如何访问 VMSS 的 Log Analytics 代理扩展的日志?

spring - Zuul Ribbon 异常总是返回 500 响应

Azure 媒体服务器编码突然开始失败