简体   繁体   English

使用 Azure Function 中的 Kusto.Data 访问 Azure 数据资源管理器 -- Kusto 发送请求失败 -- 本地调试工作

[英]Access Azure Data Explorer with Kusto.Data in Azure Function -- Kusto failed to send request -- local debugging works

I am having the following problem and an extensive search online didn't provide any good results.我遇到了以下问题,并且在线进行了广泛的搜索并没有提供任何好的结果。 When trying to access my Azure Data Explorer Database and querying using the Kusto.Data SDK in an Azure Function, it yields the following error: When trying to access my Azure Data Explorer Database and querying using the Kusto.Data SDK in an Azure Function, it yields the following error:

Kusto client failed to send a request to the service: 'An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call.' Kusto 客户端未能向服务发送请求:“在 getsockopt 或 setsockopt 调用中指定了未知、无效或不受支持的选项或级别。”

However, running the Function on my local machine, everything works fine.但是,在我的本地机器上运行 Function,一切正常。

Edit: The function excepts at using (var reader = await queryProvider.ExecuteQueryAsync(Database, query, clientRequestProperties))编辑:function using (var reader = await queryProvider.ExecuteQueryAsync(Database, query, clientRequestProperties))

EDIT2 - SOLUTION: EDIT2 - 解决方案:

You can downgrade the NuGet Kusto.Data Package to Version 9.4.1, this solves the problem and doesn't throw any error anymore.您可以将 NuGet Kusto.Data Package 降级到版本 9.4.1,这解决了问题并且不再抛出任何错误。 If you still encounter difficulties, you can try to directly access the ADX database via http requests:如果还是遇到困难,可以尝试通过 http 请求直接访问 ADX 数据库:

const string tenantId = "<tenantId>";
const string client_id = "<clientId>";
const string client_secret = "<client_secret>";
const string Cluster = "<cluster_adress";
const string Database = "<database_name>";

var authUrl = "https://login.microsoftonline.com/<tenantId>/oauth2/token";
var param = new Dictionary<string, string>
            {
                {"client_id",client_id},
                {"grant_type","client_credentials"},
                {"client_secret",client_secret},
                {"resource","https://help.kusto.windows.net"}
            };
var data = new FormUrlEncodedContent(param);
using var authClient = new HttpClient();
var response = await authClient.PostAsync(authUrl, data);
string result = response.Content.ReadAsStringAsync().Result;

//parse result
var resultJson = System.Text.Json.JsonDocument.Parse(result);
//retrieve access token
var accessToken = resultJson.RootElement.GetProperty("access_token");
//-----------------------------------------------------------------------------------------------

var dataXUrl = Cluster + "/v1/rest/query";
var database = Database;

var dataXQuery = "sample_table| where Time > ago(2min)";
var body = new Dictionary<string, string>
{
    {"db",database},
    {"csl",dataXQuery}
};

using var dataXClient = new HttpClient();
dataXClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.ToString());
dataXClient.DefaultRequestHeaders.Add("Accept", "application/json");

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, dataXUrl);
request.Content = new StringContent(JsonConvert.SerializeObject(body), Encoding.UTF8, "application/json");

var table = await dataXClient.SendAsync(request);

//pretty print
var obj = JsonConvert.DeserializeObject(table.Content.ReadAsStringAsync());
var tableJSON = JsonConvert.SerializeObject(obj, Formatting.Indented);

log.LogInformation("\n\n" + tableJSON);

I am having the same issue on a continuous webjob on an Azure App Service.我在 Azure 应用服务上的连续网络作业中遇到了同样的问题。 The Kusto nuget version I am using is 10.1.0我使用的 Kusto nuget 版本是 10.1.0

Downgrading to nuget 9.4.1 solved the problem immediately.降级到 nuget 9.4.1 立即解决了问题。

FYI - This only seems to affect 10.1.0.仅供参考 - 这似乎只影响 10.1.0。 The earlier 10.xx versions should work.较早的 10.xx 版本应该可以工作。

The ADX team believes they will have this fixed in the next nuget version. ADX 团队相信他们将在下一个 nuget 版本中修复此问题。

10.0.3 is the latest working version 10.0.3 是最新的工作版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM