能否通过REST API在Azure Analysis Service创建对象并执行TMSL脚本?
Absolutely—you can use the Azure Analysis Services (AAS) REST API to execute TMSL (Tabular Model Scripting Language) scripts for creating databases, tables, partitions, and other model objects. The confusion around only supporting refresh/sync operations is common, but the Execute endpoint is designed specifically for running TMSL commands like your createOrReplace example.
How to Execute Your TMSL Script via REST API
Here's a breakdown of the steps and request structure:
Use the
ExecuteAPI Endpoint
The core endpoint for running TMSL commands is:POST https://<your-aas-server-name>.asazure.windows.net/v1/models/<model-name>/executeReplace
<your-aas-server-name>with your AAS server's name, and<model-name>with the target model (omit/models/<model-name>if you're targeting the server level, like creating a new database).Mandatory Request Headers
You'll need these headers to authenticate and format the request:Authorization: Bearer token from Azure AD authentication (obtainable via Azure CLI, PowerShell, or an app registration).Content-Type:application/json
Request Body (Your TMSL Script)
Wrap your TMSL command in a JSON object with acommandsarray. Here's how your script would be structured for the API:{ "commands": [ { "createOrReplace": { "object": { "database": "AdventureWorksTabular1200", "table": "FactSalesQuota", "partition": "FactSalesQuota - 2011" }, "partition": { "name": "Sales Quota for 2011", "mode": "import", "dataView": "full", "source": { "query": [ "SELECT [dbo].[FactSalesQuota].* FROM [dbo].[FactSalesQuota]", "JOIN DimDate as DD", "on DD.DateKey = FactSalesQuota.DateKey", "WHERE DD.CalendarYear='2011'" ], "dataSource": "SqlServer localhost AdventureworksDW2016" } } } } ] }
Key Notes & Troubleshooting
- Permissions: Ensure your user or service principal has the appropriate AAS roles (e.g., Server Administrator, Database Administrator) to modify model objects—without these, the API will return a permission error.
- Asynchronous Execution: Schema-modifying TMSL commands (like creating partitions) often run asynchronously. The API response will include an
operationId; use theGetOperationStatusendpoint to verify completion:GET https://<your-aas-server-name>.asazure.windows.net/v1/operations/<operationId> - Script Validation: Double-check your TMSL syntax (e.g., correct data source name, valid SQL query) before executing—any errors will be detailed in the operation status response.
- Server-Level Operations: If you're creating a new database (not just objects within an existing one), use the server-level
Executeendpoint instead:POST https://<your-aas-server-name>.asazure.windows.net/v1/execute
Why You Might Have Thought Only Refresh/Sync Was Supported
Refresh and sync operations are more prominently documented, but the Execute endpoint is the flexible tool for all TMSL-based tasks, including schema modifications like creating tables and partitions.
内容的提问来源于stack exchange,提问作者Khorshed Alam

