azure api 管理_Azure Cosmos DB和MongoDB API入门

azure api 管理

In the previous article on Azure Cosmos DB, we reviewed NoSQL concepts and how to integrate with the Microsoft Azure platform-as-a-service model using the API. After working as a database engineer for over a decade, I feel that this technology is the future for many organizations for various reasons. I had that in mind as I wrote this article which will provide basic information and help you to get started with MongoDB API integration. The MongoDB API works with BSON documents. BSON is MongoDB’s binary-encoded-version of JSON, and it extends the JSON model with additional language feature support. It’s a great effort from Microsoft to build the enterprise solutions which provide flexibility in manage distributed data along with scalable option.

在上一篇有关Azure Cosmos DB的文章中,我们回顾了NoSQL概念以及如何使用APIMicrosoft Azure平台即服务模型集成 。 在担任数据库工程师十多年之后,由于各种原因,我认为该技术是许多组织的未来。 在撰写本文时,我已经牢记这一点,该文章将提供基本信息并帮助您开始使用MongoDB API集成。 MongoDB API可与BSON文档一起使用。 BSON是MongoDB的JSON二进制编码版本,它通过附加的语言功能支持扩展了JSON模型。 Microsoft付出了巨大的努力来构建企业解决方案,以提供可灵活管理分布式数据以及可伸缩选项的灵活性。

A quick overview

快速概述

  1. Azure Cosmos DB is a derived version of Azure DocumentDB

    Azure Cosmos DB是Azure DocumentDB的派生版本
  2. Its launched with Microsoft build in May 2017

    它于2017年5月与Microsoft构建一起推出
  3. It’s a globally distributed multi-model database PaaS model

    这是一个全球分布的多模型数据库PaaS模型
  4. The data is globally distributed with automatic index tuning is enabled by default

    数据是全局分布的,默认情况下启用了自动索引调整
    1. Globally distributed

      全球分布
    2. Automatic sharding 自动分片
    3. Availability and latency guaranteed

      保证可用性和延迟
    4. Automatic indexing tuning

      自动索引调整
    5. Encryption

      加密
    6. Backups

      后备

Let’s deep dive into the this Microsoft Azure PaaS (platform-as-a-service ) offering and see how it can be beneficial for us. This article covers the following topics:

让我们深入研究此Microsoft Azure PaaS(平台即服务)产品,看看它如何对我们有利。 本文涵盖以下主题:

  1. Introduction

    介绍
  2. Setting up

    配置
  3. Creating a database and collection

    创建数据库和集合
  4. MongoDB query samples

    MongoDB查询样本
  5. And more…

    和更多…

快速概述 (Quick overview)

Azure Cosmos DB is a globally distributed, replicated, multi-model database service that offers 99.99% availability within the region. It also offers rich querying over schema-free data, helps to deliver configurable and reliable performance, and enables rapid development.

Azure Cosmos DB是一项全球分布的,复制的多模型数据库服务,可在区域内提供99.99%的可用性。 它还提供了对无模式数据的丰富查询,有助于提供可配置和可靠的性能,并实现快速开发。

Azure Cosmos DB is a right choice for many or new web, mobile, gaming, and IoT applications where automatic scaling, predictable performance, high availability, high throughput, rich indexing, and provide a platform for rapid application building process and rich capability to query over schema-free data are the key requirements.

Azure Cosmos DB是许多或新的Web,移动,游戏和IoT应用程序的正确选择,这些应用程序具有自动缩放,可预测的性能,高可用性,高吞吐量,丰富的索引,并为快速的应用程序构建过程和丰富的查询功能提供了平台超过无模式数据是关键要求。

优点 (Advantages)

Azure Cosmos DB is a complete service and ready to use: It gives you a complete product and can be automatically replicated in data centers worldwide using local and global distribution. Partitioning provides elastic scaling and its automatic, the resource partition is first spread across the regions using a local distribution layer and the resource partitions are replicated across global regions using global distribution.

