Skip to content

Latest commit

 

History

History
196 lines (143 loc) · 6.11 KB

storage-table-entities-powershell-include.md

File metadata and controls

196 lines (143 loc) · 6.11 KB
author ms.service ms.topic ms.date ms.author
tamram
storage
include
10/26/2018
tamram

Managing table entities

Now that you have a table, let's look at how to manage entities, or rows, in the table.

An entity can have up to 255 properties, including 3 system properties: PartitionKey, RowKey, and Timestamp. You are responsible for inserting and updating the values of PartitionKey and RowKey. The server manages the value of Timestamp, which cannot be modified. Together the PartitionKey and RowKey uniquely identify every entity within a table.

  • PartitionKey: Determines the partition that the entity is stored in.
  • RowKey: Uniquely identifies the entity within the partition.

You may define up to 252 custom properties for an entity.

Add table entities

Add entities to a table using Add-StorageTableRow. These examples use partition keys with values "partition1" and "partition2", and row keys equal to state abbreviations. The properties in each entity are username and userid.

$partitionKey1 = "partition1"
$partitionKey2 = "partition2"

# add four rows 
Add-StorageTableRow `
    -table $storageTable `
    -partitionKey $partitionKey1 `
    -rowKey ("CA") -property @{"username"="Chris";"userid"=1}

Add-StorageTableRow `
    -table $storageTable `
    -partitionKey $partitionKey2 `
    -rowKey ("NM") -property @{"username"="Jessie";"userid"=2}

Add-StorageTableRow `
    -table $storageTable `
    -partitionKey $partitionKey1 `
    -rowKey ("WA") -property @{"username"="Christine";"userid"=3}

Add-StorageTableRow `
    -table $storageTable `
    -partitionKey $partitionKey2 `
    -rowKey ("TX") -property @{"username"="Steven";"userid"=4}

Query the table entities

There are several different ways to query the entities in a table.

Retrieve all entities

To retrieve all entities, use Get-AzureStorageTableRowAll.

Get-AzureStorageTableRowAll -table $storageTable | ft

This command yields results similar to the following table:

userid username partition rowkey
1 Chris partition1 CA
3 Christine partition1 WA
2 Jessie partition2 NM
4 Steven partition2 TX

Retrieve entities for a specific partition

To retrieve all entities in a specific partition, use Get-AzureStorageTableRowByPartitionKey.

Get-AzureStorageTableRowByPartitionKey -table $storageTable -partitionKey $partitionKey1 | ft

The results look similar to the following table:

userid username partition rowkey
1 Chris partition1 CA
3 Christine partition1 WA

Retrieve entities for a specific value in a specific column

To retrieve entities where the value in a specific column equals a particular value, use Get-AzureStorageTableRowByColumnName.

Get-AzureStorageTableRowByColumnName -table $storageTable `
    -columnName "username" `
    -value "Chris" `
    -operator Equal

This query retrieves one record.

field value
userid 1
username Chris
PartitionKey partition1
RowKey CA

Retrieve entities using a custom filter

To retrieve entities using a custom filter, use Get-AzureStorageTableRowByCustomFilter.

Get-AzureStorageTableRowByCustomFilter `
    -table $storageTable `
    -customFilter "(userid eq 1)"

This query retrieves one record.

field value
userid 1
username Chris
PartitionKey partition1
RowKey CA

Updating entities

There are three steps for updating entities. First, retrieve the entity to change. Second, make the change. Third, commit the change using Update-AzureStorageTableRow.

Update the entity with username = 'Jessie' to have username = 'Jessie2'. This example also shows another way to create a custom filter using .NET types.

# Create a filter and get the entity to be updated.
[string]$filter = `
    [Microsoft.WindowsAzure.Storage.Table.TableQuery]::GenerateFilterCondition("username",`
    [Microsoft.WindowsAzure.Storage.Table.QueryComparisons]::Equal,"Jessie")
$user = Get-AzureStorageTableRowByCustomFilter `
    -table $storageTable `
    -customFilter $filter

# Change the entity.
$user.username = "Jessie2" 

# To commit the change, pipe the updated record into the update cmdlet.
$user | Update-AzureStorageTableRow -table $storageTable 

# To see the new record, query the table.
Get-AzureStorageTableRowByCustomFilter -table $storageTable `
    -customFilter "(username eq 'Jessie2')"

The results show the Jessie2 record.

field value
userid 2
username Jessie2
PartitionKey partition2
RowKey NM

Deleting table entities

You can delete one entity or all entities in the table.

Deleting one entity

To delete a single entity, get a reference to that entity and pipe it into Remove-AzureStorageTableRow.

# Set filter.
[string]$filter = `
  [Microsoft.WindowsAzure.Storage.Table.TableQuery]::GenerateFilterCondition("username",`
  [Microsoft.WindowsAzure.Storage.Table.QueryComparisons]::Equal,"Jessie2")

# Retrieve entity to be deleted, then pipe it into the remove cmdlet.
$userToDelete = Get-AzureStorageTableRowByCustomFilter `
    -table $storageTable `
    -customFilter $filter
$userToDelete | Remove-AzureStorageTableRow -table $storageTable 

# Retrieve entities from table and see that Jessie2 has been deleted.
Get-AzureStorageTableRowAll -table $storageTable | ft

Delete all entities in the table

To delete all entities in the table, you retrieve them and pipe the results into the remove cmdlet.

# Get all rows and pipe the result into the remove cmdlet.
Get-AzureStorageTableRowAll `
    -table $storageTable | Remove-AzureStorageTableRow -table $storageTable 

# List entities in the table (there won't be any).
Get-AzureStorageTableRowAll -table $storageTable | ft