Azure Storage Table 真的是 一個讓人又愛又恨的東西,他很便宜,所以我就自己寫了一個簡單的 library 來操控
用了一陣子想說就分享一下
基本上操作很簡單
1. 首先初始化
//Init
var role = new No2verse.AzureTable.Base.AzureTableRole("TEST1", new No2verse.AzureTable.AzureStorageSettings
{
//Azure Table Connection String.
//DefaultEndpointsProtocol=https;AccountName=azureblobname;AccountKey=....
ConnectionString = AzureConnectionString
});
2.建立 Table
,請注意,如果後面 needToCheckExisted 帶入 true ,他會幫你檢查如果 table 不存在,他會自動建立,當然有判斷就會比較耗效能
//Create Table
//needToCheckExisted for check table existed and create .
var operatorMain = new No2verse.AzureTable.Collections.Operator(role, "tablesample1", true);
3. 新增修改資料,請注意你的物件必須要繼承 DTableEntity ,這樣就變得很簡單
//UpdateData
var data1 = new User
{
PartitionKey = "PART1",
RowKey = "DONMA",
Birth = new DateTime(1983, 6, 21),
Name = "DONMA HSU",
CarInfo = new User.Car { BuyDate = DateTime.Now, Color = "RED", No = 123456, Name = "SWIFT" }
};
operatorMain.Update(data1);

4. 刪除資料
//Delete Data
operatorMain.Delete("PART1", "DONMA");
5.查詢單筆資料
//Get Data
var queryMain = new No2verse.AzureTable.Collections.Query(role, "tablesample1");
var data=queryMain.DataByPRKey("PART1", "DONMA");
Console.WriteLine(JsonConvert.SerializeObject(data));
6.使用 PartitionKey 取得所有資料
//GetAllData By PartitionKey
var data2 = queryMain.DatasByPartitionKey("PART1");
Console.WriteLine(JsonConvert.SerializeObject(data2));
7.根據 PartitionKey 取得資料數量,這個如果資料量大會慢,我自己測試 50萬筆資料需要花費 72秒,10萬筆資料需要花費 16 秒
//Data Count but Slow
//All Table Count
var tableCount= queryMain.DataCount();
Console.WriteLine(tableCount);
source: https://github.com/donma/No2verse.AzureTable
基本上還有其他 function 這邊我提供一下我的 interface ,大概就可以知道能幹嘛
using No2verse.AzureTable.Collections;
namespace No2verse.AzureTable.Base
{
///
///
///
public interface IAzureTableOperator
{
string TableName { get; }
bool IsAutoGCOnDtor { get; set; }
AzureTableRole Role { get; }
bool Delete(string partitionKey, string rowKey);
bool DeleteByPK(string partitionKey);
bool DeleteByRK(string rowKey);
string Insert(DTableEntity data);
bool IsDataExist(string partitionKey, string rowKey);
Operator Update(DTableEntity data);
Operator UpdateWithEtag(DTableEntity data);
bool DeleteWithEtag(string partitionKey, string rowKey, string eTag);
}
}
using System.Collections.Generic;
namespace No2verse.AzureTable.Base
{
///
///
///
///
public interface IAzureTableQuery
{
string TableName { get; }
bool IsAutoGCOnDtor { get; set; }
AzureTableRole Role { get; }
IEnumerable All();
string[] AllPartitionKeys();
string[] AllRowKeysByPartitionKey(string partitionKey);
T[] AllDatas();
List AllDatasList();
T DataByPRKey(string partitionKey, string rowKey);
List DatasByPartitionKey(string partitionKey);
List DatasByRowKey(string rowKey);
int DataCount();
int DataCountByPartitionKey(string partitionKey);
List DataListByKeys(params PRPair[] prpairs);
bool IsDataExist(string partitionKey, string rowKey);
}
}
至於第七點如何加速,我有一些其他想法之後我再來寫文章分享一下