azure云数据库

Our testing or development may call for dynamic creation on the database level for Azure Cosmos DB rather than the account level. As we’ve seen with dynamically working with a Cosmos database account using PowerShell, we can create, remove, and obtain properties of the account. Identically, we can do this on the database level as well and we may use this in testing if we need the same Cosmos database account for other testing purposes. Development situations may also involve use cases where we want to test a concept and dynamically create a database within our Cosmos database account. In this tip, we’ll look at working with our Azure Cosmos database account on the database object level where we do nothing to manipulate the account itself, only add databases to the account once it’s been setup.

我们的测试或开发可能需要在Azure Cosmos DB的数据库级别而不是帐户级别上动态创建。 如我们所见,使用PowerShell动态处理Cosmos数据库帐户时,我们可以创建,删除和获取帐户属性。 同样,我们也可以在数据库级别执行此操作,如果我们需要使用相同的Cosmos数据库帐户进行其他测试,则可以在测试中使用此功能。 开发情况还可能涉及一些用例,在这些用例中,我们要测试概念并在Cosmos数据库帐户中动态创建数据库。 在本技巧中,我们将在数据库对象级别上使用Azure Cosmos数据库帐户,在此我们不做任何操作来操作帐户本身,仅在设置帐户后将数据库添加到该帐户中。

依存关系 (Dependencies)

In addition to using PowerShell’s Az module and connecting to Azure using the Az module, we will use an existing Cosmos database account for running tests of creating and removing a database. In the below script, we first connect – which will be required for our later scripts – and we also get a list of all our Azure Cosmos DBs. We can use this check for logic when we need to first validate the existence of a Cosmos database account, such as checking if the name of scosdb exists (or the name you use).

除了使用PowerShell的Az模块并使用Az模块连接到Azure外,我们还将使用现有的Cosmos数据库帐户来运行创建和删除数据库的测试。 在下面的脚本中,我们首先连接-这是以后的脚本所必需的-并且我们还获得所有Azure Cosmos数据库的列表。 当我们需要首先验证Cosmos数据库帐户的存在时,例如可以检查scosdb的名称是否存在(或您使用的名称),可以使用此检查逻辑。

Connect-AzAccount  | Out-Null $resource = "Microsoft.DocumentDb/databaseAccounts"(Get-AzResource -ResourceType $resource).Name

In NoSQL contexts such as MongoDB, we can use a “use database” statement or a connection to a database with a database that doesn’t exist and create a collection that doesn’t exist from an insert without explicit creation. For NoSQL databases, this is one way to create a database and collection on the fly, but if our use case requires removing it, we may want to use the explicit creation and drop, like we’ll be doing in this tip with Azure Cosmos DB. To demonstrate this on-the-flow creation process, I run the below commands in order in a MongoDB shell session – first I check the existing databases; second, I use a database that doesn’t exist and create a collection in the database using an insert; third I check the collections and databases, and we see the collection and database now exists.

在诸如MongoDB之类的NoSQL上下文中,我们可以使用“使用数据库”语句或使用不存在的数据库与数据库建立连接,并在没有显式创建的情况下从插入创建不存在的集合。 对于NoSQL数据库,这是一种动态创建数据库和集合的方法,但是如果我们的用例需要删除它,我们可能要使用显式的创建和删除,就像我们将在本技巧中使用Azure Cosmos一样。 D B。 为了演示此流程创建过程,我在MongoDB Shell会话中按顺序运行以下命令–首先,检查现有数据库; 其次,我使用一个不存在的数据库,并使用插入在数据库中创建一个集合。 第三,我检查集合和数据库,然后我们看到集合和数据库现在存在。

show dbs
use OurNewestDatabase
db.OurNewestCollection.insert({“_id”:1})
show collections
show dbs

In some contexts, we may create collections and databases without explicit creations in NoSQL databases like Azure Cosmos DB or MongoDB

在某些情况下,我们可能会在没有像Azure Cosmos DB或MongoDB这样的NoSQL数据库中显式创建创建集合和数据库的情况

This development style may be an option in some NoSQL contexts, but it may not be the option we choose in testing if we want to re-use the parameters for the explicit creation and removal of the database we create. Our approach will differ in this tip since we’ll focus on the explicit creation and removal of a database.

在某些NoSQL上下文中,此开发样式可能是一个选项,但是如果我们要重用这些参数来显式创建和删除所创建的数据库,则可能不是我们在测试中选择的选项。 在本技巧中,我们的方法将有所不同,因为我们将专注于显式创建和删除数据库。

获取数据库属性 (Getting Properties of a Database)

