[Build and Deploy Staging Site] Publish from microsoft/azuretipsandtricks-private:main/src/blog
This commit is contained in:
Родитель
f5034cad53
Коммит
6cda9774e5
|
@ -0,0 +1,227 @@
|
|||
---
|
||||
type: post
|
||||
title: "Tip 360 - How to choose between Azure Table Storage and Azure Cosmos DB Table API"
|
||||
excerpt: "Learn how to choose between Azure Table Storage and Azure Cosmos DB Table API"
|
||||
tags: [Storage]
|
||||
share: true
|
||||
date: 2022-04-05 09:00:00
|
||||
---
|
||||
|
||||
::: tip
|
||||
|
||||
:fire: Help shape the future of Azure Tips and Tricks by telling what you'd like us to write about [here](https://github.com/microsoft/AzureTipsAndTricks/issues/new?assignees=&labels=&template=survey.md&title=).
|
||||
|
||||
:bulb: Learn more : [Azure Table Storage overview](https://docs.microsoft.com/azure/storage/tables/table-storage-overview?WT.mc_id=docs-azuredevtips-azureappsdev).
|
||||
|
||||
:tv: Watch the video : [How to choose between Azure Table Storage and Azure Cosmos DB Table API](https://youtu.be/J5fsKMkRqV8?WT.mc_id=youtube-azuredevtips-azureappsdev).
|
||||
|
||||
:::
|
||||
|
||||
### How to choose between Azure Table Storage and Azure Cosmos DB Table API
|
||||
|
||||
#### Semi-structured data storage
|
||||
You can use [Azure Table Storage](https://docs.microsoft.com/azure/storage/tables/table-storage-overview?WT.mc_id=docs-azuredevtips-azureappsdev) to store data in rows and columns, in a semi-structured format. Each row can have different columns, and the data isn't linked. Azure Table Storage is a great way to store massive amounts of information, and it is fast and highly available. [Azure Cosmos DB](https://docs.microsoft.com/azure/cosmos-db/introduction?WT.mc_id=docs-azuredevtips-azureappsdev) also offers table storage through its [Table API](https://docs.microsoft.com/azure/cosmos-db/table/introduction?WT.mc_id=docs-azuredevtips-azureappsdev). You can use the same API to use Azure Table Storage and Azure Cosmos DB Table API.
|
||||
|
||||
Azure Table Storage and Azure Cosmos DB Table API are very similar, so how do you know which one to use and when?
|
||||
|
||||
This post will help you choose between [Azure Table Storage](https://docs.microsoft.com/azure/storage/tables/table-storage-overview?WT.mc_id=docs-azuredevtips-azureappsdev) and [Azure Cosmos DB Table API](https://docs.microsoft.com/azure/cosmos-db/table/introduction?WT.mc_id=docs-azuredevtips-azureappsdev).
|
||||
|
||||
#### Prerequisites
|
||||
If you want to follow along, you'll need the following:
|
||||
* An Azure subscription (If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=azure-azuredevtips-azureappsdev) before you begin)
|
||||
* An existing Azure Cosmos DB that works with the Table API. Learn how to create one in [this quickstart](https://docs.microsoft.com/azure/cosmos-db/sql/create-cosmosdb-resources-portal?WT.mc_id=docs-azuredevtips-azureappsdev)
|
||||
* An existing Azure Storage Account. Learn how to create one in [this quickstart](https://docs.microsoft.com/azure/storage/common/storage-account-create?WT.mc_id=docs-azuredevtips-azureappsdev)
|
||||
* The latest version of [Visual Studio](https://visualstudio.microsoft.com/?WT.mc_id=microsoft-azuredevtips-azureappsdev) or [VS Code](https://code.visualstudio.com/?WT.mc_id=other-azuredevtips-azureappsdev)
|
||||
|
||||
#### Comparing Azure Table Storage to Azure Cosmos DB Table API
|
||||
To help you choose between Azure Cosmos DB Table API and Azure Table Storage, we'll use a simple application that tests the speed of write- and read operations for both services. We'll use Visual Studio to create a simple console application, and you can use VS Code if you like.
|
||||
|
||||
1. Open Visual Studio and click **Create a new project**
|
||||
2. Select **Console Application** as the application template
|
||||
3. Enter a **Name** for the project
|
||||
4. Select a **Location**
|
||||
5. Click **Next**
|
||||
6. Select the latest **Framework** version
|
||||
7. Click **Create**
|
||||
8. Now that we have a project, create a new file in the project called **App.config**
|
||||
9. Add the following code to the **App.config** file and replace the values with the **connection strings** to your existing Azure Table Storage and Azure Cosmos DB
|
||||
|
||||
```
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<configuration>
|
||||
<appSettings>
|
||||
<add key="CosmosStorageConnectionString" value="" />
|
||||
<add key="TableStorageConnectionString" value="" />
|
||||
</appSettings>
|
||||
</configuration>
|
||||
```
|
||||
10. Next, right-click the project file and select **"Manage NuGet Packages"**
|
||||
11. Install the package [Azure.Data.Tables](https://github.com/Azure/azure-sdk-for-net/blob/Azure.Data.Tables_12.2.0/sdk/tables/Azure.Data.Tables/README.md?WT.mc_id=github-azuredevtips-azureappsdev) and **System.Configuration.ConfigurationManager**. The project structure should now look like the image below
|
||||
|
||||
<img :src="$withBase('/files/144solution.png')">
|
||||
|
||||
(The project structure in Visual Studio)
|
||||
|
||||
12. Open **Program.cs** and replace the code in it with the code below. This connects to Azure Table Storage, creates a Table in it, and creates and reads rows. It does the same for Azure Cosmos DB
|
||||
|
||||
```
|
||||
namespace TableSBS
|
||||
{
|
||||
using Azure;
|
||||
using Azure.Data.Tables;
|
||||
using Azure.Data.Tables.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
/// <summary>
|
||||
/// This sample program shows how to use the Azure tables SDK to work with tables
|
||||
/// </summary>
|
||||
public class Program
|
||||
{
|
||||
/// <summary>
|
||||
/// Run common Table CRUD and query operations using the Azure Cosmos DB endpoints
|
||||
/// ("premium tables")
|
||||
/// </summary>
|
||||
/// <param name="args">Command line arguments</param>
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
string connectionString = ConfigurationManager.AppSettings["CosmosStorageConnectionString"];
|
||||
|
||||
int numIterations = 500;
|
||||
|
||||
//run against Cosmo DB Table API
|
||||
TableServiceClient tableClient = new TableServiceClient(connectionString);
|
||||
|
||||
Program p = new Program();
|
||||
|
||||
p.Run(tableClient, numIterations, "Azure Cosmos DB");
|
||||
|
||||
|
||||
connectionString = ConfigurationManager.AppSettings["TableStorageConnectionString"];
|
||||
|
||||
//run against Azure Table Storage
|
||||
tableClient = new TableServiceClient(connectionString);
|
||||
|
||||
p.Run(tableClient, numIterations, "Azure Table Storage");
|
||||
|
||||
Console.WriteLine("\n");
|
||||
Console.WriteLine("Press any key to exit...");
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Run a bunch of core Table operations. Each operation is run ~500 times to measure latency.
|
||||
/// You can swap the endpoint and compare with regular Azure Table storage.
|
||||
/// </summary>
|
||||
public void Run(TableServiceClient tableServiceClient, int numIterations, string target)
|
||||
{
|
||||
Console.WriteLine("\n");
|
||||
Console.WriteLine("Creating Table if it doesn't exist, for " + target);
|
||||
|
||||
tableServiceClient.DeleteTable("People");
|
||||
tableServiceClient.CreateTable("People");
|
||||
|
||||
List<CustomerEntity> items = new List<CustomerEntity>();
|
||||
Stopwatch watch = new Stopwatch();
|
||||
|
||||
Console.WriteLine("\n");
|
||||
Console.WriteLine("Running inserts: ");
|
||||
|
||||
var tableClient = tableServiceClient.GetTableClient("People");
|
||||
|
||||
double latencyInMsnsertLatencyInMs = 0;
|
||||
|
||||
for (int i = 0; i < numIterations; i++)
|
||||
{
|
||||
watch.Start();
|
||||
|
||||
CustomerEntity item = new CustomerEntity()
|
||||
{
|
||||
PartitionKey = Guid.NewGuid().ToString(),
|
||||
RowKey = Guid.NewGuid().ToString(),
|
||||
Email = $"{GetRandomString(6)}@contoso.com",
|
||||
PhoneNumber = "425-555-0102",
|
||||
Bio = GetRandomString(1000)
|
||||
};
|
||||
|
||||
tableClient.AddEntity(item);
|
||||
|
||||
latencyInMsnsertLatencyInMs += watch.Elapsed.TotalMilliseconds;
|
||||
|
||||
Console.Write($"\r\tInsert # {i}");
|
||||
|
||||
items.Add(item);
|
||||
|
||||
watch.Reset();
|
||||
}
|
||||
|
||||
Console.Write($"\r\tInsert completed in {latencyInMsnsertLatencyInMs / numIterations} ms on average.");
|
||||
|
||||
Console.WriteLine("\n");
|
||||
Console.WriteLine("Running retrieves: ");
|
||||
|
||||
double readLatencyInMs = 0;
|
||||
|
||||
for (int i = 0; i < numIterations; i++)
|
||||
{
|
||||
watch.Start();
|
||||
|
||||
tableClient.GetEntity<CustomerEntity>(items[i].PartitionKey, items[i].RowKey);
|
||||
|
||||
readLatencyInMs += watch.Elapsed.TotalMilliseconds;
|
||||
|
||||
Console.Write($"\r\tRead # {i}");
|
||||
|
||||
watch.Reset();
|
||||
}
|
||||
|
||||
Console.Write($"\r\tRetrieve completed in {readLatencyInMs / numIterations} ms on average.");
|
||||
}
|
||||
|
||||
public string GetRandomString(int length)
|
||||
{
|
||||
Random random = new Random(System.Environment.TickCount);
|
||||
string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
return new string(Enumerable.Repeat(chars, length).Select(s => s[random.Next(s.Length)]).ToArray());
|
||||
}
|
||||
|
||||
public class CustomerEntity : ITableEntity
|
||||
{
|
||||
public CustomerEntity(string lastName, string firstName)
|
||||
{
|
||||
this.PartitionKey = lastName;
|
||||
this.RowKey = firstName;
|
||||
}
|
||||
|
||||
public CustomerEntity() { }
|
||||
|
||||
public string Email { get; set; }
|
||||
|
||||
public string PhoneNumber { get; set; }
|
||||
|
||||
public string Bio { get; set; }
|
||||
public string PartitionKey { get; set; }
|
||||
public string RowKey { get; set; }
|
||||
public DateTimeOffset? Timestamp { get; set; }
|
||||
public ETag ETag { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
13. Run the application. You'll see that Azure Cosmos DB Table API is faster than Azure Table Storage. This could make a difference in high-performance applications
|
||||
|
||||
<img :src="$withBase('/files/144result.png')">
|
||||
|
||||
(Azure Cosmos DB Table API vs. Azure Table Storage)
|
||||
|
||||
Besides performance, another big difference between Azure Cosmos DB Table API and Azure Table Storage is that Azure Cosmos DB is made for [geographic performance and availability](https://docs.microsoft.com/azure/cosmos-db/distribute-data-globally?WT.mc_id=docs-azuredevtips-azureappsdev). You can create writable Azure Cosmos DB nodes all over the world, making your application fast and available everywhere. Also, Azure Cosmos DB has latency for reads and writes included in [its SLA](https://azure.microsoft.com/support/legal/sla/cosmos-db/v1_3/?WT.mc_id=azure-azuredevtips-azureappsdev). It promises to have a read latency below 10 milliseconds, and a write latency below 15 milliseconds. Azure Table Storage doesn't make any latency promises in [its SLA](https://azure.microsoft.com/en-us/support/legal/sla/storage/v1_5/?WT.mc_id=azure-azuredevtips-azureappsdev).
|
||||
|
||||
<img :src="$withBase('/files/144compare.png')">
|
||||
|
||||
(The biggest differences between Azure Cosmos DB Table API and Azure Table Storage)
|
||||
|
||||
#### Conclusion
|
||||
You can use [Azure Cosmos DB Table API](https://docs.microsoft.com/azure/cosmos-db/table/introduction?WT.mc_id=docs-azuredevtips-azureappsdev) and [Azure Table Storage](https://docs.microsoft.com/azure/storage/tables/table-storage-overview?WT.mc_id=docs-azuredevtips-azureappsdev) with the same [API and SDK](https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/tables/Azure.Data.Tables?WT.mc_id=github-azuredevtips-azureappsdev). When you need extremely high global performance, you can use Azure Cosmos DB for your applications. And if you need fast and reliable semi-structured table storage, you can use Azure Table Storage. Go and check it out!
|
Загрузка…
Ссылка в новой задаче