Azure Cosmos DB是一项完整的服务,可以立即使用:它为您提供了完整的产品,可以使用本地和全球分布在全球的数据中心中自动复制。 分区提供了弹性伸缩及其自动扩展功能,首先使用本地分布层将资源分区分布在各个区域中,然后使用全局分布在全局区域中复制资源分区。

入门 (Getting started )

Let’s pop into the demo, and create a new account.

让我们进入演示,并创建一个新帐户。

This section details the step required to create and configure Azure Cosmos DB.

本节详细介绍创建和配置Azure Cosmos DB所需的步骤。

  1. Azure portal Azure门户
  2. New button and type the search string 新建”按钮,然后键入搜索字符串Cosmos to lookup for Azure Cosmos DBCosmos以查找Azure Cosmos DB



  3. Four programming models are supported:

    支持四种编程模型:

    • Key-Value

      核心价值
    • Column family

      列族
    • Documents

      文件资料
    • Graph

      图形


    In this section, we’ll walk-through the steps to integrate MongoDB API. Let us choose MongoDB API and type in other values of the form. The following table takes you through the required information to fill out the values needed for the MongoDB API integration

    在本部分中,我们将逐步介绍集成MongoDB API的步骤。 让我们选择MongoDB API,然后输入表单的其他值。 下表引导您了解必需的信息,以填写MongoDB API集成所需的值

    Setting

    Suggested value

    Description

    ID

    Unique value

    A unique name you choose to identify the new account. documents.azure.com is appended to the ID you provide to create your URI, so use a unique but identifiable ID. The ID may contain only lowercase letters, numbers, and the ‘-‘ character, and must be between 3 and 50 characters.

    API

    MongoDB

    The API determines the type of account to create. Five APIs exist to suit the needs of your application: SQL (document database), Gremlin (graph database), MongoDB (document database), Azure Table, and Cassandra, each which currently require a separate account. 

    Select MongoDB because in this quick start you are creating a document database that is queryable using MongoDB.

    Learn more about the MongoDB API

    Subscription

    Your subscription

    The Azure subscription that you want to use for the new account

    Resource Group

    The same value as ID

    The new resource group name for your account. For simplicity, you can use the same name as your ID.

    Location

    The region closest to your users

    The geographic location in which to host your Azure Cosmos DB account. Choose the location closest to your users to give them the fastest access to the data.

    设置

    建议值

    描述

    ID

    独特价值

    您选择用来标识新帐户的唯一名称。 documents.azure.com会附加到您提供的用于创建URI的ID上,因此请使用唯一但可识别的ID。 ID只能包含小写字母,数字和'-'字符,并且必须在3到50个字符之间。

    API

    MongoDB

    API确定要创建的帐户类型。 目前有五个API可以满足您的应用程序的需求:SQL(文档数据库),Gremlin(图形数据库),MongoDB(文档数据库),Azure Table和Cassandra,目前每个API都需要一个单独的帐户。

    选择MongoDB,因为在此快速入门中,您正在创建可使用MongoDB查询的文档数据库。

    了解有关MongoDB API的更多信息

    订阅

    您的订阅

    您要用于新帐户的Azure订阅

    资源组

    与ID相同的值

    您帐户的新资源组名称。 为简单起见,您可以使用与ID相同的名称。

    位置

    离您的用户最近的区域

    托管Azure Cosmos DB帐户的地理位置。 选择离您的用户最近的位置,以使他们能够最快地访问数据。









  4. The database is first stored locally. This is our local distribution level. In this case, it happens to be in the region of US East. The databases are then replicated to other regions. This always ensures that the database is available and easily accessible. The extensible APIs that you can use include .NET, .NET Core, Node.js, Java, Python, and MongoDB. If you’re using .NET, you can use DocumentDB API, MongoDB API, Graph API, or Table API. Java and Node.js will only work with the Document DB APIs, MongoDB APIs, and Graph APIs. Python will only work with the DocumentDB API, and Gremlin, only the Graph API.

    该数据库首先存储在本地。 这是我们的本地分布级别。 在这种情况下,它恰好在美国东部地区。 然后将数据库复制到其他区域。 这始终确保数据库可用并且易于访问。 您可以使用的可扩展API包括.NET,.NET Core,Node.js,Java,Python和MongoDB。 如果使用的是.NET,则可以使用DocumentDB API,MongoDB API,Graph API或Table API。 Java和Node.js仅适用于Document DB API,MongoDB API和Graph API。 Python仅适用于DocumentDB API,Gremlin仅适用于Graph API。





  5. In this case, the Geo-Redundancy and Multi-region writes are disabled. For the demo, I will be going with the default setting. But, let us take a moment to discuss partitioning, which provides elastic scaling for Azure Cosmos DB. First, we have our local distribution layer. Here, the resource partitions are spread across the region. And then we have a global distribution layer, now our resource partitions are replicated. And the best part of this is, it’s all handled automatically

    在这种情况下,将禁用“地理冗余”和“多区域”写入。 对于演示,我将使用默认设置。 但是,让我们花一点时间讨论分区,该分区为Azure Cosmos DB提供了弹性扩展。 首先,我们有本地分布层。 在这里,资源分区分布在整个区域。 然后我们有了一个全局分布层,现在我们的资源分区已被复制。 最好的部分是,它们都是自动处理的

    It uses five different phases of consistency levels: bounded staleness, strong, session, eventual, and consistent-prefix and it provides a guaranteed latency of less than 10 milliseconds for data read and less than 15 milliseconds for data write operations.

    它使用一致性级别的五个不同阶段:有限制的陈旧性,强性,会话性,最终性和一致性前缀,并且它为数据读取提供了小于10毫秒的保证延迟,对于数据写入操作提供了小于15毫秒的延迟。

  6. A Request unit (RU) is the measure of the throughput. It also ensures predictable performance no matter how demanding the workload the application places on the database. The operation in Azure Cosmos DB has a deterministic RU value and it is directly based on the throughput required to complete the process. You can compare throughput to throughput currency as it allows you to specify the throughput level that is required to complete the operation. This way you don’t need to really worry about provisioning any hardware. This gives us an insight into selecting and defining the required RU. Every operation such as reads, writes, SQL Query and Stored Procedure execution have a deterministic RU. For example, the write operation will always cost more than reads, and running a query on data over an indexed data set will always cost less than querying over data that are not indexed. For every operation, Azure Cosmos DB has a way to compute and let us know exactly how many RUs that the request got consumed and was charged. RUs are really a very simple way to manage and ensure predictable performance.

    请求单位(RU)是吞吐量的度量。 无论应用程序对数据库施加多大的工作量,它也可确保可预测的性能。 Azure Cosmos DB中的操作具有确定的RU值,它直接基于完成该过程所需的吞吐量。 您可以将吞吐量与吞吐量货币进行比较,因为它允许您指定完成操作所需的吞吐量级别。 这样,您就不必担心配置任何硬件。 这使我们能够洞悉选择和定义所需的RU。 每个操作(例如读取,写入,SQL查询和存储过程执行)都具有确定的RU。 例如,写操作将始终比读取花费更多,并且对索引数据集上的数据运行查询将始终比对未索引数据的查询花费更少。 对于每个操作,Azure Cosmos DB都有一种计算方法,可以让我们确切知道该请求已被消耗并收取了多少RU。 RU实际上是一种非常简单的方法来管理和确保可预测的性能。


  7. Notifications to monitor the deployment process. When the deployment is complete, open the new account from the All Resources tile. 通知”以监视部署过程。 部署完成后,从“所有资源”图块中打开新帐户。

