Azure Functions 用の Azure Data Explorer 入力バインド (プレビュー)

Azure Data Explorer 入力バインドは、データベースからデータを取得します。

例

C# 関数は、次のいずれかの C# モードを使用して作成できます。

  • 分離されたワーカーモデル:実行時から隔離されたワーカープロセス上で動作するコンパイル済みC#関数。 .NET および .NET Framework の長期サポート (LTS) および LTS 以外のバージョンで実行されている C# 関数をサポートするには、分離ワーカー プロセスが必要です。
  • インプロセスモデル:Azure Functionsランタイムと同じプロセス上で動作するコンパイル済みC#関数。
  • C# スクリプト:主に Azure ポータルで C# 関数を作成する際に使用されます。

重要

進行中のモデルのサポートは2026年11月10日に終了しました。 フルサポートを受け続けるには、 アプリを隔離ワーカーモデルに移行してください。

Azure Data Explorer 入力バインド (プロセス外) のその他のサンプルは、GitHub リポジトリにあります。

このセクションには、次の例が含まれています。

この例では、Product クラスと Products テーブルを参照しています。どちらも前のセクションで定義されています。

HTTP トリガー、クエリ文字列からの ID による行の取得

次の例は、単一のレコードを取得する C# 関数を示しています。 この関数は、クエリ文字列を使用して ID を指定する HTTP 要求によってトリガーされます。 その ID は、指定されたクエリを使用して Product レコードを取得するために使用されます。

注

HTTP クエリ文字列パラメーターは大文字と小文字が区別されます。

using System.Text.Json.Nodes;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.Kusto;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.WebJobs.Extensions.Kusto.SamplesOutOfProc.OutputBindingSamples.Common;

namespace Microsoft.Azure.WebJobs.Extensions.Kusto.SamplesOutOfProc.InputBindingSamples
{
    public static class GetProductsQuery
    {
        [Function("GetProductsQuery")]
        public static JsonArray Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "getproductsquery")] HttpRequestData req,
            [KustoInput(Database: "productsdb",
            KqlCommand = "declare query_parameters (productId:long);Products | where ProductID == productId",
            KqlParameters = "@productId={Query.productId}",Connection = "KustoConnectionString")] JsonArray products)
        {
            return products;
        }
    }
}

HTTP トリガー、ルート パラメーターからの複数の行の取得

次の例は、クエリによって返されたレコードを取得する C# 関数 を示しています (この場合は製品名に基づきます)。 この関数は、ルート データを使用してクエリ パラメーターの値を指定する HTTP 要求によってトリガーされます。 このパラメーターは、指定されたクエリ内の Product レコードをフィルター処理するために使用されます。

using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Extensions.Kusto;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.WebJobs.Extensions.Kusto.SamplesOutOfProc.OutputBindingSamples.Common;

namespace Microsoft.Azure.WebJobs.Extensions.Kusto.SamplesOutOfProc.InputBindingSamples
{
    public static class GetProductsFunction
    {
        [Function("GetProductsFunction")]
        public static IEnumerable<Product> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "getproductsfn/{name}")] HttpRequestData req,
            [KustoInput(Database: "productsdb",
            KqlCommand = "declare query_parameters (name:string);GetProductsByName(name)",
            KqlParameters = "@name={name}",Connection = "KustoConnectionString")] IEnumerable<Product> products)
        {
            return products;
        }
    }
}

Java の Azure Data Explorer 入力バインドのその他のサンプルは、GitHub リポジトリにあります。

このセクションには、次の例が含まれています。

次の例では Product クラス (別個のファイル Product.java 内) とそれに対応するデータベース テーブルを参照します。

package com.microsoft.azure.kusto.common;

import com.fasterxml.jackson.annotation.JsonProperty;

public class Product {
    @JsonProperty("ProductID")
    public long ProductID;
    @JsonProperty("Name")
    public String Name;
    @JsonProperty("Cost")
    public double Cost;

    public Product() {
    }

    public Product(long ProductID, String name, double Cost) {
        this.ProductID = ProductID;
        this.Name = name;
        this.Cost = Cost;
    }
}

