次の方法で共有


REST API を使用して Microsoft Fabric SQL Database を作成する

適用対象:✅Microsoft Fabric SQL Database

Fabric プラットフォームには、リソースのデプロイと管理に使用できる豊富な REST API のセットがあります。 これらの API を使用して、Fabric SQL データベースをデプロイできます。 この記事とサンプル スクリプトでは、Fabric SQL データベースをデプロイしてデータを追加するために使用できる基本的な PowerShell スクリプトを示します。

前提条件

REST API を使用して新しい SQL データベースを作成する

このサンプル スクリプトでは、Connect-AzAccount のエイリアスである az login を使用して資格情報の入力を求めます。 これらの資格情報を使用して、REST API 呼び出しに使用するアクセス トークンを取得します。 SQLCMD は、Connect-AzAccount に指定されたアカウントのコンテキストを使用します。

このスクリプトは、ログインしたユーザーのエイリアスと日付で名前が付けられたデータベースを作成します。 現在、REST API は状態を返さないため、ループしてデータベースが作成されているかどうかを確認する必要があります。 データベースが作成されると、SQLCMD を使用していくつかのオブジェクトが作成され、それらの存在が照会されます。 最後に、データベースを削除します。

次のスクリプトでは、<your workspace id> を Fabric ワークスペース ID に置き換えます。 ワークスペースの ID は URL で簡単に見つけることができます。これは、ブラウザー ウィンドウの / の後の 2 つの /groups/ 文字内の一意の文字列です。 たとえば、11aa111-a11a-1111-1abc-aa1111aaaahttps://fabric.microsoft.com/groups/11aa111-a11a-1111-1abc-aa1111aaaa/ です。

このスクリプトでは以下を示します。

  1. Get-AzAccessToken を使用してアクセス トークンを取得し、それを SecureString から変換します。 PowerShell 7 を使用している場合は、ConvertFrom-SecureString もオプションです。
  2. アイテム - アイテムの作成 API を使用して、新しい SQL データベースを作成します。
  3. Fabric ワークスペース内のすべての SQL データベースを一覧表示します。
  4. SQLCMD を使用してデータベースに接続し、スクリプトを実行してオブジェクトを作成します。
  5. アイテム - アイテムの削除 API を使用してデータベースを削除します。
Import-Module Az.Accounts

Connect-AzAccount

$workspaceid = '<your workspace id>'

$databaseid = $null 
$headers = $null
$responseHeaders = $null 

# 1. Get the access token and add it to the headers

$access_token = (Get-AzAccessToken -AsSecureString -ResourceUrl https://api.fabric.microsoft.com) 

$ssPtr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($access_token.Token)

try {

    $headers = @{ 
       Authorization = $access_token.Type + ' ' + ([System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ssPtr))
    }

    $access_token.UserId -match('^[^@]+') | Out-Null

    # 2. Create the database and wait for it to be created.

    $body = @{
        displayName = $matches[0] + (Get-Date -Format "MMddyyyy")
        type = "SQLDatabase"
        description = "Created using public api"
    }

    $parameters = @{
        Method="Post"
        Headers=$headers
        ContentType="application/json"
        Body=($body | ConvertTo-Json)
        Uri = 'https://api.fabric.microsoft.com/v1/workspaces/' + $workspaceid + '/items'
    }

    Invoke-RestMethod @parameters -ErrorAction Stop

    $databases = (Invoke-RestMethod -Headers $headers -Uri https://api.fabric.microsoft.com/v1/workspaces/$($workspaceid)/SqlDatabases).value
    $databaseid = $databases.Where({$_.displayName -eq $body.displayName}).id

    While($databaseid -eq $null)
    {
        Write-Host 'Waiting on database create.'
        Start-Sleep 30
        $databases = (Invoke-RestMethod -Headers $headers -Uri https://api.fabric.microsoft.com/v1/workspaces/$($workspaceid)/SqlDatabases).value
        $databaseid = $databases.Where({$_.displayName -eq $body.displayName}).id
    }

    # 3. List all SQL databases in a Fabric workspace

    Write-Host 'Listing databases in workspace.'

    Invoke-RestMethod -Headers $headers -Uri https://api.fabric.microsoft.com/v1/workspaces/$($workspaceid)/items?type=SQlDatabase | select -ExpandProperty Value | ft

    $databaseProperties = (Invoke-RestMethod -Headers $headers -Uri https://api.fabric.microsoft.com/v1/workspaces/$($workspaceid)/SqlDatabases/$($databaseid) | select -ExpandProperty Properties)

    #4. Connnect to the database and create a table

    Write-Host 'Attempting to connect to the database.'

    sqlcmd.exe -S $databaseProperties.ServerFqdn -d $databaseProperties.DatabaseName -G -Q 'create table test2 
       ( 
       id int 
       )' 

    #5. Delete the database

    $parameters = @{
        Method="Delete"
        Headers=$headers
        ContentType="application/json"
        Body=($body | ConvertTo-Json)
        Uri = 'https://api.fabric.microsoft.com/v1/workspaces/' + $workspaceid + '/items/' + $databaseid
    }

    Invoke-RestMethod @parameters

    Write-Output 'Cleaned up:' $body.displayName
 
 } finally {
    # The following lines ensure that sensitive data is not left in memory.
    $headers = [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
    $parameters = [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ssPtr)
}