As we noticed in our check, we can call the Get-AzResource to discover if a resource exists – we searched for all Azure Cosmos DB accounts (Microsoft.DocumentDB). With this same function, we can obtain some of the properties of our existing Cosmos DB account – in this case the scosdb account. Calling the function with the parameter of ResourceType and Name will return some specific information about this account – we’ll save some of these values to variables, such as kind, location and rGroup.

正如我们在检查中注意到的那样,我们可以调用Get-AzResource来发现资源是否存在-我们搜索了所有Azure Cosmos DB帐户(Microsoft.DocumentDB)。 使用相同的功能,我们可以获得现有Cosmos DB帐户的某些属性-在这种情况下为scosdb帐户。 使用ResourceTypeName参数调用该函数将返回有关此帐户的一些特定信息-我们会将其中一些值保存到变量中,例如kindlocationrGroup

$resource = "Microsoft.DocumentDb/databaseAccounts"
$cosmosdb = "scosdb"
$rGroup = (Get-AzResource -ResourceType $resource -Name $cosmosdb).ResourceGroupName
$kind = (Get-AzResource -ResourceType $resource -Name $cosmosdb).Kind
$location = (Get-AzResource -ResourceType $resource -Name $cosmosdb).Location

These properties can be useful if we want to create another Cosmos database account that has similar properties – such as creating one that has the same location and resource group name. We’ll also look at using properties for creating a database within the Cosmos database account.

如果我们要创建另一个具有相似属性的Cosmos数据库帐户,例如创建具有相同位置和资源组名称的Cosmos数据库帐户,这些属性将很有用。 我们还将研究如何使用属性在Cosmos数据库帐户中创建数据库。

创建数据库 (Creating a Database)

If we consider the use case of a unit and security test where we want to run a test on a newly created database and remove it when finished where our Azure Cosmos DB has other unit tests running on other databases, removing the Cosmos account would cause disruptions. For this use case, we want to dynamically create a SQL API database within our Cosmos database account for our testing. The creation will carry some similarities to the account creation and with our properties that we saved in the above script from the existing account, we’ll be able to use the variables to create our database.

如果考虑单元和安全测试的用例,我们要在新创建的数据库上运行测试,然后在Azure Cosmos DB在其他数据库上运行其他单元测试的地方完成后将其删除,则删除Cosmos帐户会导致中断。 对于此用例,我们希望在Cosmos数据库帐户中动态创建一个SQL API数据库以进行测试。 创建将与帐户创建具有一些相似之处,并且与我们在上述脚本中从现有帐户中保存的属性一样,我们将能够使用变量来创建数据库。

In the below code, we create our database account with our Azure Cosmos DB and we see output returned from this call. We use the -Force parameter here to avoid the dialog that returns asking us to input yes or no, which is more appropriate for automated development contexts. If you prefer the dialog box, you can remove this parameter.

在下面的代码中,我们使用Azure Cosmos DB创建数据库帐户,然后看到此调用返回的输出。 我们在此处使用-Force参数来避免返回要求我们输入yes或no的对话框,该对话框更适合自动化开发环境。 如果您喜欢该对话框,则可以删除此参数。

$resource = "Microsoft.DocumentDb/databaseAccounts"
$type = "Microsoft.DocumentDb/databaseAccounts/apis/databases"
$cosmosdb = "scosdb"
$testdb = "TestDB"
$api = "2015-04-08"$rGroup = (Get-AzResource -ResourceType $resource -Name $cosmosdb).ResourceGroupName$properties = @{}
$properties.Add("resource", @{"id" = "$testdb"})
$properties.Add("options", @{"throughput" = 1000})New-AzResource -ResourceType $type -ApiVersion $api -ResourceGroupName $rGroup -Name "$cosmosdb/sql/$testdb" -PropertyObject $properties -Force

Part of the returned output when we create our SQL API database in our Azure Cosmos DB

在Azure Cosmos DB中创建SQL API数据库时返回的输出的一部分

As we see in the Azure portal, we’ve created our SQL API database with a throughput of 1000 RUs within our Cosmos database account.

正如我们在Azure门户中看到的那样,我们已经在Cosmos数据库帐户中创建了吞吐量为1000 RUSQL API数据库。

The Azure Portal view of our SQL API database in our Azure Cosmos DB

Azure Cosmos DB中SQL API数据库的Azure Portal视图

Like our unit and security testing scenario, we can use this model to horizontally scale SQL API databases within our Cosmos database account, test proof-of-concepts that we don’t intend to keep after we prove out our concept, or other development purposes that allow us to re-use our creation scripts saving us redevelopment efforts.

像我们的单元和安全测试方案一样,我们可以使用此模型在Cosmos数据库帐户内水平扩展SQL API数据库,测试在证明概念或其他开发目的后不打算保留的概念验证这样我们就可以重新使用创建脚本,从而节省了我们的重新开发工作。

删除数据库 (Removing a Database)

