Introduction
Azure Table Storage in a layman’s language is a service that helps in storing non-relational structured data which is also known as the NoSQL data. This data can be used in storage and data operations on structured or semi-structured data.
Know more about table storage, click on below link
Why Azure Table Storage?
Azure Table Storage service is a schema-less where data is stored in tables as a collection of entities which are easier to adapt your data as the application and their need evolves. These table storage data is fast and cost-effective for many different types of applications. Since there are more than one entity in a table, that means one can store multiple entities in a table having different properties. The cost of this is lower than the traditional SQL for similar volumes of data.
Table storage can be used to store flexible datasets like user data for web applications, address books, device information, or other types of metadata that are required by your service. You can store any number of entities in a table, and a storage account may contain any number of tables, up to the capacity limit of the storage account.
Each entity can have up to 252 properties and 3 reserved mandatory properties they are,
- PartitonKey
- RowKey
- Timestamp
What do we mean by the properties mentioned above?
Properties are the unique and case sensitive just like table name and should be reviewed while creating a table. Let me explain this to you by using a example of a ‘toy’. Let us consider toy as an entity, then its properties would be barcode, company’s name, etc. Similarly like this there are 252 custom properties and 3 system properties. These 3 system properties are PartitionKey, RowKey and Timestamp (as mentioned earlier).
Note
Timestamp is system generated but you will have to specify the PartitionKey and RowKey while inserting data into the table.
How to Create Table Storage Using Azure Portal
Before creating the table storage you must have azure subscription, resource group and storage account. To check how to create azure subscription please click on below link,
Below are the simple step to create table storage in azure
- Login on azure portal portal.azure.com and go to home page.
- Go to storage account and click on it.
Here you will find your storage accounts that you have already created. Select the one and click on it.
- after open the storage account you will find different type of storage services like Blob service, queue service, file service and table service. I have already explained Blob and queue services in my previous article.
Now go to Table service and click on 'Tables'.
- After click on 'Tables' further click on '+Table' to click a new table. give the name of table and click on 'Ok'.
Steps on Managing Tables Using PowerShell
Step 1
Download and install Windows PowerShell on the system.
Step 2
Choose ‘Pin to taskbar’ by right-clicking on the ‘Windows PowerShell’. This will pin it to the taskbar of your computer.
Step 3
‘Run ISE as Administrator’ is to be choosen on this step.
Step for creating a Table
Step 1
Copy these commands given below and paste them into your screen. The highlighted text is to be replaced with your account.
Step 2
Now login into your account.
- $StorageAccountName = "csharpcorner"
- $StorageAccountKey = *******************************
- $Ctx = New-AzureStorageContext $StorageAccountName - StorageAccountKey
- $StorageAccountKey
Step 3
Go on creating a new table.
- $tabName = "Employee"
- New-AzureStorageTable –Name $tabName –Context $Ctx
Data can be retrieved, deleted and inserted into the table using preset commands in PowerShell.
Retrieve Table
- $tabName = "Employee"
- Get-AzureStorageTable –Name $tabName –Context $Ctx
Delete Table
- $tabName = "Employee"
- Remove-AzureStorageTable –Name $tabName –Context $Ctx
Insert rows into Table
- function Add-Entity() {
- [CmdletBinding()]
-
- param(
- $table,
- [String]$partitionKey,
- [String]$rowKey,
- [String]$name,
- [Int]$id,
- [String]$address,
- [String]$dpt
- )
-
- $entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity
- -ArgumentList $partitionKey, $rowKey
-
- $entity.Properties.Add("Name", $name)
- $entity.Properties.Add("ID", $id)
- $entity.Properties.Add("Address", $address)
- $entity.Properties.Add("Dpt", $dpt)
-
-
- $result = $table.CloudTable.Execute(
- [Microsoft.WindowsAzure.Storage.Table.TableOperation]
- ::Insert($entity))
- }
-
- $StorageAccountName = "csharpcorner"
- $StorageAccountKey = Get-AzureStorageKey -StorageAccountName $StorageAccountName
- $Ctx = New-AzureStorageContext $StorageAccountName - StorageAccountKey
- $StorageAccountKey.Primary
-
- $TableName = "Employee"
-
- $table = Get-AzureStorageTable –Name $TableName -Context $Ctx -ErrorAction Ignore
Retrieve Table Data
- $StorageAccountName = "csharpcorner"
- $StorageAccountKey = Get-AzureStorageKey - StorageAccountName $StorageAccountName
- $Ctx = New-AzureStorageContext – StorageAccountName $StorageAccountName -
- StorageAccountKey $StorageAccountKey.Primary;
-
- $TableName = "Employee"
-
- #Get a reference to a table.
- $table = Get-AzureStorageTable –Name $TableName -Context $Ctx
-
- #Create a table query.
- $query = New-Object Microsoft.WindowsAzure.Storage.Table.TableQuery
-
- #Define columns to select.
- $list = New-Object System.Collections.Generic.List[string]
- $list.Add("RowKey")
- $list.Add("ID")
- $list.Add("Name")
- $list.Add("Address")
- $list.Add("Dpt")
-
- #Set query details.
- $query.FilterString = "ID gt 0"
- $query.SelectColumns = $list
- $query.TakeCount = 20
-
- #Execute the query.
- $entities = $table.CloudTable.ExecuteQuery($query)
-
- #Display entity properties with the table format.
-
- $entities | Format-Table PartitionKey, RowKey, @{ Label = "Name";
- Expression={$_.Properties["Name"].StringValue}}, @{ Label = "ID";
- Expression={$_.Properties[“ID”].Int32Value}}, @{ Label = "Address";
- Expression={$_.Properties[“Address”].StringValue}}, @{ Label = "Dpt";
- Expression={$_.Properties[“Dpt”].StringValue}} -AutoSize
Delete Rows from Table
- $StorageAccountName = "csharpcorner"
-
- $StorageAccountKey = Get-AzureStorageKey - StorageAccountName $StorageAccountName
- $Ctx = New-AzureStorageContext – StorageAccountName $StorageAccountName -
- StorageAccountKey $StorageAccountKey.Primary
-
- #Retrieve the table.
- $TableName = "Csharpcorner"
- $table = Get-AzureStorageTable -Name $TableName -Context $Ctx -ErrorAction
- Ignore
-
- #If the table exists, start deleting its entities.
- if ($table -ne $null) {
- #Together the PartitionKey and RowKey uniquely identify every
- #entity within a table.
-
- $tableResult = $table.CloudTable.Execute(
- [Microsoft.WindowsAzure.Storage.Table.TableOperation]
- ::Retrieve(“Partition1”, "Row1"))
-
- $entity = $tableResult.Result;
-
- if ($entity -ne $null) {
- $table.CloudTable.Execute(
- [Microsoft.WindowsAzure.Storage.Table.TableOperation]
- ::Delete($entity))
- }
- }
This script will delete the first row from the table, as Partition1 and Row1 are clearly specified in the script. After you are done with deleting the row, check for the result by running the script for retrieving rows. The result you will be getting is that the first row is deleted.
Note
Please make sure before running these commands please that the account name is replaced with your account name and accountkey with your account key.
Steps to Manage Table using Azure Storage Explorer
Step 1
Log in to your Azure account.
Step 2
Now, look for your storage account.
Step 3
Click on the link for ‘Storage explorer’.
Step 4
Choose ‘Azure Storage Explorer for Windows’ from the list.
Note
Azure Storage Explorer for Windows is a free tool that can be downloaded and installed on the computer.
Step 5
Click on the ‘Add Account’ button at the top while running this program on your computer.
Step 6
Enter ‘Storage Account Name’ and ‘Storage account Key’ and click ‘Test Access.
Step 7
Existing ‘Tables' of the storage are in the left panel under tables. Click on the row to get the access.
Steps for Creating a Table
Step 1
Click on ‘New’ and enter the name of the table.
Insert Row into Table
Step 1
Click on ‘+Add' to add a field.
Step 2
Enter Field Name and value by click on add property.
Step 3
From the dropdown menu, select the data type and enter the field value.
Step 4
Click on insert
Azure Storage Explorer is a very easy interface developer as compared to writing lengthy scripts in Windows PowerShell. On, Azure Storage Explorer it is easy to upload, create, delete and download and manage the tables.
Summary
Hope you understand all the methods of creating table storage from the portal, using PowerShell and Azure storage explorer. Try these simple steps and let me know if any queries. You can check more articles on azure table storage here,
Thanks for reading. Have a good day.