Microsoft-azure-tables

提供:Dev Guides
移動先:案内検索

Microsoft Azure-テーブル

ここにテーブルを保存しても、リレーショナルデータベースは意味しません。 Azure Storageは、外部キーや他の種類のリレーションなしでテーブルのみを保存できます。 これらのテーブルは拡張性が高く、大量のデータを処理するのに最適です。 テーブルを保存して、大量のデータを照会できます。 リレーショナルデータベースは、別のサービスであるSQL Data Servicesを使用して保存できます。

サービスの3つの主要な部分は-

  • テーブル
  • 実体
  • プロパティ

たとえば、「Book」がエンティティの場合、そのプロパティはId、Title、Publisher、Authorなどになります。 エンティティのコレクションに対してテーブルが作成されます。 252のカスタムプロパティと3つのシステムプロパティがあります。 エンティティには、常にPartitionKey、RowKey、およびTimestampのシステムプロパティがあります。 タイムスタンプはシステムで生成されますが、テーブルにデータを挿入するときにPartitionKeyとRowKeyを指定する必要があります。 以下の例では、より明確になります。 テーブル名とプロパティ名は大文字と小文字が区別されます。テーブルを作成する際は常にこれらを考慮する必要があります。

PowerShellを使用してテーブルを管理する方法

  • ステップ1 *-チュートリアルで前述したように、Windows PowerShellをダウンロードしてインストールします。
  • ステップ2 *-「Windows PowerShell」を右クリックし、「タスクバーにピン留め」を選択してコンピューターのタスクバーにピン留めします。
  • ステップ3 *-「管理者としてISEを実行」を選択します。

テーブルを作成する

  • ステップ1 *-次のコマンドをコピーして、画面に貼り付けます。 強調表示されたテキストをアカウントに置き換えます。
  • ステップ2 *-アカウントにログインします。
$StorageAccountName = "mystorageaccount"
$StorageAccountKey = "mystoragekey"
$Ctx = New-AzureStorageContext $StorageAccountName - StorageAccountKey
$StorageAccountKey
  • ステップ3 *-新しいテーブルを作成します。
$tabName = "Mytablename"
New-AzureStorageTable –Name $tabName –Context $Ctx

次の画像は、「book」という名前で作成されたテーブルを示しています。

テーブルの作成

結果として、次のエンドポイントが与えられていることがわかります。

https://finddevguides.table.core.windows.net/Book

同様に、PowerShellのプリセットコマンドを使用して、データを取得、削除、およびテーブルに挿入できます。

テーブルを取得

$tabName = "Book"
Get-AzureStorageTable –Name $tabName –Context $Ctx

テーブルを削除

$tabName = "Book"
Remove-AzureStorageTable –Name $tabName –Context $Ctx

テーブルに行を挿入

function Add-Entity() {
   [CmdletBinding()]

   param(
      $table,
      [String]$partitionKey,
      [String]$rowKey,
      [String]$title,
      [Int]$id,
      [String]$publisher,
      [String]$author
   )

   $entity = New-Object -TypeName Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity
      -ArgumentList $partitionKey, $rowKey

   $entity.Properties.Add("Title", $title)
   $entity.Properties.Add("ID", $id)
   $entity.Properties.Add("Publisher", $publisher)
   $entity.Properties.Add("Author", $author)


   $result = $table.CloudTable.Execute(
      [Microsoft.WindowsAzure.Storage.Table.TableOperation]
      ::Insert($entity))
}

$StorageAccountName = "finddevguides"
$StorageAccountKey = Get-AzureStorageKey -StorageAccountName $StorageAccountName
$Ctx = New-AzureStorageContext $StorageAccountName - StorageAccountKey
   $StorageAccountKey.Primary

$TableName = "Book"

$table = Get-AzureStorageTable –Name $TableName -Context $Ctx -ErrorAction Ignore

#Add multiple entities to a table.
Add-Entity -Table $table -PartitionKey Partition1 -RowKey Row1 -Title .Net -Id 1
   -Publisher abc -Author abc
