Azure Cosmos DB SQL API:Spring Data Azure Cosmos DB v3 示例Azure Cosmos DB SQL API: Spring Data Azure Cosmos DB v3 examples

适用于: SQL API

重要

这些发行说明适用于 Spring Data Azure Cosmos DB 的版本 3。These release notes are for version 3 of Spring Data Azure Cosmos DB. 可以在此处找到版本 2 的发行说明You can find release notes for version 2 here.

Spring Data Azure Cosmos DB 仅支持 SQL API。Spring Data Azure Cosmos DB supports only the SQL API.

请参阅以下文章,了解其他 Azure Cosmos DB API 上的 Spring Data:See these articles for information about Spring Data on other Azure Cosmos DB APIs:

重要

如果没有 Azure 试用版订阅,请在开始前创建一个试用版订阅If you don't have an Azure trail subscription, create a trial subscription before you begin.

可以创建一个 Azure Cosmos DB 免费层帐户,你将在帐户中获得前 400 RU/s 的免费吞吐量和 5 GB 的免费存储。You can create an Azure Cosmos DB free tier account, with the first 400 RU/s and 5 GB of storage for free. 还可以使用 URI 为 https://localhost:8081Azure Cosmos DB 模拟器You can also use the Azure Cosmos DB Emulator with a URI of https://localhost:8081. 有关在模拟器中使用的密钥,请参阅对请求进行身份验证For the key to use with the emulator, see Authenticating requests.

azure-spring-data-cosmos-java-sql-api-samples GitHub 存储库中包含可对 Azure Cosmos DB 资源执行 CRUD 操作和其他常见操作的最新示例应用程序。The latest sample applications that perform CRUD operations and other common operations on Azure Cosmos DB resources are included in the azure-spring-data-cosmos-java-sql-api-samples GitHub repository. 本文将提供:This article provides:

  • 每个示例 Spring Data Azure Cosmos DB 项目文件中各项任务的链接。Links to the tasks in each of the example Spring Data Azure Cosmos DB project files.
  • 指向相关的 API 参考内容的链接。Links to the related API reference content.

先决条件Prerequisites

需要以下条件才能运行此示例应用程序:You need the following to run this sample application:

  • Java 开发工具包 8Java Development Kit 8
  • Spring Data Azure Cosmos DB v3Spring Data Azure Cosmos DB v3

可以选择使用 Maven 获取最新的 Spring Data Azure Cosmos DB v3 二进制文件,以便在项目中使用。You can optionally use Maven to get the latest Spring Data Azure Cosmos DB v3 binaries for use in your project. Maven 会自动添加任何必需的依赖项。Maven automatically adds any necessary dependencies. 否则,可以直接下载 pom.xml 文件中列出的依赖项并将它们添加到生成路径。Otherwise, you can directly download the dependencies listed in the pom.xml file and add them to your build path.

<dependency>
    <groupId>com.azure</groupId>
    <artifactId>azure-spring-data-cosmos</artifactId>
    <version>LATEST</version>
</dependency>

运行示例应用程序Running the sample applications

克隆示例存储库:Clone the sample repo:

$ git clone https://github.com/Azure-Samples/azure-spring-data-cosmos-java-sql-api-samples

$ cd azure-spring-data-cosmos-java-sql-api-samples

可以使用 IDE(Eclipse、IntelliJ 或 VSCODE)或使用 Maven 从命令行运行示例。You can run the samples using either an IDE (Eclipse, IntelliJ, or VSCODE) or from the command line using Maven.

必须在 application.properties 中设置这些环境变量,In application.properties these environment variables must be set

cosmos.uri=${ACCOUNT_HOST}
cosmos.key=${ACCOUNT_KEY}
cosmos.secondaryKey=${SECONDARY_ACCOUNT_KEY}

dynamic.collection.name=spel-property-collection
# Populate query metrics
cosmos.queryMetricsEnabled=true

才能授予示例对你的帐户、数据库和容器进行读/写访问的权限。in order to give the samples read/write access to your account, databases and containers.

你的 IDE 可能会提供执行 Spring Data 示例代码的功能。Your IDE may provide the ability to execute the Spring Data sample code. 否则,可以使用以下终端命令来执行该示例:Otherwise you may use the following terminal command to execute the sample:

mvn spring-boot:run

文档 CRUD 示例Document CRUD examples

samples 文件演示如何执行以下任务。The samples file shows how to perform the following tasks. 若要在运行以下示例之前了解 Azure Cosmos 文档,请参阅概念性文章:使用数据库、容器和项To learn about Azure Cosmos documents before running the following samples, see Working with databases, containers, and items conceptual article.

任务Task API 参考API reference
创建文档Create a document CosmosRepository.saveCosmosRepository.save
按 ID 读取文档Read a document by ID CosmosRepository.derivedQueryMethodCosmosRepository.derivedQueryMethod
删除所有文档Delete all documents CosmosRepository.deleteAllCosmosRepository.deleteAll

派生查询方法示例Derived query method examples

samples 文件演示如何执行以下任务。The samples file shows how to perform the following tasks. 若要在运行以下示例之前了解 Azure Cosmos DB 查询,可参阅 Baeldung 在 Spring 中的派生查询方法一文。To learn about Azure Cosmos DB queries before running the following samples, you may find it helpful to read Baeldung's Derived Query Methods in Spring article.

| 查询文档 | CosmosRepository.derivedQueryMethod || Query for documents | CosmosRepository.derivedQueryMethod |

自定义查询示例Custom query examples

samples 文件演示如何使用 SQL 查询语法执行以下任务。The samples file shows how to do the following tasks using the SQL query grammar. 若要在运行以下示例之前了解 Azure Cosmos DB 中的 SQL 查询引用,请参阅 Azure Cosmos DB 的 SQL 查询示例To learn about the SQL query reference in Azure Cosmos DB before you run the following samples, see SQL query examples for Azure Cosmos DB.

任务Task API 参考API reference
查询所有文档Query for all documents @Query 注释@Query annotation
使用 == 查询等式Query for equality using == @Query 注释@Query annotation
使用 != 和 NOT 查询不等式Query for inequality using != and NOT @Query 注释@Query annotation
使用 >、<、>=、<= 等范围运算符进行查询Query using range operators like >, <, >=, <= @Query 注释@Query annotation
使用范围运算符对字符串进行查询Query using range operators against strings @Query 注释@Query annotation
使用 ORDER BY 进行查询Query with ORDER BY @Query 注释@Query annotation
使用 DISTINCT 查询Query with DISTINCT @Query 注释@Query annotation
使用聚合函数进行查询Query with aggregate functions @Query 注释@Query annotation
使用子文档Work with subdocuments @Query 注释@Query annotation
使用文档内联接进行查询Query with intra-document Joins @Query 注释@Query annotation
使用字符串、数学和数组运算符进行查询Query with string, math, and array operators @Query 注释@Query annotation