提示
本文中的内容适用于 Azure 表存储。 但是,现在提供了表存储高级版本(即 Azure Cosmos DB for Table),该版本提供吞吐量优化表、全局分发和自动辅助索引。 若要详细了解和尝试高级体验,请查看 Azure Cosmos DB for Table。 本文中的编程语言在高级版中尚不受支持,但在将来会添加该支持。
Azure 表存储是一种 NoSQL 数据存储,可用于存储和查询大量的结构化非关系型数据。 该服务的主要组件包括表、实体和属性。 表是实体的集合。 实体是一组属性。 每个实体最多可以有 252 个属性(都是一些名称-值对)。 本文假设用户熟知 Azure 表存储服务的概念。 有关详细信息,请参阅 Understanding the Table Service Data Model(了解表服务数据模型)和通过 .NET 开始使用 Azure 表存储。
此操作指南文章介绍常见的 Azure 表存储操作。 你将学习如何执行以下操作:
- 创建表
- 检索表
- 添加表实体
- 查询表
- 删除表实体
- 删除表
本操作指南文章介绍如何在新的资源组中创建新存储帐户,以便你可以在创建完成后轻松删除。 也可以使用现有的存储帐户。
这些示例需要 Az PowerShell 模块 Az.Storage (1.1.0 or greater)
和 Az.Resources (1.2.0 or greater)
。 在 PowerShell 窗口中,运行 Get-Module -ListAvailable Az*
可查找版本。 如果未显示任何信息或需要升级,请参阅安装 Azure PowerShell 模块。
重要
使用 PowerShell 的 Azure 功能必须已安装 Az
模块。 AzTable
的当前版本与较旧的 AzureRM 模块不兼容。 如果需要,请遵循用于安装 Az 模块的最新安装说明。
出于模块名称兼容性原因,我们也在 PowerShell 库中以旧名称 AzureRmStorageTables
发布此模块。 此文档将仅引用新名称。
安装或更新 Azure PowerShell 后,必须安装模块 AzTable,其中包含用于管理实体的命令。 若要安装此模块,请以管理员身份运行 PowerShell 并使用 Install-Module 命令。
Install-Module AzTable
授权表数据操作
AzTable PowerShell 模块支持通过共享密钥授权使用帐户访问密钥进行授权。 本文中的示例演示如何通过共享密钥来授权表数据操作。
Azure 表存储支持使用 Microsoft Entra ID 进行授权。 但是,AzTable PowerShell 模块并非原生支持使用 Microsoft Entra ID 进行授权。 将 Microsoft Entra ID 与 AzTable 模块配合使用需要从 PowerShell 调用 .NET 客户端库中的方法。
登录 Azure
若要开始,请运行 Add-AzAccount
命令以登录 Azure 订阅,并按照屏幕上的说明操作。
Add-AzAccount -Environment AzureChinaCloud
检索位置列表
如果你不知道要使用哪个位置,可以列出可用的位置。 显示列表后,找到要使用的位置。 这些示例使用 chinaeast。 将此值存储在变量 location 中,以供以后使用。
Get-AzLocation | select Location
$location = "chinaeast"
创建资源组
使用 New-AzResourceGroup 命令创建资源组。
Azure 资源组是在其中部署和管理 Azure 资源的逻辑容器。 将资源组名称存储在变量中,以供以后使用。 本示例在 chinaeast 区域中创建名为 pshtablesrg 的资源组。
$resourceGroup = "pshtablesrg"
New-AzResourceGroup -ResourceGroupName $resourceGroup -Location $location
创建存储帐户
使用 New-AzStorageAccount 创建具有本地冗余存储 (LRS) 的标准常规用途存储帐户。 请确保指定一个唯一的存储帐户名称。 接下来,获取表示存储帐户的上下文。 对存储帐户执行操作时,你可以引用上下文而不需要重复提供凭据。
$storageAccountName = "pshtablestorage"
$storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup `
-Name $storageAccountName `
-Location $location `
-SkuName Standard_LRS `
-Kind Storage
$ctx = $storageAccount.Context
创建新表
若要创建表,请使用 New-AzStorageTable cmdlet。 在本示例中,表名为 pshtesttable
。
$tableName = "pshtesttable"
New-AzStorageTable -Name $tableName -Context $ctx
在存储帐户中检索表列表
使用 Get-AzStorageTable 在存储帐户中检索表列表。
Get-AzStorageTable -Context $ctx | select Name
检索对特定表的引用
若要对表执行操作,需要引用特定表。 使用 Get-AzStorageTable 获取引用。
$storageTable = Get-AzStorageTable -Name $tableName -Context $ctx
引用特定表的 CloudTable 属性
重要
通过 AzTable PowerShell 模块处理表数据时,必须使用 CloudTable 属性。 调用 Get-AzStorageTable 命令来获取对此对象的引用。
若要使用 AzTable 对表执行操作,请返回对特定表的 CloudTable 属性的引用。 CloudTable 属性公开可用于从 PowerShell 管理表数据的 .NET 方法。
$cloudTable = $storageTable.CloudTable
管理表实体
至此,你已创建了一个表,接下来让我们了解如何管理表中的实体或行。
实体最多可以有 255 个属性,其中包括 3 个系统属性:PartitionKey、RowKey 和 Timestamp 。 你负责插入和更新 PartitionKey 与 RowKey 的值 。 服务器负责管理 Timestamp 的值,该值不可修改。 PartitionKey 和 RowKey 共同唯一标识表中的每个实体。
- PartitionKey:确定实体存储在其中的分区。
- RowKey:唯一标识分区内的实体。
最多可为一个实体定义 252 个自定义属性。
添加表实体
使用 Add-AzTableRow 向表中添加实体。 这些示例使用值为 partition1
和 partition2
的分区键,且行键等于状态缩写。 每个实体中的属性为 username
和 userid
。
$partitionKey1 = "partition1"
$partitionKey2 = "partition2"
# add four rows
Add-AzTableRow `
-table $cloudTable `
-partitionKey $partitionKey1 `
-rowKey ("CA") -property @{"username"="Chris";"userid"=1}
Add-AzTableRow `
-table $cloudTable `
-partitionKey $partitionKey2 `
-rowKey ("NM") -property @{"username"="Jessie";"userid"=2}
Add-AzTableRow `
-table $cloudTable `
-partitionKey $partitionKey1 `
-rowKey ("WA") -property @{"username"="Christine";"userid"=3}
Add-AzTableRow `
-table $cloudTable `
-partitionKey $partitionKey2 `
-rowKey ("TX") -property @{"username"="Steven";"userid"=4}
查询表实体
你可以使用 Get-AzTableRow 命令查询表中的实体。
注意
Get-AzureStorageTableRowAll、Get-AzureStorageTableRowByPartitionKey、Get-AzureStorageTableRowByColumnName 和 Get-AzureStorageTableRowByCustomFilter cmdlet 已弃用,将在未来的版本更新中删除 。
检索所有实体
Get-AzTableRow -table $cloudTable | ft
此命令得到的结果类似于下表所示:
userid | username | partition | rowkey |
---|---|---|---|
1 | Chris | partition1 | CA |
3 | Christine | partition1 | WA |
2 | Jessie | partition2 | NM |
4 | Steven | partition2 | TX |
检索表中实体的计数
$totalEntities=(Get-AzTableRow -table $cloudTable | measure).Count
Echo $totalEntities
此命令会生成类似于以下示例的实体计数:
4
检索特定分区键的实体
Get-AzTableRow -table $cloudTable -partitionKey $partitionKey1 | ft
结果类似于下表所示:
userid | username | partition | rowkey |
---|---|---|---|
1 | Chris | partition1 | CA |
3 | Christine | partition1 | WA |
在特定列中检索具有特定值的实体
Get-AzTableRow -table $cloudTable `
-columnName "username" `
-value "Chris" `
-operator Equal
此查询将检索一条记录。
field | value |
---|---|
userid | 1 |
username | Chris |
PartitionKey | partition1 |
RowKey | CA |
使用自定义筛选器检索实体
Get-AzTableRow `
-table $cloudTable `
-customFilter "(userid eq 1)"
此查询将检索一条记录。
field | value |
---|---|
userid | 1 |
username | Chris |
PartitionKey | partition1 |
RowKey | CA |
更新实体
更新实体有三个步骤。 第一步,检索要更改的实体。 第二步,进行更改。 第三步,使用 Update-AzTableRow 提交更改。
将 username = 'Jessie' 的实体更改为 username = 'Jessie2'。 此示例还介绍了使用 .NET 类型创建自定义筛选器的另一种方法。
# Create a filter and get the entity to be updated.
[string]$filter = `
[Microsoft.Azure.Cosmos.Table.TableQuery]::GenerateFilterCondition("username",`
[Microsoft.Azure.Cosmos.Table.QueryComparisons]::Equal,"Jessie")
$user = Get-AzTableRow `
-table $cloudTable `
-customFilter $filter
# Change the entity.
$user.username = "Jessie2"
# To commit the change, pipe the updated record into the update cmdlet.
$user | Update-AzTableRow -table $cloudTable
# To see the new record, query the table.
Get-AzTableRow -table $cloudTable `
-customFilter "(username eq 'Jessie2')"
结果将显示 Jessie2 记录。
field | value |
---|---|
userid | 2 |
username | Jessie2 |
PartitionKey | partition2 |
RowKey | NM |
删除表实体
可以删除表中的一个实体或所有实体。
删除一个实体
若要删除单个实体,请获取对该实体的引用并将其传递到 Remove-AzTableRow。
# Set filter.
[string]$filter = `
[Microsoft.Azure.Cosmos.Table.TableQuery]::GenerateFilterCondition("username",`
[Microsoft.Azure.Cosmos.Table.QueryComparisons]::Equal,"Jessie2")
# Retrieve entity to be deleted, then pipe it into the remove cmdlet.
$userToDelete = Get-AzTableRow `
-table $cloudTable `
-customFilter $filter
$userToDelete | Remove-AzTableRow -table $cloudTable
# Retrieve entities from table and see that Jessie2 has been deleted.
Get-AzTableRow -table $cloudTable | ft
删除表中的所有实体
若要删除表中的所有实体,请检索所有实体,并将结果通过管道传递到删除 cmdlet。
# Get all rows and pipe the result into the remove cmdlet.
Get-AzTableRow `
-table $cloudTable | Remove-AzTableRow -table $cloudTable
# List entities in the table (there won't be any).
Get-AzTableRow -table $cloudTable | ft
删除表
若要删除表,请使用 Remove-AzStorageTable。 此 cmdlet 将删除表,包括表中的所有数据。
Remove-AzStorageTable -Name $tableName -Context $ctx
# Retrieve the list of tables to verify the table has been removed.
Get-AzStorageTable -Context $Ctx | select Name
清理资源
如果在本操作指南开始时创建了新的资源组和存储帐户,则可以通过删除资源组来删除在本练习中创建的所有资产。 该命令会删除包含在组中的所有资源以及资源组本身。
Remove-AzResourceGroup -Name $resourceGroup
后续步骤
本操作指南文章介绍了使用 PowerShell 执行常见的 Azure 表存储操作,其中包括如何:
- 创建表
- 检索表
- 添加表实体
- 查询表
- 删除表实体
- 删除表
有关详细信息,请参阅以下文章:
从 PowerShell 使用 Azure 表 - AzureRmStorageTable/AzTable PS 模块 v2.0
Microsoft Azure 存储资源管理器是 Microsoft 免费提供的独立应用,适用于在 Windows、macOS 和 Linux 上以可视方式处理 Azure 存储数据。