2014年10月1日

[Azure]DocumentDB開發

剛剛建了一個DocumentDB來測試看看,下面是一些測試過程的sample code與心得。

DocumentDB的架構如下:

image

首先是建立資料庫與Collection,並將文件加入Collection

    private async void CreateDB()
{
//宣告三個物件
dynamic obj1 = new {
Id = "1",
Name ="Demo1"
};
dynamic obj2 = new {
Id = "2",
Name = "Demo2"
};
dynamic obj3 = new
{
Id = "3",
Name = "Demo3",
DisplayName = "Test display" //測試不同物件型態
};
using (var client = new DocumentClient(new Uri("https://<<your db>>.documents.azure.com:443/"),
"<<your key>>"))
{
var database = new Database
{
Id = "DemoDB"
};
database = await client.CreateDatabaseAsync(database);

var collection = new DocumentCollection { Id = "Demo" };
collection = await client.CreateDocumentCollectionAsync(database.SelfLink, collection);

await client.CreateDocumentAsync(collection.SelfLink, obj1);
await client.CreateDocumentAsync(collection.SelfLink, obj2);
await client.CreateDocumentAsync(collection.SelfLink, obj3);//不同物件型態一樣可以新增成功
}
}



 



接著,查詢剛剛加入的文件



    protected async void Button2_Click(object sender, EventArgs e)
{
using (var client = new DocumentClient(new Uri("https://<<yourdb>>.documents.azure.com:443/"),
"<<your key>>"))
{
var databases = client.CreateDatabaseQuery();
var database = databases.AsEnumerable().Where(x => x.Id == "DemoDB").SingleOrDefault();

var collections = client.CreateDocumentCollectionQuery(database.CollectionsLink);
var collection = collections.AsEnumerable().Where(c => c.Id == "Demo").SingleOrDefault();

var query = client.CreateDocumentQuery(collection.DocumentsLink, "select * from Demo d where d.DisplayName = 'Test display'");
var obj = query.AsEnumerable().ToArray();


await client.DeleteDatabaseAsync(database.SelfLink);
}
}



 



 



測試Stored Procedure



    protected async void Button3_Click(object sender, EventArgs e)
{
StoredProcedure sp = new StoredProcedure()
{
Id = "testSP",
Body = File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,"sp.txt"))
};
using (var client = new DocumentClient(new Uri("https://<<your db>>.documents.azure.com:443/"),
"<<your key>>"))
{
var databases = client.CreateDatabaseQuery();
var database = databases.AsEnumerable().Where(x => x.Id == "DemoDB").SingleOrDefault();
var collections = client.CreateDocumentCollectionQuery(database.CollectionsLink);
var collection = collections.AsEnumerable().Where(c => c.Id == "Demo").SingleOrDefault();
var sps = client.CreateStoredProcedureQuery(collection.StoredProceduresLink);
if (sps.AsEnumerable().Count() > 0)
{
await client.DeleteStoredProcedureAsync(sps.AsEnumerable().Single().SelfLink);
}
sp = await client.CreateStoredProcedureAsync(collection.SelfLink, sp);
}
}



 



Stored procedure定義如下:



function getTestItem(id1, id2){
var cxt = getContext();
var colleciton = cxt.getCollection();
var response = cxt.getResponse();
colleciton.queryDocuments(colleciton.getSelfLink(),
'Select * from Demo d where d.Id="' + id1 + '" or d.Id="' + id2 + '"'
,function(err, documents){
if(err){
response.setBody(err);
}else{
if(documents.length > 0){
response.setBody(response.getBody() + JSON.stringify(documents));
}
}
}
);

}



 



呼叫此Stored Procedure,檢查result物件可以看到回傳的JSon物件結構。



    protected async void Button4_Click(object sender, EventArgs e)
{
using (var client = new DocumentClient(new Uri("https://<<your db>>.documents.azure.com:443/"),
"<<your key>>"))
{
var databases = client.CreateDatabaseQuery();
var database = databases.AsEnumerable().Where(x => x.Id == "DemoDB").SingleOrDefault();
var collections = client.CreateDocumentCollectionQuery(database.CollectionsLink);
var collection = collections.AsEnumerable().Where(c => c.Id == "Demo").SingleOrDefault();

var sp = client.CreateStoredProcedureQuery(collection.StoredProceduresLink).AsEnumerable().Where(s => s.Id == "testSP").SingleOrDefault();

dynamic result = await client.ExecuteStoredProcedureAsync<dynamic>(sp.SelfLink, "3", "1");

}
}



 



 



 




沒有留言:

Blog Archive

About Me