split

On this page本页内容

Definition定义

split

Changed in version 4.2.在版本4.2中更改

Splits a chunk in a sharded cluster into two chunks. 分片集群中的一个分割为两个块。Starting in MongoDB 4.2, shards manage and split chunks based on the chunk size statistics they maintain.从MongoDB 4.2开始,分片根据它们维护的块大小统计数据来管理和分割块。

For exceptional circumstances the split command does allow administrators to manually create splits. 在特殊情况下,split命令允许管理员手动创建拆分。See Split Chunks in a Sharded Cluster for information on these circumstances, and on the MongoDB shell commands that wrap split.有关这些情况以及包装splitMongoDB shell命令的信息,请参阅拆分集群中的拆分块

The split command must be run in the admin database and uses the following form:split命令必须在admin数据库中运行,并使用以下形式:

db.adminCommand( { split: <database>.<collection>,
                   <find|middle|bounds> } )

The split command takes a document with the following fields:split命令获取包含以下字段的文档:

Field字段Type类型Description描述
splitstringThe name of the collection where the chunk exists. 所在集合的名称。Specify the collection's full namespace, including the database name. 指定集合的完整命名空间,包括数据库名称。
finddocument

An query statement that specifies an equality match on the shard key. 一个查询语句,用于指定分片键上的相等匹配。The match selects the chunk that contains the specified document. 匹配将选择包含指定文档的块。You must specify only one of the following: find, bounds, or middle.您只能指定以下选项之一:findboundmiddle

You cannot use the find option on an empty collection.不能对空集合使用find选项。

boundsarray

New in version 2.4: The bounds of a chunk to split. 2.4版中的新功能:要分割的块的边界。bounds applies to chunks in collections partitioned using a hashed shard key. bounds适用于使用哈希分片键分区的集合中的块。The parameter's array must consist of two documents specifying the lower and upper shard-key values of the chunk. 参数的数组必须由两个文档组成,这两个文档指定块的下分片键值和上分片键值。The values must match the minimum and maximum values of an existing chunk. 这些值必须与现有块的最小值和最大值相匹配。Specify only one of the following: find, bounds, or middle.仅指定以下选项之一:findboundsmiddle

You cannot use the bounds option on an empty collection.不能对空集合使用bounds选项。

middledocumentThe document to use as the split point to create two chunks. 要用作拆分点以创建两个块的文档。split requires one of the following options: find, bounds, or middle. 需要以下选项之一:findboundmiddle

Considerations注意事项

When used with either the find or the bounds option, the split command splits the chunk along the median. 当与findbound选项一起使用时,split命令会沿中间值分割块。As such, the command cannot use the find or the bounds option to split an empty chunk since an empty chunk has no median.因此,该命令不能使用findbound选项拆分空块,因为空块没有中间值。

To create splits in empty chunks, use either the middle option with the split command or use the sh.splitAt() command.要在空块中创建拆分,请在split命令中使用middle选项或使用sh.splitAt()命令。

Command Formats命令格式

To create a chunk split, connect to a mongos instance, and issue the following command to the admin database:要创建区块分割,请连接到mongos实例,并向admin数据库发出以下命令:

db.adminCommand( { split: <database>.<collection>,
                   find: <document> } )

Or:

db.adminCommand( { split: <database>.<collection>,
                   middle: <document> } )

Or:

db.adminCommand( { split: <database>.<collection>,
                   bounds: [ <lower>, <upper> ] } )

To create a split for a collection that uses a hashed shard key, use the bounds parameter. 要为使用哈希分片键的集合创建拆分,请使用bounds参数。Do not use the middle parameter for this purpose.不要为此使用middle参数。

Warning警告

Be careful when splitting data in a sharded collection to create new chunks. 拆分分片集合中的数据以创建新块时要小心。When you shard a collection that has existing data, MongoDB automatically creates chunks to evenly distribute the collection. 当您对具有现有数据的集合进行分片时,MongoDB会自动创建块以均匀分布集合。To split data effectively in a sharded cluster you must consider the number of documents in a chunk and the average document size to create a uniform chunk size. 为了在分片集群中有效地分割数据,必须考虑块中的文档数量和平均文档大小,以创建一致性的块大小。When chunks have irregular sizes, shards may have an equal number of chunks but have very different data sizes. 当块的大小不规则时,分片可能具有相同数量的块,但具有非常不同的数据大小。Avoid creating splits that lead to a collection with differently sized chunks.避免创建导致具有不同大小块的集合的拆分。

Examples示例

The following sections provide examples of the split command.以下部分提供了split命令的示例。

Split a Chunk in Half将块一分为二

db.adminCommand( { split : "test.people", find : { _id : 99 } } )

The split command identifies the chunk in the people collection of the test database, that holds documents that match { _id : 99 }. split命令标识test数据库的people集合中的块,其中包含匹配{ _id : 99 }的文档。split does not require that a match exist, in order to identify the appropriate chunk. 不要求存在匹配以识别适当的块。Then the command splits it into two chunks of equal size.然后该命令将其拆分为两个大小相等的块。

Note注意

Define an Arbitrary Split Point定义任意拆分点

To define an arbitrary split point, use the following form:要定义任意拆分点,请使用以下形式:

db.adminCommand( { split : "test.people", middle : { _id : 99 } } )

The split command identifies the chunk in the people collection of the test database, that would hold documents matching the query { _id : 99 }. split命令标识test数据库的people集合中的块,该块将保存与查询{ _id : 99 }匹配的文档。split does not require that a match exist, in order to identify the appropriate chunk. 不要求存在匹配以识别适当的块。Then the command splits it into two chunks, with the matching document as the lower bound of one of the split chunks.然后,命令将其拆分为两个块,匹配的文档作为其中一个拆分块的下限。

This form is typically used when pre-splitting data in a collection.此表单通常在预拆分集合中的数据时使用。

Split a Chunk Using Values of a Hashed Shard Key使用哈希分片键的值分割区块

This example uses the hashed shard key userid in a people collection of a test database. 此示例使用测试数据库的人员集合中的哈希分片键useridThe following command uses an array holding two single-field documents to represent the minimum and maximum values of the hashed shard key to split the chunk:以下命令使用包含两个单字段文档的数组来表示哈希分片键的最小值和最大值,以分割块:

db.adminCommand( { split: "test.people",
                  bounds : [ { userid: NumberLong("-5838464104018346494") },
                             { userid: NumberLong("-5557153028469814163") }
             ] } )
Note注意

MongoDB uses the 64-bit NumberLong type to represent the hashed value.MongoDB使用64位NumberLong类型表示哈希值。

Use sh.status() to see the existing bounds of the shard keys.使用sh.status()查看分片键的现有边界。

Metadata Lock Error元数据锁定错误

If another process, such as a balancer process, changes metadata while split is running, you may see a metadata lock error.如果另一个进程(如平衡器进程)在split运行时更改元数据,则可能会看到metadata lock error

errmsg: "The collection's metadata lock is already taken."

This message indicates that the split has failed with no side effects. 此消息表示拆分失败,没有副作用。Retry the split command.重试split命令。

←  shardingStatesplitVector →