You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Rest API在Azure Synapse Analytics项目中创建Pipeline

.NET环境下通过REST API创建Azure Synapse Analytics Pipeline 操作步骤
  • 步骤1:准备必要的身份验证信息

    调用Azure Synapse REST API前需先获取Azure AD的访问令牌,你需要提前准备以下参数:

    • 订阅ID、资源组名称、Synapse工作区名称
    • 具备Synapse工作区操作权限的Azure AD应用的客户端ID、客户端密钥、租户ID
      在.NET中可通过HttpClient请求Azure AD令牌端点获取Bearer令牌,请求信息如下:

    端点地址:https://login.microsoftonline.com/{租户ID}/oauth2/v2.0/token
    请求方法:POST
    请求Body参数(application/x-www-form-urlencoded格式):
    grant_type=client_credentials&client_id={客户端ID}&client_secret={客户端密钥}&scope=https://dev.azuresynapse.net/.default
    响应结果中的access_token字段即为后续请求需要携带的身份令牌。

  • 步骤2:构造Pipeline创建请求

    创建Pipeline的REST API请求规则如下:

    端点地址:PUT https://{Synapse工作区名称}.dev.azuresynapse.net/pipelines/{自定义Pipeline名称}?api-version=2020-12-01
    请求方法:PUT
    请求头:

    • Authorization: Bearer {步骤1获取的access_token}
    • Content-Type: application/json
      请求Body为Pipeline的定义JSON,示例如下:
    {
      "name": "test_pipeline",
      "properties": {
        "description": "通过REST API创建的测试Pipeline",
        "activities": [
          {
            "name": "WaitActivity1",
            "type": "Wait",
            "typeProperties": {
              "waitTimeInSeconds": 1
            }
          }
        ]
      }
    }
    
  • 步骤3:在.NET中发送请求

    直接使用.NET原生的HttpClient类发送请求即可,无需引入Synapse相关SDK,示例代码片段如下:

    using System.Net.Http;
    using System.Text;
    using System.Text.Json;
    
    // 1. 实例化HttpClient
    var httpClient = new HttpClient();
    
    // 2. 构造获取令牌的请求
    var tokenReqContent = new FormUrlEncodedContent(new Dictionary<string, string>
    {
        {"grant_type", "client_credentials"},
        {"client_id", "你的客户端ID"},
        {"client_secret", "你的客户端密钥"},
        {"scope", "https://dev.azuresynapse.net/.default"}
    });
    var tokenResp = await httpClient.PostAsync("https://login.microsoftonline.com/你的租户ID/oauth2/v2.0/token", tokenReqContent);
    tokenResp.EnsureSuccessStatusCode();
    var tokenJson = JsonSerializer.Deserialize<JsonElement>(await tokenResp.Content.ReadAsStringAsync());
    var accessToken = tokenJson.GetProperty("access_token").GetString();
    
    // 3. 构造创建Pipeline的请求
    httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
    var pipelineDef = new
    {
        name = "test_pipeline",
        properties = new
        {
            description = "REST API创建的测试Pipeline",
            activities = new[]
            {
                new {
                    name = "WaitActivity1",
                    type = "Wait",
                    typeProperties = new { waitTimeInSeconds = 1 }
                }
            }
        }
    };
    var pipelineReqContent = new StringContent(JsonSerializer.Serialize(pipelineDef), Encoding.UTF8, "application/json");
    var createResp = await httpClient.PutAsync("https://你的工作区名称.dev.azuresynapse.net/pipelines/test_pipeline?api-version=2020-12-01", pipelineReqContent);
    
    // 4. 处理响应
    if (createResp.IsSuccessStatusCode)
    {
        // 状态码200代表创建完成,202代表异步创建中,可通过响应头的Operation-Location字段轮询创建状态
        Console.WriteLine("Pipeline创建请求提交成功");
    }
    
  • 步骤4:验证创建结果

    可通过GET请求查询Pipeline详情确认是否创建成功,请求端点为:
    GET https://{Synapse工作区名称}.dev.azuresynapse.net/pipelines/{Pipeline名称}?api-version=2020-12-01
    请求成功返回200状态码及Pipeline的完整配置即为创建成功。

  • 注意事项

    • 你使用的Azure AD应用需要被分配Synapse工作区的Synapse Contributor或更高权限,否则会返回403权限错误
    • Pipeline定义的JSON结构需符合规范,否则会返回400参数错误

内容的提问来源于stack exchange,提问作者Prashant Rewatkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 10:27:02