HTTP トリガー、複数の行の取得

この例では、ルート パラメーターを使用して製品の ID の名前を指定します。 一致するすべての製品が Products テーブルから取得されます。

package com.microsoft.azure.kusto.inputbindings;

import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.kusto.annotation.KustoInput;
import com.microsoft.azure.kusto.common.Product;


import java.util.Optional;

public class GetProducts {
    @FunctionName("GetProducts")
    public HttpResponseMessage run(
        @HttpTrigger(name = "req", methods = {
            HttpMethod.GET}, authLevel = AuthorizationLevel.ANONYMOUS, route = "getproducts/{productId}") HttpRequestMessage<Optional<String>> request,
            @KustoInput(name = "getjproducts", kqlCommand = "declare query_parameters (productId:long);Products | where ProductID == productId",
                    kqlParameters = "@productId={productId}", database = "productsdb", connection = "KustoConnectionString") Product[] products) {
        return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products)
                .build();
    }
}

HTTP トリガー、クエリ文字列からの ID による行の取得

次の例は、製品名による Products テーブルのクエリを示しています。 この関数は、クエリ文字列を使用してクエリ パラメーターの値を指定する HTTP 要求によってトリガーされます。 このパラメーターは、指定されたクエリ内の Product レコードをフィルター処理するために使用されます。

package com.microsoft.azure.kusto.inputbindings;

import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;
import com.microsoft.azure.functions.kusto.annotation.KustoInput;
import com.microsoft.azure.kusto.common.Product;

import java.util.Optional;

public class GetProductsQueryString {
    @FunctionName("GetProductsQueryString")
    public HttpResponseMessage run(@HttpTrigger(name = "req", methods = {
            HttpMethod.GET}, authLevel = AuthorizationLevel.ANONYMOUS, route = "getproducts") HttpRequestMessage<Optional<String>> request,
            @KustoInput(name = "getjproductsquery", kqlCommand = "declare query_parameters (name:string);GetProductsByName(name)",
                    kqlParameters = "@name={Query.name}", database = "productsdb", connection = "KustoConnectionString") Product[] products) {
        return request.createResponseBuilder(HttpStatus.OK).header("Content-Type", "application/json").body(products)
                .build();
    }
}

Azure Data Explorer 入力バインドのその他のサンプルは、GitHub リポジトリにあります。

このセクションには、次の例が含まれています。

この例では、次のデータベース テーブルを参照します。

HTTP トリガー、複数の行の取得

次の例は、function.json ファイル内の Azure Data Explorer 入力バインドと、クエリから読み取って結果を HTTP 応答で返す JavaScript 関数を示しています。

次のバインド データは function.json ファイルにあります。

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "direction": "in",
      "type": "httpTrigger",
      "methods": [
        "get"
      ],
      "route": "getproducts/{productId}"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "productget",
      "type": "kusto",
      "database": "productsdb",
      "direction": "in",
      "kqlCommand": "declare query_parameters (productId:long);Products | where ProductID == productId",
      "kqlParameters": "@productId={productId}",
      "connection": "KustoConnectionString"
    }
  ],
  "disabled": false
}

これらのプロパティについては、「構成」セクションを参照してください。

次のスニペットは、JavaScript コードの例です。

module.exports = async function (context, req, productget) {
    return {
        status: 200,
        body: productget
    };
}

HTTP トリガー、クエリ文字列からの名前による行の取得

次の例は、製品名による Products テーブルのクエリを示しています。 この関数は、クエリ文字列を使用してクエリ パラメーターの値を指定する HTTP 要求によってトリガーされます。 このパラメーターは、指定されたクエリ内の Product レコードをフィルター処理するために使用されます。

次のバインド データは function.json ファイルにあります。

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "direction": "in",
      "type": "httpTrigger",
      "methods": [
        "get"
      ],
      "route": "getproductsfn"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "productfnget",
      "type": "kusto",
      "database": "productsdb",
      "direction": "in",
      "kqlCommand": "declare query_parameters (name:string);GetProductsByName(name)",
      "kqlParameters": "@name={Query.name}",
      "connection": "KustoConnectionString"
    }
  ],
  "disabled": false
}

