弹性数据库工具入门Get started with Elastic Database Tools
适用于:
Azure SQL 数据库
本文档通过帮助你运行示例应用来介绍弹性数据库客户端库的开发人员体验。This document introduces you to the developer experience for the elastic database client library by helping you run a sample app. 此示例应用将创建一个简单的分片应用程序,并探讨 Azure SQL 数据库的弹性数据库工具特性的主要功能。The sample app creates a simple sharded application and explores key capabilities of the Elastic Database Tools feature of Azure SQL Database. 它重点介绍分片映射管理、数据依赖型路由和多分片查询的用例。It focuses on use cases for shard map management, data-dependent routing, and multi-shard querying. 该客户端库可用于 Java 和 .NET。The client library is available for .NET as well as Java.
适用于 Java 的弹性数据库工具Elastic Database Tools for Java
先决条件Prerequisites
- Java 开发人员工具包 (JDK) 1.8 或更高版本A Java Developer Kit (JDK), version 1.8 or later
- MavenMaven
- SQL 数据库或本地 SQL Server 实例SQL Database or a local SQL Server instance
下载并运行示例应用Download and run the sample app
若要生成 JAR 文件并开始使用示例项目,请执行以下操作:To build the JAR files and get started with the sample project, do the following:
克隆包含客户端库以及示例应用的 GitHub 存储库。Clone the GitHub repository containing the client library, along with the sample app.
编辑 ./sample/src/main/resources/resource.properties 文件以设置以下内容:Edit the ./sample/src/main/resources/resource.properties file to set the following:
- TEST_CONN_USERTEST_CONN_USER
- TEST_CONN_PASSWORDTEST_CONN_PASSWORD
- TEST_CONN_SERVER_NAMETEST_CONN_SERVER_NAME
若要生成示例项目,请在 ./sample 目录中运行以下命令:To build the sample project, in the ./sample directory, run the following command:
mvn install
若要启动示例项目,请在 ./sample 目录中运行以下命令:To start the sample project, in the ./sample directory, run the following command:
mvn -q exec:java "-Dexec.mainClass=com.microsoft.azure.elasticdb.samples.elasticscalestarterkit.Program"
若要详细了解客户端库功能,请试验各种选项。To learn more about the client library capabilities, experiment with the various options. 随意浏览代码以了解示例应用的实现方法。Feel free to explore the code to learn about the sample app implementation.
祝贺!Congratulations! 你已成功使用弹性数据库工具在 Azure SQL 数据库上生成并运行了第一个分片应用程序。You have successfully built and run your first sharded application by using Elastic Database Tools on Azure SQL Database. 使用 Visual Studio 或 SQL Server Management Studio 连接到数据库,并快速查看一下样本创建的分片。Use Visual Studio or SQL Server Management Studio to connect to your database and take a quick look at the shards that the sample created. 会看到该示例创建的新示例分片数据库和分片映射管理器数据库。You will notice new sample shard databases and a shard map manager database that the sample has created.
若要将客户端库添加到自己的 Maven 项目,请在 POM 文件中添加以下依赖项:To add the client library to your own Maven project, add the following dependency in your POM file:
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>elastic-db-tools</artifactId>
<version>1.0.0</version>
</dependency>
适用于 .NET 的弹性数据库工具Elastic Database Tools for .NET
先决条件Prerequisites
- 使用 C# 的 Visual Studio 2012 或更高版本。Visual Studio 2012 or later with C#. 可以从 Visual Studio 下载页面下载免费版本。Download a free version at Visual Studio Downloads.
- NuGet 2.7 或更高版本。NuGet 2.7 or later. 若要获取最新版本,请参阅安装 NuGet。To get the latest version, see Installing NuGet.
下载并运行示例应用Download and run the sample app
若要安装该库,请转到 Microsoft.Azure.SqlDatabase.ElasticScale.Client。To install the library, go to Microsoft.Azure.SqlDatabase.ElasticScale.Client. 该库和以下部分中描述的示例应用一起安装。The library is installed with the sample app that's described in the following section.
若要下载并运行该示例,请遵循以下步骤:To download and run the sample, follow these steps:
下载 Elastic DB Tools for Azure SQL - Getting Started sample(Azure SQL 弹性数据库工具 - 入门示例)。Download the Elastic DB Tools for Azure SQL - Getting Started sample. 将示例解压缩到所选位置。Unzip the sample to a location that you choose.
若要创建项目,请从 elastic-db-tools-master 目录打开 ElasticDatabaseTools.sln 解决方案 。To create a project, open the ElasticDatabaseTools.sln solution from the elastic-db-tools-master directory.
将 ElasticScaleStarterKit 项目设置为“启动项目”。Set the ElasticScaleStarterKit project as the Startup Project.
在 ElasticScaleStarterKit 项目中,打开 App.config 文件 。In the ElasticScaleStarterKit project, open the App.config file. 然后遵循该文件中的说明,添加服务器名称和登录信息(用户名和密码)。Then follow the instructions in the file to add your server name and your sign-in information (username and password).
生成并运行应用程序。Build and run the application. 出现提示时,请允许 Visual Studio 还原该解决方案的 NuGet 包。When you are prompted, enable Visual Studio to restore the NuGet packages of the solution. 此操作从 NuGet 下载最新版本的弹性数据库客户端库。This action downloads the latest version of the elastic database client library from NuGet.
若要详细了解客户端库功能,请试验各种选项。To learn more about the client library capabilities, experiment with the various options. 请注意应用程序在控制台输出中执行的步骤,并随意浏览后台代码。Note the steps that the application takes in the console output, and feel free to explore the code behind the scenes.
祝贺!Congratulations! 你已成功使用弹性数据库工具在 SQL 数据库上生成并运行了第一个分片应用程序。You have successfully built and run your first sharded application by using Elastic Database Tools on SQL Database. 使用 Visual Studio 或 SQL Server Management Studio 连接到数据库,并快速查看一下样本创建的分片。Use Visual Studio or SQL Server Management Studio to connect to your database and take a quick look at the shards that the sample created. 会看到该示例创建的新示例分片数据库和分片映射管理器数据库。You will notice new sample shard databases and a shard map manager database that the sample has created.
重要
建议始终使用最新版本的 Management Studio,以便与 Azure 和 SQL 数据库的更新保持同步。We recommend that you always use the latest version of Management Studio so that you stay synchronized with updates to Azure and SQL Database. 更新 SQL Server Management Studio。Update SQL Server Management Studio.
重要的代码示例片段Key pieces of the code sample
管理分片和分片映射:该代码演示如何在“ShardManagementUtils.cs”文件中处理分片、范围和映射。Managing shards and shard maps: The code illustrates how to work with shards, ranges, and mappings in the ShardManagementUtils.cs file. 有关详细信息,请参阅使用分片映射管理器扩大数据库。For more information, see Scale out databases with the shard map manager.
数据依赖型路由:“DataDependentRoutingSample.cs”文件演示了如何向正确的分片路由事务。Data-dependent routing: Routing of transactions to the right shard is shown in the DataDependentRoutingSample.cs file. 有关详细信息,请参阅数据依赖型路由。For more information, see Data-dependent routing.
跨多个分片进行查询:“MultiShardQuerySample.cs”文件演示了如何跨多个分片进行查询。Querying over multiple shards: Querying across shards is illustrated in the MultiShardQuerySample.cs file. 有关详细信息,请参阅多分片查询。For more information, see Multi-shard querying.
添加空分片:“CreateShardSample.cs”文件中的代码以迭代方式添加新的空分片。Adding empty shards: The iterative adding of new empty shards is performed by the code in the CreateShardSample.cs file. 有关详细信息,请参阅使用分片映射管理器扩大数据库。For more information, see Scale out databases with the shard map manager.
其他弹性缩放操作Other elastic scale operations
拆分现有分片:拆分分片的功能由拆分/合并工具提供。Splitting an existing shard: The capability to split shards is provided by the split-merge tool. 有关详细信息,请参阅在横向扩展的云数据库之间移动数据。For more information, see Moving data between scaled-out cloud databases.
合并现有分片:分片合并也是使用拆分/合并工具执行的。Merging existing shards: Shard merges are also performed by using the split-merge tool. 有关详细信息,请参阅在横向扩展的云数据库之间移动数据。For more information, see Moving data between scaled-out cloud databases.
成本Cost
弹性数据库工具库免费。The Elastic Database Tools library is free. 使用弹性数据库工具时,除 Azure 使用费用外,不需支付任何额外费用。When you use Elastic Database Tools, you incur no additional charges beyond the cost of your Azure usage.
例如,示例应用程序会创建新的数据库。For example, the sample application creates new databases. 此功能的费用取决于所选的 SQL 数据库版本以及应用程序的 Azure 使用情况。The cost of this capability depends on the SQL Database edition you choose and the Azure usage of your application.
有关定价信息,请参阅 SQL 数据库定价详细信息。For pricing information, see SQL Database pricing details.
后续步骤Next steps
有关弹性数据库工具的详细信息,请参阅以下文章:For more information about Elastic Database Tools, see the following articles:
- 代码示例:Code samples:
- 弹性数据库工具(.NET、Java)Elastic Database Tools (.NET, Java)
- Elastic Database Tools for Azure SQL - Entity Framework Integration(Azure SQL 的弹性数据库工具 - 实体框架集成)Elastic Database Tools for Azure SQL - Entity Framework Integration
- 脚本中心上的分片弹性Shard Elasticity on Script Center
- 博客:弹性缩放公告Blog: Elastic Scale announcement
- 第 9 频道:弹性缩放概述视频Channel 9: Elastic Scale overview video
- 讨论论坛:Azure SQL 数据库论坛Discussion forum: Azure SQL Database forum
- 衡量性能:分片映射管理器的性能计数器To measure performance: Performance counters for shard map manager