透過 REST API 在 Microsoft Fabric 中建立 SQL 資料庫
✅適用於:Microsoft Fabric 中的 SQL 資料庫
Fabric 平臺有一組豐富的 REST API,可用來部署和管理資源。 這些 API 可用來部署 Fabric SQL 資料庫。 本文和範例腳本示範基本 PowerShell 腳本,可用來部署 Fabric SQL 資料庫,並將數據新增至其中。
必要條件
- 您需要的是現有 Fabric 容量。 如果您沒有,則開始試用 Fabric。
- 請務必 使用管理入口網站租用戶設定在 Fabric 中啟用 SQL 資料庫。
- 建立新的工作區 或使用現有的 Fabric 工作區。
- 您必須是工作區管理員或成員角色的成員,才能建立 SQL 資料庫。
- 安裝 SQLCMD 的 golang 版本。 在 Windows 上執行
winget install sqlcmd
以安裝。 如需其他操作系統,請參閱 aka.ms/go-sqlcmd。 - Az PowerShell 模組。 在 PowerShell 中執行
Install-Module az
以安裝。
透過 REST API 建立新的 SQL 資料庫
此範例文本會使用 Connect-AzAccount
的別名 az login
來提示輸入認證。 它會使用這些認證來取得存取令牌,以用於 REST API 呼叫。 SQLCMD 會使用提供給 Connect-AzAccount
的帳戶內容。
腳本會使用登入用戶的別名和日期來建立名為 的資料庫。 目前,REST API 不會傳回狀態,因此我們必須迴圈並檢查要建立的資料庫。 建立資料庫之後,會使用 SQLCMD 來建立某些物件,然後查詢其存在。 最後,我們會刪除資料庫。
在下列腳本中,將 取代 <your workspace id>
為您的 Fabric 工作區識別碼。
您可以在 URL 中輕鬆找到工作區的識別碼,這是瀏覽器視窗中之後/
兩/groups/
個字元內的唯一字串。 例如, 11aa111-a11a-1111-1abc-aa1111aaaa
在中 https://fabric.microsoft.com/groups/11aa111-a11a-1111-1abc-aa1111aaaa/
。
此文稿示範:
- 使用 Get-AzAccessToken 取得存取令牌 ,並 將其從安全字串轉換。 如果使用 PowerShell 7,ConvertFrom-SecureString 也是一個選項。
- 使用 專案建立新的 SQL 資料庫 - 建立專案 API。
- 列出 Fabric 工作區中的所有 SQL 資料庫。
- 使用 SQLCMD 連線到資料庫, 以執行腳本來建立物件。
- 使用 項目移除資料庫 - 刪除專案 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)
}