これらのプロパティについては、「構成」セクションを参照してください。

次のスニペットは、JavaScript コードの例です。

module.exports = async function (context, req, producproductfngettget) {
    return {
        status: 200,
        body: productfnget
    };
}

Azure Data Explorer 入力バインドのその他のサンプルは、GitHub リポジトリにあります。

このセクションには、次の例が含まれています。

HTTP トリガー、複数の行の取得

次の例は、function.json ファイル内の Azure Data Explorer 入力バインドと、クエリから読み取って結果を HTTP 応答で返す Python 関数を示しています。

次のバインド データは function.json ファイルにあります。

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "Anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get"
      ],
      "route": "getproducts/{productId}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "name": "productsdb",
      "type": "kusto",
      "database": "sdktestsdb",
      "direction": "in",
      "kqlCommand": "declare query_parameters (productId:long);Products | where ProductID == productId",
      "kqlParameters": "@productId={Query.productId}",
      "connection": "KustoConnectionString"
    }
  ]
}

これらのプロパティについては、「構成」セクションを参照してください。

次のスニペットは、Python コードの例です。

import azure.functions as func
from Common.product import Product


def main(req: func.HttpRequest, products: str) -> func.HttpResponse:
    return func.HttpResponse(
        products,
        status_code=200,
        mimetype="application/json"
    )

HTTP トリガー、クエリ文字列からの ID による行の取得

次の例は、製品名による Products テーブルのクエリを示しています。 この関数は、クエリ文字列を使用してクエリ パラメーターの値を指定する HTTP 要求によってトリガーされます。 このパラメーターは、指定されたクエリ内の Product レコードをフィルター処理するために使用されます。

次のバインド データは function.json ファイルにあります。

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "direction": "in",
      "type": "httpTrigger",
      "methods": [
        "get"
      ],
      "route": "getproductsfn"
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "productfnget",
      "type": "kusto",
      "database": "productsdb",
      "direction": "in",
      "kqlCommand": "declare query_parameters (name:string);GetProductsByName(name)",
      "kqlParameters": "@name={Query.name}",
      "connection": "KustoConnectionString"
    }
  ],
  "disabled": false
}

これらのプロパティについては、「構成」セクションを参照してください。

次のスニペットは、Python コードの例です。

import azure.functions as func

def main(req: func.HttpRequest, products: str) -> func.HttpResponse:
    return func.HttpResponse(
        products,
        status_code=200,
        mimetype="application/json"
    )

属性

C# ライブラリでは KustoAttribute 属性を使用して、次のプロパティを持つ関数に対して Azure Data Explorer バインドを宣言します。

属性のプロパティ 説明
データベース 必須。 クエリを実行する必要があるデータベース。
接続 必須。 環境変数または関数アプリの設定によって解決される接続文字列を保持する変数の "名前"。 既定では、変数 KustoConnectionString を検索します。 実行時に、環境に対してこの変数が検索されます。 接続文字列のドキュメントについては、「Kusto 接続文字列」を参照してください。 たとえば、"KustoConnectionString": "Data Source=https://your_cluster.kusto.windows.net;Database=your_Database;Fed=True;AppClientId=your_AppId;AppKey=your_AppKey;Authority Id=your_TenantId となります。
KqlCommand 必須。 実行する必要がある KqlQuery パラメーター。 KQL クエリまたは KQL 関数呼び出しを指定できます。
KqlParameters 省略可能。 KqlCommand の述語変数として機能するパラメーター。 たとえば、"@name={name},@Id={id}" のようになり、{name} と {id} は実行時に述語として機能する実際の値に置き換えられます。 パラメーター名とパラメーター値にコンマ (,) または等号 (=) を含めることはできません。
マネージドサービスアイデンティティ 省略可能。 マネージド ID を使用して、Azure Data Explorer に接続できます。 システム マネージド ID を使用するには、"system" を使用します。その他の ID 名はユーザー マネージド ID として解釈されます。

注釈

Java 関数ランタイム ライブラリでは、@KustoInput 注釈 (com.microsoft.azure.functions.kusto.annotation.KustoInput) が使用されます。

