Bagikan melalui


Membuat database SQL dengan REST API untuk Microsoft Fabric

Berlaku untuk:Database SQL di Microsoft Fabric

Anda dapat menggunakan Fabric REST API untuk menyebarkan dan mengelola sumber daya, termasuk database SQL di Fabric.

Artikel ini dan contoh skrip menunjukkan cara menggunakan PowerShell untuk memanggil Fabric REST API untuk menyebarkan database Fabric SQL.

Prasyarat

Membuat database SQL baru melalui REST API

Contoh skrip ini menggunakan Connect-AzAccount, alias az login untuk meminta kredensial. Ini menggunakan kredensial tersebut untuk mendapatkan token akses yang akan digunakan untuk panggilan REST API. SQLCMD menggunakan konteks akun yang diberikan kepada Connect-AzAccount.

Skrip membuat database bernama dengan alias pengguna yang masuk dan tanggal. Saat ini, REST API tidak mengembalikan status sehingga kita harus mengulang dan memeriksa database yang akan dibuat. Setelah database dibuat, SQLCMD digunakan untuk membuat beberapa objek lalu mengkueri keberadaannya. Akhirnya, kita menghapus database.

Dalam skrip berikut, ganti <your workspace id> dengan ID ruang kerja Fabric Anda. Anda dapat menemukan ID ruang kerja dengan mudah di URL, yang merupakan rangkaian unik antara dua karakter / setelah /groups/ di jendela penelusur Anda. Misalnya, 11aa111-a11a-1111-1abc-aa1111aaaa di https://fabric.microsoft.com/groups/11aa111-a11a-1111-1abc-aa1111aaaa/.

Skrip ini menunjukkan:

  1. Ambil token akses menggunakan Get-AzAccessToken dan mengonversinya dari string aman. Jika menggunakan PowerShell 7, ConvertFrom-SecureString juga merupakan opsi.
  2. Buat database SQL baru menggunakan Item - Buat ITEM API.
  3. Mencantumkan semua database SQL di ruang kerja Fabric.
  4. Sambungkan ke database dengan SQLCMD untuk menjalankan skrip untuk membuat objek.
  5. Hapus database menggunakan Items - Delete Item API.
Import-Module Az.Accounts

az login

$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 
    );
    insert into test2 values (1);
    insert into test2 values (2);
    insert into test2 values (3);
    select * from test2;' 

    #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)
}