使用 Spring Data MongoDB 指定分片集合 [英] Specifing a Sharded Collection with Spring Data MongoDB

查看:44
本文介绍了使用 Spring Data MongoDB 指定分片集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring Boot 和 Spring Data MongoDB 与底层分片 MongoDB 集群交互.我的 Spring Boot 应用程序通过 mongos 路由器访问集群.

I am using Spring Boot and Spring Data MongoDB to interface with an underlying sharded MongoDB cluster. My Spring Boot Application access the cluster via a mongos router.

使用 Spring Data MongoDB,您可以通过 @Document(collection = "nameOfCollection") 指定对象持久化到的集合,或者默认为类名(首字母小写).这些集合不需要事先存在;它们可以在运行时创建.

Using Spring Data MongoDB, you can specify the collection an object is persisted to via @Document(collection = "nameOfCollection"), or it defaults to the class name (first letter lowercase). These collections do not need to exist before-hand; they can be created at runtime.

要在 MongoDB 中对集合进行分片,您需要

To shard a collection in MongoDB, you need to

1 - 在数据库上启用分片:sh.enableSharding("myDb")

1 - Enable sharding on the Database: sh.enableSharding("myDb")

2 - 在分片数据库上分片集合:sh.shardCollection("myDb.myCollection", {id:"hashed"})

2 - Shard the collection on a sharded database: sh.shardCollection("myDb.myCollection", {id:"hashed"})

假设有一个现有的分片数据库,Spring Data MongoDB 是否提供了一种使用分片键对集合进行分片的方法? 据我所知,我无法使用 Spring 对集合进行分片,并且因此必须在启动应用程序运行之前配置分片集合.我觉得奇怪的是,Spring 允许我使用未定义的集合,但不提供配置集合的方法.

Assuming there is an existing sharded database, does Spring Data MongoDB offer a way to shard a collection with a shard key? As far as I can tell, I cannot shard a collection with Spring, and therefore must configure the sharded collection before my Boot application runs. I find it odd that Spring would allow me to use undefined collections, but does not provide a way to configure the collection.

我已经看过 使用 spring mongo 进行分片如何在 spring-data 中为 mongo 配置对分片集合的访问?更多地参考分片 MongoDB 集群的部署.这个问题假设所有的管道都在那里,并且集合本身必须被分片.

I have seen both Sharding with spring mongo and How configuring access to a sharded collection in spring-data for mongo? which refer more to the deployment of a sharded MongoDB cluster. This question assumes all the plumbing is there and that the collection itself simply must be sharded.

推荐答案

尽管这个问题很老,但我也有同样的问题,而且从最近开始似乎没有提供自定义分片键了.

Despite this question being old, I've got the same question, and it looks like there is away to provide custom sharding key since recently.

基于注解的 Shard Key 配置在 spring-data-mongodb:3.x 上可用,https://docs.spring.io/spring-data/mongodb/docs/3.0.x/reference/html/#sharding

Annotation-based Shard Key configuration is available on spring-data-mongodb:3.x, https://docs.spring.io/spring-data/mongodb/docs/3.0.x/reference/html/#sharding

@Document("users")
@Sharded(shardKey = { "country", "userId" }) 
public class User {

    @Id
    Long id;

    @Field("userid")
    String userId;

    String country;
}

截至今天,spring-boot-starter-mongodb 带有 2.x 版本.

As of today spring-boot-starter-mongodb comes with 2.x version though.

这篇关于使用 Spring Data MongoDB 指定分片集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