MongoDB入门 (Get Started with MongoDB)

Lets us go ahead and create some data, and the first step is to create a container for the data. Each of the APIs has a different term for the container. Microsoft Azure Cosmos DB MongoDB API comes with in-built comprehensive support for MongoDB query language constructs. The following are the few samples to understand how to use MongoDB query. You can find a more detailed list in the here

让我们继续创建一些数据,第一步是为数据创建一个容器。 每个API都有不同的容器术语。 Microsoft Azure Cosmos DB MongoDB API附带了对MongoDB查询语言构造的内置全面支持。 以下是一些了解如何使用MongoDB查询的示例。 您可以在此处找到更详细的列表

Let me expand the collection and click on documents. In the portal, it display as documents in the collection. The following sample document created is used for the demonstration

让我扩展集合并单击文档。 在门户中,它显示为集合中的文档。 创建的以下样本文档用于演示

To create a new document, Click New Document, type in the document with key-and-value pair data and click Save. Repeat the steps to enter the other pair of document in the fruits collection.

要创建新文档,请单击“ 新建文档” ,键入包含键值对数据的文档,然后单击“保存”。 重复这些步骤,以在Fruits集合中输入另一对文档。

{
 “id”: “Strawberry”,
 “Qty”: “13”,
}

{
“ id”:“草莓”,
“数量”:“ 13”,
}

