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

在C# .NET中使用Databricks Statement Execution API遇仓库ID错误求助

问题:调用Azure Databricks Statement Execution API返回BadRequest错误,提示{warehouseId}无效

我需要用C# .NET查询Azure Databricks Delta Lake数据,使用Databricks的Statement Execution API,但运行控制台应用时返回BadRequest错误,提示{warehouseId}不是有效端点ID。

我的代码如下:

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace DatabricksApiApp
{
    class Program
    {
        static readonly HttpClient client = new HttpClient();

        static async Task Main(string[] args)
        {
            try
            {
                // Set up the endpoint and the access token
                string baseUrl = "https://adb-7786225719779619.19.azuredatabricks.net/api/2.0/sql/statements";
                string accessToken = "{access_token}";
                string warehouseId = "ff7b6377ff77ab94";

                // Prepare the HTTP request
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, baseUrl)
                {
                    Content = new StringContent(
                        "{\"statement\":\"SELECT * FROM default.product_1\", \"warehouse_id\":\"{warehouseId}\", \"timeout_seconds\":600}",
                        Encoding.UTF8,
                        "application/json"
                    )
                };
                request.Headers.Add("Authorization", $"Bearer {accessToken}");

                // Send the request
                HttpResponseMessage response = await client.SendAsync(request);

                // Check the response
                if (response.IsSuccessStatusCode)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("Data retrieved successfully!");
                    Console.WriteLine(responseBody);
                }
                else
                {
                    Console.WriteLine($"Failed to retrieve data. Status code: {response.StatusCode}");
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine($"Response Body: {responseBody}");
                }
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("Message :{0} ", e.Message);
            }
        }
    }
}

错误信息:

Failed to retrieve data. Status code: BadRequest
Response Body: {"error_code":"INVALID_PARAMETER_VALUE","message":"{warehouseId} is not a valid endpoint id."}

我在Azure Databricks工作区的SQL Warehouses连接详情中看到HTTP Path为/sql/1.0/warehouses/ff7b6377ff77ab94,认为ff7b6377ff77ab94是仓库ID,但不确定哪里操作有误或遗漏?


解决方案

核心问题:字符串未正确插值

你代码里的JSON字符串中,"{warehouseId}"没有被替换成实际的仓库ID变量值,而是直接将字符串{warehouseId}发送给了API,导致API识别这个占位符为无效的仓库ID。

修正后的代码

使用字符串插值(在字符串前加$符号)来替换JSON中的变量:

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace DatabricksApiApp
{
    class Program
    {
        static readonly HttpClient client = new HttpClient();

        static async Task Main(string[] args)
        {
            try
            {
                // Set up the endpoint and the access token
                string baseUrl = "https://adb-7786225719779619.19.azuredatabricks.net/api/2.0/sql/statements";
                string accessToken = "{access_token}"; // 替换为你的实际access token
                string warehouseId = "ff7b6377ff77ab94";

                // 使用字符串插值生成正确的JSON内容
                string jsonContent = $@"{{
                    ""statement"": ""SELECT * FROM default.product_1"",
                    ""warehouse_id"": ""{warehouseId}"",
                    ""timeout_seconds"": 600
                }}";

                // Prepare the HTTP request
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, baseUrl)
                {
                    Content = new StringContent(jsonContent, Encoding.UTF8, "application/json")
                };
                request.Headers.Add("Authorization", $"Bearer {accessToken}");

                // Send the request
                HttpResponseMessage response = await client.SendAsync(request);

                // Check the response
                if (response.IsSuccessStatusCode)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("Data retrieved successfully!");
                    Console.WriteLine(responseBody);
                }
                else
                {
                    Console.WriteLine($"Failed to retrieve data. Status code: {response.StatusCode}");
                    string responseBody = await response.Content.ReadAsStringAsync();
                    Console.WriteLine($"Response Body: {responseBody}");
                }
            }
            catch (HttpRequestException e)
            {
                Console.WriteLine("\nException Caught!");
                Console.WriteLine("Message :{0} ", e.Message);
            }
        }
    }
}

额外检查点

  • 验证Access Token有效性:确保{access_token}替换为具有SQL仓库访问权限的有效令牌,可通过Databricks工作区的个人访问令牌生成。
  • 确认仓库ID正确性:从SQL Warehouses页面的"连接详情"中复制完整的仓库ID,确保没有多余空格或字符。
  • API端点正确性:确认baseUrl的格式正确,Azure Databricks的Statement Execution API端点格式为https://<databricks-instance>/api/2.0/sql/statements,你的当前端点看起来是正确的。

内容的提问来源于stack exchange,提问作者Eugene Goldberg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 02:56:01