Add-Entity -Table $table -PartitionKey Partition2 -RowKey Row2 -Title JAVA -Id 2
   -Publisher abc -Author abc
Add-Entity -Table $table -PartitionKey Partition3 -RowKey Row3 -Title PHP -Id 3
   -Publisher xyz -Author xyz
Add-Entity -Table $table -PartitionKey Partition4 -RowKey Row4 -Title SQL -Id 4
   -Publisher xyz -Author xyz

テーブルデータの取得

$StorageAccountName = "finddevguides"
$StorageAccountKey = Get-AzureStorageKey - StorageAccountName $StorageAccountName
$Ctx = New-AzureStorageContext – StorageAccountName $StorageAccountName -
   StorageAccountKey $StorageAccountKey.Primary;

$TableName = "Book"

#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("Title")
$list.Add("Publisher")
$list.Add("Author")

#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 = "Title";
Expression={$_.Properties["Title"].StringValue}}, @{ Label = "ID";
Expression={$_.Properties[“ID”].Int32Value}}, @{ Label = "Publisher";
Expression={$_.Properties[“Publisher”].StringValue}}, @{ Label = "Author";
Expression={$_.Properties[“Author”].StringValue}} -AutoSize

出力は次の画像のようになります。

テーブルの取得

テーブルから行を削除

$StorageAccountName = "finddevguides"

$StorageAccountKey = Get-AzureStorageKey - StorageAccountName $StorageAccountName
$Ctx = New-AzureStorageContext – StorageAccountName $StorageAccountName -
   StorageAccountKey $StorageAccountKey.Primary

#Retrieve the table.
$TableName = "Book"
$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))
   }
}

上記のスクリプトは、スクリプトでPartition1とRow1を指定したことがわかるように、テーブルから最初の行を削除します。 行の削除が完了したら、行を取得するスクリプトを実行して結果を確認できます。 ここで、最初の行が削除されていることがわかります。

これらのコマンドの実行中に、アカウント名をアカウント名に、アカウントキーをアカウントキーに置き換えたことを確認してください。

Azure Storage Explorerを使用してテーブルを管理する方法

  • ステップ1 *-Azureアカウントにログインして、ストレージアカウントに移動します。
  • ステップ2 *-次の画像の紫色の円で示されているリンク「ストレージエクスプローラ」をクリックします。

ストレージエクスプローラー

  • ステップ3 *-リストから「Azure Storage Explorer for Windows」を選択します。 これは、コンピューターにダウンロードしてインストールできる無料のツールです。
  • ステップ4 *-コンピューターでこのプログラムを実行し、上部の[アカウントの追加]ボタンをクリックします。
  • ステップ5 *-「ストレージアカウント名」と「ストレージアカウントキー」を入力して、「テストアクセス」をクリックします。 次の画像では、ボタンが丸で囲まれています。

ストレージアカウント名

  • ステップ6 *-ストレージに既にテーブルがある場合は、左側のパネルの[テーブル]の下に表示されます。 行をクリックして表示できます。

テーブルを作成する

  • ステップ1 *-「新規」をクリックして、次の図に示すようにテーブル名を入力します。

新しいテーブルの作成

テーブルに行を挿入

  • ステップ1 *-「新規」をクリックします。
  • ステップ2 *-フィールド名を入力します。
  • ステップ3 *-ドロップダウンからデータ型を選択し、フィールド値を入力します。

ドロップダウンからデータを選択

  • ステップ4 *-作成された行を表示するには、左パネルのテーブル名をクリックします。

Azure Storage Explorerは、テーブルを管理するための非常に基本的で簡単なインターフェイスです。 このインターフェイスを使用して、テーブルを簡単に作成、削除、アップロード、およびダウンロードできます。 これにより、Windows PowerShellで長いスクリプトを作成するのに比べて、開発者はタスクを非常に簡単に行えます。