CZ 1 AZURE STORAGE TABLES: Stworzenie klienta

advertisement
CZ 1 AZURE STORAGE TABLES:
Stworzenie klienta:
CloudStorageAccount storageAccount = new CloudStorageAccount(new
StorageCredentials( "<accName>", "<key>"), true);
Stworzenie tabeli:
CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("<tableName>");
table.CreateIfNotExists();
Encja danych:
public class CustomerEntity : TableEntity
{
public CustomerEntity(string lastName, string firstName)
{
this.PartitionKey = lastName;
this.RowKey = firstName;
}
public CustomerEntity() { }
public string Email { get; set; }
public string PhoneNumber { get; set; }
}
Wstawienie encji:
CustomerEntity customer1 = new CustomerEntity("Harp", "Walter");
customer1.Email = "[email protected]";
customer1.PhoneNumber = "425-555-0101";
TableOperation insertOperation = TableOperation.Insert(customer1);
table.Execute(insertOperation);
Wykonanie zestawu operacji:
TableBatchOperation batchOperation = new TableBatchOperation();
CustomerEntity customer1 = new CustomerEntity("Smith", "Jeff");
customer1.Email = "[email protected]";
customer1.PhoneNumber = "425-555-0104";
CustomerEntity customer2 = new CustomerEntity("Smith", "Ben");
customer2.Email = "[email protected]";
customer2.PhoneNumber = "425-555-0102";
batchOperation.Insert(customer1);
batchOperation.Insert(customer2);
table.ExecuteBatch(batchOperation);
Pobieranie zestawów encji:
1:
TableQuery<CustomerEntity> query = new
TableQuery<CustomerEntity>().Where(TableQuery.GenerateFilterCondition("Part
itionKey", QueryComparisons.Equal, "Smith"));
foreach (CustomerEntity entity in table.ExecuteQuery(query))
{
//TODO
}
2:
TableQuery<CustomerEntity> rangeQuery = new
TableQuery<CustomerEntity>().Where(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey",
QueryComparisons.Equal, "Smith"),
TableOperators.And,
TableQuery.GenerateFilterCondition("RowKey",
QueryComparisons.LessThan, "E")));
foreach (CustomerEntity entity in table.ExecuteQuery(rangeQuery))
{
//TODO
}
Pobieranie pojedyńczej encji:
TableOperation retrieveOperation =
TableOperation.Retrieve<CustomerEntity>("Smith", "Ben");
TableResult retrievedResult = table.Execute(retrieveOperation);
Aktualizacja encji:
TableOperation retrieveOperation =
TableOperation.Retrieve<CustomerEntity>("Smith", "Ben");
TableResult retrievedResult = table.Execute(retrieveOperation);
CustomerEntity updateEntity = (CustomerEntity)retrievedResult.Result;
if (updateEntity != null)
{
updateEntity.PhoneNumber = "425-555-0105";
TableOperation updateOperation = TableOperation.Replace(updateEntity);
table.Execute(updateOperation);
}
Wstawienie lub podmiana:
TableOperation retrieveOperation =
TableOperation.Retrieve<CustomerEntity>("Smith", "Ben");
TableResult retrievedResult = table.Execute(retrieveOperation);
CustomerEntity updateEntity = (CustomerEntity)retrievedResult.Result;
if (updateEntity != null)
{
updateEntity.PhoneNumber = "425-555-1234";
TableOperation insertOrReplaceOperation =
TableOperation.InsertOrReplace(updateEntity);
table.Execute(insertOrReplaceOperation);
}
Pobranie niektórych pól encji:
TableQuery<DynamicTableEntity> projectionQuery = new
TableQuery<DynamicTableEntity>().Select(new string[] { "Email" });
EntityResolver<string> resolver = (pk, rk, ts, props, etag) =>
props.ContainsKey("Email") ? props["Email"].StringValue : null;
foreach (string projectedEmail in table.ExecuteQuery(projectionQuery,
resolver, null, null))
{
//TODO
}
Asynchroniczne pobieranie danych (partiami):
TableQuery<CustomerEntity> tableQuery = new TableQuery<CustomerEntity>();
TableContinuationToken continuationToken = null;
do
{
TableQuerySegment<CustomerEntity> tableQueryResult =
await table.ExecuteQuerySegmentedAsync(tableQuery,
continuationToken);
continuationToken = tableQueryResult.ContinuationToken;
foreach (var result in tableQueryResult.Results)
{
//TODO
}
} while(continuationToken != null);
Kasowanie encji:
TableOperation retrieveOperation =
TableOperation.Retrieve<CustomerEntity>("Smith", "Ben");
TableResult retrievedResult = table.Execute(retrieveOperation);
CustomerEntity deleteEntity = (CustomerEntity)retrievedResult.Result;
if (deleteEntity != null)
{
TableOperation deleteOperation = TableOperation.Delete(deleteEntity);
table.Execute(deleteOperation);
}
Kasowanie tabeli:
CloudTable table = tableClient.GetTableReference("people");
table.DeleteIfExists();
CZ 2 AZURE DOCUMENTDB:
Stworzenie klienta:
var client = new DocumentClient(new Uri("<accUrl>"),"<key>");
Stworzenie bazy:
await this.client.CreateDatabaseAsync(new Database { Id = "<dbName>" });
Stworzenie kolekcji:
DocumentCollection collectionInfo = new DocumentCollection();
collectionInfo.Id = collectionName;
collectionInfo.IndexingPolicy = new IndexingPolicy(new
RangeIndex(DataType.String) { Precision = -1 });
await this.client.CreateDocumentCollectionAsync(
UriFactory.CreateDatabaseUri("<dbName>"),
new DocumentCollection { Id = "<collName>" },
new RequestOptions { OfferThroughput = 400 });
Encje danych:
public class Family
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
public string LastName { get; set; }
public Parent[] Parents { get; set; }
public Child[] Children { get; set; }
public Address Address { get; set; }
public bool IsRegistered { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
public class Parent
{
public string FamilyName { get; set; }
public string FirstName { get; set; }
}
public class Child
{
public string FamilyName { get; set; }
public string FirstName { get; set; }
public string Gender { get; set; }
public int Grade { get; set; }
public Pet[] Pets { get; set; }
}
public class Pet
{
public string GivenName { get; set; }
}
public class Address
{
public string State { get; set; }
public string County { get; set; }
public string City { get; set; }
}
Stworzenie dokumentu w bazie:
await
this.client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(
"<dbName>", "<collName>"), familyObject);
Stworzenie zapytania do bazy:
1 (LINQ):
IQueryable<Family> familyQuery = this.client.CreateDocumentQuery<Family>(
UriFactory.CreateDocumentCollectionUri(databaseName, collectionName),
queryOptions).Where(f => f.LastName == "Andersen");
foreach (Family family in familyQuery)
{
//TODO
}
2 (SQL):
IQueryable<Family> familyQueryInSql =
this.client.CreateDocumentQuery<Family>(
UriFactory.CreateDocumentCollectionUri(databaseName, collectionName),
"SELECT * FROM Family WHERE Family.lastName = 'Andersen'",
queryOptions);
foreach (Family family in familyQueryInSql)
{
//TODO
}
Aktualizacja dokumentu w bazie:
await this.client.ReplaceDocumentAsync(UriFactory.CreateDocumentUri(
"<dbName>", "<collName>", "<familyId>"), updatedFamilyObject);
Skasowanie dokumentu w bazie:
await this.client.DeleteDocumentAsync(
UriFactory.CreateDocumentUri("<dbName>", "<collName>", "<familyId>"));
Skasowanie kolekcji:
await this.client.DeleteDocumentCollectionAsync(
UriFactory.CreateDocumentCollectionUri("<dbname>", "<colName>"));
Skasowanie bazy danych:
await this.client.DeleteDatabaseAsync(
UriFactory.CreateDatabaseUri("<dbName>"));
Download