要素 説明
名前 必須。 関数コード内のクエリ結果を表す変数の名前。
データベース 必須。 クエリを実行する必要があるデータベース。
接続 必須。 環境変数または関数アプリの設定によって解決される接続文字列を保持する変数の "名前"。 既定では、変数 KustoConnectionString を検索します。 実行時に、環境に対してこの変数が検索されます。 接続文字列のドキュメントについては、「Kusto 接続文字列」を参照してください。 たとえば、"KustoConnectionString": "Data Source=https://your_cluster.kusto.windows.net;Database=your_Database;Fed=True;AppClientId=your_AppId;AppKey=your_AppKey;Authority Id=your_TenantId となります。
kqlCommand 必須。 実行する必要がある KqlQuery パラメーター。 KQL クエリまたは KQL 関数呼び出しを指定できます。
kqlParameters 省略可能。 KqlCommand の述語変数として機能するパラメーター。 たとえば、"@name={name},@Id={id}" のようになり、{name} と {id} は実行時に述語として機能する実際の値に置き換えられます。 パラメーター名とパラメーター値にコンマ (,) または等号 (=) を含めることはできません。
managedServiceIdentity マネージド ID を使用して、Azure Data Explorer に接続できます。 システム マネージド ID を使用するには、"system" を使用します。その他の ID 名はユーザー マネージド ID として解釈されます。

構成

次の表は、function.json ファイルで設定したバインド構成のプロパティを説明しています。

function.json のプロパティ 説明
型 必須。 kusto に設定する必要があります。
方向 必須。 in に設定する必要があります。
名前 必須。 関数コード内のクエリ結果を表す変数の名前。
データベース 必須。 クエリを実行する必要があるデータベース。
接続 必須。 環境変数または関数アプリの設定によって解決される接続文字列を保持する変数の "名前"。 既定では、変数 KustoConnectionString を検索します。 実行時に、環境に対してこの変数が検索されます。 接続文字列のドキュメントについては、「Kusto 接続文字列」を参照してください。 たとえば、"KustoConnectionString": "Data Source=https://your_cluster.kusto.windows.net;Database=your_Database;Fed=True;AppClientId=your_AppId;AppKey=your_AppKey;Authority Id=your_TenantId となります。
kqlCommand 必須。 実行する必要がある KqlQuery パラメーター。 KQL クエリまたは KQL 関数呼び出しを指定できます。
kqlParameters 省略可能。 KqlCommand の述語変数として機能するパラメーター。 たとえば、"@name={name},@Id={id}" のようになり、{name} と {id} は実行時に述語として機能する実際の値に置き換えられます。 パラメーター名とパラメーター値にコンマ (,) または等号 (=) を含めることはできません。
managedServiceIdentity マネージド ID を使用して、Azure Data Explorer に接続できます。 システム マネージド ID を使用するには、"system" を使用します。その他の ID 名はユーザー マネージド ID として解釈されます。

ローカルで開発する場合は、 コレクション内の Valuesにアプリケーション設定を追加します。

使用方法

属性のコンストラクターは、データベースと、属性 KQLCommand と KQLParameters および接続設定名を受け取ります。 KQL コマンドには、KQL ステートメントまたは KQL 関数を指定できます。 接続文字列の設定名は、local.settings.jsonを含むアプリケーション設定 (ローカル開発の場合は にあります) に対応します。 (例: "KustoConnectionString": "Data Source=https://your_cluster.kusto.windows.net;Database=your_Database;Fed=True;AppClientId=your_AppId;AppKey=your_AppKey;Authority Id=your_TenantId)。 入力バインディングによって実行されるクエリはパラメーター化されます。 KQL パラメーターで指定された値は、実行時に使用されます。

重要

最適なセキュリティを確保するために、関数アプリでは、キーを含む接続文字列を使用する代わりに、Azure Data Explorer に接続するときにマネージド ID を使用する必要があります。 詳細については、「Kusto 接続文字列」を参照してください。 mananaged ID ベースの接続の場合は、バインド定義で managedServiceIdentity プロパティを設定する必要があります。

次のステップ

データをテーブルに保存する(出力バインディング)