In testing situations, we may only create a database to perform a unit or security test without keeping the database that we created. The same pattern may also be true if we have a quick proof-of-concept test where we build an Azure Cosmos DB and remove it when we’re done with our test (retaining the results of our findings). The purpose of removing the database doesn’t only eliminate possible costs by keeping something unused in our environment, it prevents other developers from using it or misunderstanding the intent. This latter point becomes an issue in organizations where developers move on and newer developers wonder what was the function of architecture and feel reluctant to remove it since the history is questionable. We never want resources we’re not using for this reason. Finally, removing a resource that we’re not using completes the cycle of our intent – if our intent is testing, the final cycle may be the removal of what we created.

在测试情况下,我们只能创建一个数据库来执行单元测试或安全测试,而不会保留我们创建的数据库。 如果我们有一个快速的概念验证测试,即建立一个Azure Cosmos数据库并在完成测试后将其删除(保留我们的发现结果),则同样的模式也可能成立。 删除数据库的目的不仅是通过在我们的环境中保留一些未使用的东西来消除可能的成本,而且还可以防止其他开发人员使用它或误解意图。 后一点成为组织中的一个问题,在这些组织中,开发人员继续前进,新的开发人员想知道架构的功能是什么,并且由于历史久远而感到不愿删除它。 由于这个原因,我们永远不希望我们不使用资源。 最后,删除我们不使用的资源将完成我们的意图循环–如果我们的意图是测试,则最后一个循环可能是删除我们创建的内容。

If we create databases for unit and security testing or proof-of-concept reasons in our Azure Cosmos DB and don’t need the Cosmos database account, we may remove it on this level. Recall that the creation and removal of the Cosmos database requires some time, so we may want to keep the account and remove individual databases within the account. This is also true if we use the Cosmos database account as a holder for databases used in testing and development.

如果我们出于Azure Cosmos数据库中的单元和安全测试或概念验证的原因而创建数据库,并且不需要Cosmos数据库帐户,则可以在此级别上将其删除。 回想一下,Cosmos数据库的创建和删除需要一些时间,因此我们可能想要保留该帐户并删除该帐户内的各个数据库。 如果我们使用Cosmos数据库帐户作为测试和开发中使用的数据库的持有人,则也是如此。

$resource = "Microsoft.DocumentDb/databaseAccounts"
$type = "Microsoft.DocumentDb/databaseAccounts/apis/databases"
$cosmosdb = "scosdb"
$testdb = "TestDB"
$api = "2015-04-08"$rGroup = (Get-AzResource -ResourceType $resource -Name $cosmosdb).ResourceGroupNameRemove-AzResource -ResourceType $type -ApiVersion $api -ResourceGroupName $rGroup -Name "$cosmosdb/sql/$testdb" -Force

We confirm that our database within our Azure Cosmos DB is removed in the portal

我们确认已在门户中删除了Azure Cosmos DB中的数据库

When removing our SQL API database, we don’t need to include the properties and we can see the number of parameters we’re able to re-use from our creation process. Like with the creation process, we include the -Force parameter to avoid the dialog box – a useful technique when we want an automated process building and removing our SQL API database.

在删除SQL API数据库时,我们不需要包括属性,并且可以看到在创建过程中可以重复使用的参数数量。 与创建过程一样,我们包括-Force参数来避免出现对话框-这是一种有用的技术,当我们希望自动构建和删除SQL API数据库时使用。

结论 (Conclusion)

As we see with PowerShell and the Az module, we can get information about our Azure Cosmos DB account, and dynamically create and remove databases within our Cosmos database account. This can be useful in development for new concepts we want to test and validate – such as expansions to horizontal scaling – or it can be useful for unit and security tests where we create, test and remove the database following the test. Unlike creating the Cosmos database account, creating a database within the Cosmos database account in PowerShell is faster and is more appropriate if we want to retain the account while letting the database go. In addition to the dynamic creation, we can get properties we need for the account to update configuration information if required. Finally, if our situation calls for removal, we can remove the database within the Azure Cosmos DB.

如使用PowerShell和Az模块所看到的,我们可以获得有关Azure Cosmos DB帐户的信息,并在Cosmos数据库帐户内动态创建和删除数据库。 这对于开发我们要测试和验证的新概念(例如扩展到水平缩放)很有用,或者对于在测试之后创建,测试和删除数据库的单元和安全测试也很有用。 与创建Cosmos数据库帐户不同,在PowerShell中使用Cosmos数据库帐户创建数据库速度更快,并且如果我们想在放开数据库的同时保留该帐户,则更合适。 除了动态创建,我们还可以获取帐户所需的属性,以在需要时更新配置信息。 最后,如果情况需要删除,则可以删除Azure Cosmos DB中的数据库。

目录 (Table of contents)

