通过 REST API 创建 Microsoft Fabric SQL 数据库

适用于:Microsoft Fabric SQL 数据库

Fabric 平台有一组丰富的 REST API,可用于部署和管理资源。 这些 API 可用于部署 Fabric SQL 数据库。 本文和示例脚本演示了可用于部署 Fabric SQL 数据库并向其添加数据的基本 PowerShell 脚本。

先决条件

通过 REST API 创建新的 SQL 数据库

此示例脚本使用 Connect-AzAccountaz login 的别名)来提示输入凭据。 它使用这些凭据获取用于 REST API 调用的访问令牌。 SQLCMD 使用提供给 Connect-AzAccount 的帐户的上下文。

该脚本会创建一个以登录用户的别名和日期命名的数据库。 目前,REST API 不会返回状态,因此我们必须循环检查要创建的数据库。 创建数据库后,SQLCMD 用于创建一些对象,然后查询这些对象是否存在。 最后,删除数据库。

在以下脚本中,将 <your workspace id> 替换为你的 Fabric 工作区 ID。 可以在 URL 中轻松找到工作区的 ID,它是浏览器窗口中 / 后两个 /groups/ 字符内的唯一字符串。 例如,11aa111-a11a-1111-1abc-aa1111aaaa 中的 https://fabric.microsoft.com/groups/11aa111-a11a-1111-1abc-aa1111aaaa/

此脚本演示:

  1. 使用 Get-AzAccessToken 检索访问令牌,并将其从安全字符串形式进行转换。 如果使用 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)
}