弹性数据库工具入门Get started with Elastic Database Tools

本文档通过帮助你运行示例应用来介绍弹性数据库客户端库的开发人员体验。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
  • Azure 或本地 SQL Server 实例中的 SQL 数据库服务器A SQL Database server in Azure 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:

  1. 克隆包含客户端库以及示例应用的 GitHub 存储库Clone the GitHub repository containing the client library, along with the sample app.

  2. 编辑 ./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
  3. 若要生成示例项目,请在 ./sample 目录中运行以下命令:To build the sample project, in the ./sample directory, run the following command:

    mvn install
    
  4. 若要启动示例项目,请在 ./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"
    
  5. 若要详细了解客户端库功能,请试验各种选项。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.

    Progress-java

祝贺!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 连接到 SQL 数据库,并快速查看一下样本创建的分片。Use Visual Studio or SQL Server Management Studio to connect to your SQL 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

下载并运行示例应用Download and run the sample app

若要安装该库,请转到 Microsoft.Azure.SqlDatabase.ElasticScale.ClientTo 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:

  1. 从 MSDN 下载 Elastic DB Tools for Azure SQL - Getting Started sample(Azure SQL 弹性数据库工具 - 入门示例)。Download the Elastic DB Tools for Azure SQL - Getting Started sample from MSDN. 将示例解压缩到所选位置。Unzip the sample to a location that you choose.

  2. 若要创建项目,请从 C# 目录打开 ElasticScaleStarterKit.sln 解决方案。To create a project, open the ElasticScaleStarterKit.sln solution from the C# directory.

  3. 在示例项目的解决方案中打开 app.config 文件。In the solution for the sample project, open the app.config file. 然后遵循该文件中的说明,添加 Azure SQL 数据库服务器名称和登录信息(用户名和密码)。Then follow the instructions in the file to add your Azure SQL Database server name and your sign-in information (username and password).

  4. 生成并运行应用程序。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.

  5. 若要详细了解客户端库功能,请试验各种选项。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 连接到 SQL 数据库,并快速查看一下样本创建的分片。Use Visual Studio or SQL Server Management Studio to connect to your SQL 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.

Important

建议始终使用最新版本的 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 StudioUpdate 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

成本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: