使用弹性数据库工具添加分片Adding a shard using Elastic Database tools

适用于:是Azure SQL 数据库 APPLIES TO: yesAzure SQL Database

添加新范围或键的分片To add a shard for a new range or key

对于已经存在的分片映射,应用程序通常仅需要添加新分片,以处理预期的新键或键范围数据。Applications often need to add new shards to handle data that is expected from new keys or key ranges, for a shard map that already exists. 例如,由租户 ID 分片的应用程序可能需要为新的租户预配新分片,或者在每个新的月份开始之前,每月分片的数据可能需要预配新分片。For example, an application sharded by Tenant ID may need to provision a new shard for a new tenant, or data sharded monthly may need a new shard provisioned before the start of each new month.

如果新的键值范围还不是现有映射的组成部分,则添加新分片以及将新的键或范围关联到该分片很简单。If the new range of key values is not already part of an existing mapping, it is simple to add the new shard and associate the new key or range to that shard.

示例:将分片及其范围添加到现有的分片映射Example: adding a shard and its range to an existing shard map

本示例使用 TryGetShard(Java.NET)、CreateShard(Java.NET)、CreateRangeMapping(Java.NET)方法,并创建 ShardLocation(Java.NET)类的实例。This sample uses the TryGetShard (Java, .NET) the CreateShard (Java, .NET), CreateRangeMapping (Java, .NET methods, and creates an instance of the ShardLocation (Java, .NET) class. 在以下示例中,创建了一个名为 sample_shard_2 的数据库以及其中所有必要的架构对象,用于保存范围 [300, 400)。In the sample below, a database named sample_shard_2 and all necessary schema objects inside of it have been created to hold range [300, 400).

// sm is a RangeShardMap object.
// Add a new shard to hold the range being added.
Shard shard2 = null;

if (!sm.TryGetShard(new ShardLocation(shardServer, "sample_shard_2"),out shard2))
{
    shard2 = sm.CreateShard(new ShardLocation(shardServer, "sample_shard_2"));  
}

// Create the mapping and associate it with the new shard
sm.CreateRangeMapping(new RangeMappingCreationInfo<long>
                            (new Range<long>(300, 400), shard2, MappingStatus.Online));

对于 .NET 版本,也可以使用 Powershell 作为替代方法来创建新的分片映射管理器。For the .NET version, you can also use PowerShell as an alternative to create a new Shard Map Manager. 此处提供了一个示例。An example is available here.

为现有范围的空部分添加分片To add a shard for an empty part of an existing range

在某些情况下,用户可能已将某个范围映射到了某个分片,并在该分片中填充了部分数据,但是,现在用户希望将以后的数据定向到其他的分片。In some circumstances, you may have already mapped a range to a shard and partially filled it with data, but you now want upcoming data to be directed to a different shard. 例如,用户以前按日期范围进行分片,并且已在某个分片中分配了 50 天的数据,但从第 24 天开始,用户希望以后的数据都驻留在其他分片中。For example, you shard by day range and have already allocated 50 days to a shard, but on day 24, you want future data to land in a different shard. 弹性数据库拆分/合并工具可以执行此操作,但是,如果不需要数据移动(例如,[25, 50) 天范围的数据 - 即,从第 25 天(含)到第 50 天(不含)的数据尚不存在),完全可以直接使用分片映射管理 API 执行此操作。The elastic database split-merge tool can perform this operation, but if data movement is not necessary (for example, data for the range of days [25, 50), that is, day 25 inclusive to 50 exclusive, does not yet exist) you can perform this entirely using the Shard Map Management APIs directly.

示例:拆分范围并将空部分分配到新增的分片Example: splitting a range and assigning the empty portion to a newly added shard

已创建名为“sample_shard_2”的数据库以及其中所有必要的架构对象。A database named "sample_shard_2" and all necessary schema objects inside of it have been created.

// sm is a RangeShardMap object.
// Add a new shard to hold the range we will move
Shard shard2 = null;

if (!sm.TryGetShard(new ShardLocation(shardServer, "sample_shard_2"),out shard2))
{
    shard2 = sm.CreateShard(new ShardLocation(shardServer,"sample_shard_2"));  
}

// Split the Range holding Key 25
sm.SplitMapping(sm.GetMappingForKey(25), 25);

// Map new range holding [25-50) to different shard:
// first take existing mapping offline
sm.MarkMappingOffline(sm.GetMappingForKey(25));

// now map while offline to a different shard and take online
RangeMappingUpdate upd = new RangeMappingUpdate();
upd.Shard = shard2;
sm.MarkMappingOnline(sm.UpdateMapping(sm.GetMappingForKey(25), upd));

重要说明:仅当确定所更新映射的范围为空时,才使用此方法。Important: Use this technique only if you are certain that the range for the updated mapping is empty. 上述方法不会在数据中检查要移动的范围,因此最好在代码中包含检查操作。The preceding methods do not check data for the range being moved, so it is best to include checks in your code. 如果要移动的范围中存在行,则实际的数据分布将与更新后的分片映射不匹配。If rows exist in the range being moved, the actual data distribution will not match the updated shard map. 在这种情况下,请改用拆分/合并工具来执行操作。Use the split-merge tool to perform the operation instead in these cases.

其他资源Additional resources

尚未使用弹性数据库工具?Not using elastic database tools yet? 请查看入门指南Check out our Getting Started Guide.