Documentdb-create-database
提供:Dev Guides
DocumentDB-データベースの作成
この章では、データベースの作成方法を学びます。 Microsoft Azure DocumentDBを使用するには、DocumentDBアカウント、データベース、コレクション、およびドキュメントが必要です。 私たちはすでにDocumentDBアカウントを持っています、今データベースを作成するために2つのオプションがあります-
- Microsoft Azure Portalまたは
- .Net SDK
Microsoft Azureポータルを使用してDocumentDBのデータベースを作成する
ポータルを使用してデータベースを作成するには、次の手順に従います。
- ステップ1 *-Azureポータルにログインすると、ダッシュボードが表示されます。
- ステップ2 *-作成されたDocumentDBアカウントをクリックすると、次のスクリーンショットに示すように詳細が表示されます。
- ステップ3 *-[データベースの追加]オプションを選択し、データベースのIDを指定します。
- ステップ4 *-[OK]をクリックします。
データベースが追加されていることがわかります。 現時点ではコレクションはありませんが、後でJSONドキュメントを格納するコンテナであるコレクションを追加できます。 IDとリソースIDの両方があることに注意してください。
.Net SDKを使用してDocumentDB用のデータベースを作成する
Net SDKを使用してデータベースを作成するには、次の手順に従います。.
- ステップ1 *-前の章のVisual Studioでコンソールアプリケーションを開きます。
- ステップ2 *-新しいデータベースオブジェクトを作成して、新しいデータベースを作成します。 新しいデータベースを作成するには、CreateDatabaseタスクで「mynewdb」に設定しているIdプロパティを割り当てるだけです。
private async static Task CreateDatabase(DocumentClient client) {
Console.WriteLine();
Console.WriteLine("******* *Create Database* ******");
var databaseDefinition = new Database { Id = "mynewdb" };
var result = await client.CreateDatabaseAsync(databaseDefinition);
var database = result.Resource;
Console.WriteLine(" Database Id: {0}; Rid: {1}", database.Id, database.ResourceId);
Console.WriteLine("******* *Database Created* ******");
}
- ステップ3 *-このdatabaseDefinitionをCreateDatabaseAsyncに渡し、Resourceプロパティで結果を取得します。 すべてのオブジェクト作成メソッドは、作成されたアイテム(この場合はデータベース)を説明するResourceプロパティを返します。
Resourceプロパティから新しいデータベースオブジェクトを取得し、DocumentDBが割り当てたリソースIDとともにコンソールに表示されます。
- ステップ4 *-DocumentClientがインスタンス化された後、CreateDocumentClientタスクからCreateDatabaseタスクを呼び出します。
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
await CreateDatabase(client);
}
以下は、これまでの完全なProgram.csファイルです。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Linq;
using Newtonsoft.Json;
namespace DocumentDBDemo {
class Program {
private const string EndpointUrl = "https://azuredocdbdemo.documents.azure.com:443/";
private const string AuthorizationKey = "BBhjI0gxdVPdDbS4diTjdloJq7Fp4L5RO/
StTt6UtEufDM78qM2CtBZWbyVwFPSJIm8AcfDu2O+AfV T+TYUnBQ==";
static void Main(string[] args) {
try {
CreateDocumentClient().Wait();
} catch (Exception e) {
Exception baseException = e.GetBaseException();
Console.WriteLine("Error: {0}, Message: {1}", e.Message, baseException.Message);
}
Console.ReadKey();
}
private static async Task CreateDocumentClient() {
//Create a new instance of the DocumentClient
using (var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey)) {
await CreateDatabase(client);
}
}
private async static Task CreateDatabase(DocumentClient client) {
Console.WriteLine();
Console.WriteLine("******* *Create Database* ******");
var databaseDefinition = new Database { Id = "mynewdb" };
var result = await client.CreateDatabaseAsync(databaseDefinition);
var database = result.Resource;
Console.WriteLine(" Database Id: {0}; Rid: {1}", database.Id, database.ResourceId);
Console.WriteLine("******* *Database Created* ******");
}
}
}
上記のコードをコンパイルして実行すると、データベースとリソースIDを含む次の出力を受け取ります。
******* *Create Database* ******
Database Id: mynewdb; Rid: ltpJAA==
******* *Database Created* ******