In the next query windows, type in {}, and click Execute Selection to get all the documents of the collection

在接下来的查询窗口中,键入{},然后单击“执行选择”以获取集合的所有文档。

The following example selects the specific “Apples” document from the fruits collection

下面的示例从水果集合中选择特定的“苹果”文档

{“id”:”Apples”}

{“ id”:“苹果”}

The following example uses $in operator to compare more than one value from the fruits collection. In this case, the documents with the value Apples and Oranges are listed.

下面的示例使用$ in运算符比较水果集合中的多个值。 在这种情况下,将列出值苹果和橘子的文档。

{“id”:{$in: [“Apples”,”Oranges”]}}

{“ id”:{$ in:[“ Apples”,“ Oranges”]}}

The following example $or is used to retrieve the documents that satisfies the logical condition using $lt operator and id value which is equal to bananas are listed.

以下示例$ or用于使用$ lt运算符检索满足逻辑条件的文档,并列出等于香蕉的id值。

{ $or: [ {qty: { $lt:15 } },{“id”:”bananas”} ] }

{$ or:[{qty:{$ lt:15}},{“ id”:“香蕉”}]}

The following example is a regular expression used to retrieve the documents that start with word App.

以下示例是一个正则表达式,用于检索以App开头的文档。

{ “id”: {$regex: “^App”}}

{“ id”:{$ regex:“ ^ App”}}

The following example is a regular expression used to retrieve the documents that end with word nas.

以下示例是一个正则表达式,用于检索以单词nas结尾的文档。

{ “id”: {$regex: “nas$”}}

{“ id”:{$ regex:“ nas $”}}

The following example shows how to update the document. To update the document, use the Edit Filter option and type in the condition clause to retrieve the specific document and modify the fields and click Update button to save the changes.

以下示例显示了如何更新文档。 要更新文档,请使用“ 编辑过滤器”选项,然后键入condition子句以检索特定文档并修改字段,然后单击“更新”按钮以保存更改。

The following example shows how to delete the document. To delete the document, use the Edit Filter option and type in the condition clause to retrieve the specific document and click Delete to reflect the changes.

以下示例显示了如何删除文档。 要删除文档,请使用“ 编辑过滤器”选项,然后键入condition子句以检索特定文档,然后单击“ 删除”以反映所做的更改。

That’s all for now…

目前为止就这样了…

摘要 (Summary)

Thus far, we’ve taken a walkthrough of MongoDB API integration with Azure Cosmos DB. We started with introduction, definition, and configuration. Then we explored the multi API, multi-model capabilities, MongoDB APIs integration. In my next article, I will discuss more on MongoDB migration and other details. I hope you enjoyed reading this article. Feel free to provide feedback or questions in the comments below

到目前为止,我们已经完成了MongoDB API与Azure Cosmos DB集成的演练。 我们从介绍,定义和配置开始。 然后,我们探索了多API,多模型功能,MongoDB API集成。 在下一篇文章中,我将讨论有关MongoDB迁移的更多信息和其他细节。 希望您喜欢阅读本文。 随时在下面的评论中提供反馈或问题

翻译自: https://www.sqlshack.com/getting-started-with-azure-cosmos-db-and-mongodb-api/

azure api 管理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值