Creating and Removing Azure Cosmos DBs with PowerShell
Getting and Updating Connection Information for Azure Cosmos DB
Creating and Removing Databases with PowerShell In Azure Cosmos DB
Increasing or Decreasing Scale for Azure Cosmos DB
Creating Containers with PowerShell For Azure Cosmos DB
使用PowerShell创建和删除Azure Cosmos数据库
获取和更新Azure Cosmos DB的连接信息
在Azure Cosmos DB中使用PowerShell创建和删除数据库
增加或减少Azure Cosmos DB的规模
使用PowerShell为Azure Cosmos DB创建容器

翻译自: https://www.sqlshack.com/creating-and-removing-databases-with-powershell-in-azure-cosmos-db/

azure云数据库

azure云数据库_在Azure Cosmos DB中使用PowerShell创建和删除数据库相关推荐

  1. mysql 将xml插入数据库_从xml文档中读取数据并插入mysql数据库中

    // TODO: Add your control notification handler code here CMarkup xml; xml.Load("色谱案例表.xml" ...

  2. azure db 设置时区_在Azure Cosmos DB中应用字段运算符和对象

    azure db 设置时区 Since we will sometimes require removing documents in Azure Cosmos DB, we'll want to b ...

  3. azure 入门_Azure Cosmos DB中的子文档入门

    azure 入门 As we've worked with Azure Cosmos DB, we've seen that we can store data within fields and t ...

  4. db link的查看创建与删除

    --db link的查看创建与删除 ----------------------------2013/10/30 1.查看dblink select owner,object_name from db ...

  5. Oracle数据库 二 创建和删除数据库

    Database configuration Assistant 创建或删除数据库 在开始菜单中可已找到安装数据库后的database configuration assistant 软件,该软件用于 ...

  6. 【简洁明了MySQL】MySQL基础操作之连接,创建和删除数据库

    MYSQL基础操作之连接,创建和删除数据库 一.MySQL连接 1.方法一:客户端直接连接法(不推荐) 2. 方法二:使用命令行登录 3.两种不同的登陆状态 4.创建用户 二.创建数据库 1.crea ...

  7. azure云数据库_配置Azure SQL数据库防火墙

    azure云数据库 介绍 (Introduction) The Azure SQL Database firewall lets you decide which IP addresses may o ...

  8. azure云数据库_使用Azure SQL数据库构建ASP.NET应用

    azure云数据库 In this article, you will learn about Azure SQL Database and its uses. Then the article sp ...

  9. azure云数据库_在Azure SQL数据库中保护数据的五种方法

    azure云数据库 When storing data in the cloud the main concern companies generally have is whether or not ...

最新文章

  1. NAR:测序数据鉴别和去除rRNA序列利器RiboDetector
  2. Py之logging:logging的简介、安装、使用方法之详细攻略
  3. CaSS中lisp命令不可用_小白想要自学南方cass?又苦于不会修改比例尺,这里有一份小白快速上手的测绘神器...
  4. 怪物行为树案例_Behavior Designer行为树(简单实现巡逻)
  5. Git之删除本地无用分支
  6. 使用JPA标准@ViewScoped通过分页,过滤和排序进行Primefaces DataTable延迟加载
  7. leetcode99. 恢复二叉搜索树(优先队列)
  8. HTML5 API详解(6):getUserMedia实现拍照功能
  9. Oracle查询指定表里的触发器
  10. 初入java行业,环境你可知如何配置
  11. CSS基础之清除浮动
  12. web.xml 总结
  13. jenkins java jar_Jenkins 安装和配置、启动jar包
  14. 【Spring学习笔记-0】Spring开发所需要的核心jar包
  15. 一个简单的TCP客户/服务器的程序
  16. 图片完整检查linux,Linux 下的免费图片查看器
  17. springboot源码 红色J_通达信精准指标,精确箱体——(主图 源码)介绍
  18. 惠普笔记本电脑驱动BIOS下载中心,战66驱动下载
  19. 人工智能的局限性--王垠
  20. ZDNET的一个技术博客评选结果

热门文章

  1. freebsd mysql删_FreeBSD增加、删除以及管理用户(适用Linux)
  2. idhttp.post方式 调用datasnap rest 远程方法(转咏南兄)
  3. Andrew Stankevich#39;s Contest (1)
  4. 嵌入式实时操作系统的可裁剪性及其实现
  5. ★LeetCode(942)——增减字符串匹配(JavaScript)
  6. 前端根据设计图精确开发 (攻具)
  7. 【零基础学Java】—Collections集合工具类(四十二)
  8. cpu 被挂起和阻塞_同步异步阻塞非阻塞并发并行讲解
  9. 你见过“最没见过世面”的女孩子是什么样的?
  10. 事业单位非编制值得去吗?