Azure Table の出力バインドを使用して、Azure Cosmos DB for Table または Azure Table Storage のテーブルにエンティティを書き込みます。
セットアップと構成の詳細については、概要をご覧ください
注
この出力バインドでは、テーブル内での新しいエンティティの作成のみがサポートされます。 関数コードから既存のエンティティを更新する必要がある場合、代わりに Azure Tables SDK を直接使用します。
重要
この記事では、タブを使用して、Node.js プログラミング モデルの複数のバージョンに対応しています。 v4 モデルは一般提供されており、JavaScript と TypeScript の開発者にとって、より柔軟で直感的なエクスペリエンスが得られるように設計されています。 v4 モデルの動作の詳細については、Azure Functions Node.js 開発者ガイドを参照してください。 v3 と v4 の違いの詳細については、移行ガイドを参照してください。
例
このバインドに関しては現在、Goのサポートは利用できません。
A C# 関数は、次の C# モードのいずれかを使用して作成できます。
-
分離されたワーカー モデル: ランタイムから分離されたワーカー プロセスで実行されるコンパイル済みの C# 関数。 分離ワーカー プロセスは、LTS および 非 LTS バージョンの .NET および .NET Framework で実行されている C# 関数をサポートするために必要です。 分離ワーカー プロセス関数の拡張機能では、
Microsoft.Azure.Functions.Worker.Extensions.*名前空間が使用されます。 -
インプロセス モデル: Functions ランタイムと同じプロセスで実行されるコンパイル済みの C# 関数。 このモデルの一部では、主に C# ポータルの編集のためにサポートされている C# スクリプトを使用して Functions を実行できます。 インプロセス関数の拡張機能では、
Microsoft.Azure.WebJobs.Extensions.*名前空間が使用されます。
重要
インプロセス モデルのサポートは 2026 年 11 月 10 日に終了します。 完全なサポートのために、分離ワーカー モデルにアプリを移行することを強くお勧めします。
次の MyTableData のクラスは、テーブル内のデータの行を表します。
public class MyTableData : Azure.Data.Tables.ITableEntity
{
public string Text { get; set; }
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public DateTimeOffset? Timestamp { get; set; }
public ETag ETag { get; set; }
}
Queue Storage トリガーによって開始される次の関数は、 MyDataTableという名前の OutputTable に新しいエンティティを書き込みます。
[Function("TableFunction")]
[TableOutput("OutputTable", Connection = "AzureWebJobsStorage")]
public static MyTableData Run(
[QueueTrigger("table-items")] string input,
[TableInput("MyTable", "<PartitionKey>", "{queueTrigger}")] MyTableData tableInput,
FunctionContext context)
{
var logger = context.GetLogger("TableFunction");
logger.LogInformation($"PK={tableInput.PartitionKey}, RK={tableInput.RowKey}, Text={tableInput.Text}");
return new MyTableData()
{
PartitionKey = "queue",
RowKey = Guid.NewGuid().ToString(),
Text = $"Output record with rowkey {input} created at {DateTime.Now}"
};
}
次の例は、HTTP トリガーを使用して 1 つのテーブル行を書き込む Java 関数を示しています。
public class Person {
private String PartitionKey;
private String RowKey;
private String Name;
public String getPartitionKey() {return this.PartitionKey;}
public void setPartitionKey(String key) {this.PartitionKey = key; }
public String getRowKey() {return this.RowKey;}
public void setRowKey(String key) {this.RowKey = key; }
public String getName() {return this.Name;}
public void setName(String name) {this.Name = name; }
}
public class AddPerson {
@FunctionName("addPerson")
public HttpResponseMessage get(
@HttpTrigger(name = "postPerson", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION, route="persons/{partitionKey}/{rowKey}") HttpRequestMessage<Optional<Person>> request,
@BindingName("partitionKey") String partitionKey,
@BindingName("rowKey") String rowKey,
@TableOutput(name="person", partitionKey="{partitionKey}", rowKey = "{rowKey}", tableName="%MyTableName%", connection="MyConnectionString") OutputBinding<Person> person,
final ExecutionContext context) {
Person outPerson = new Person();
outPerson.setPartitionKey(partitionKey);
outPerson.setRowKey(rowKey);
outPerson.setName(request.getBody().get().getName());
person.setValue(outPerson);
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type", "application/json")
.body(outPerson)
.build();
}
}
次の例は、HTTP トリガーを使用して複数のテーブル行を書き込む Java 関数を示しています。
public class Person {
private String PartitionKey;
private String RowKey;
private String Name;
public String getPartitionKey() {return this.PartitionKey;}
public void setPartitionKey(String key) {this.PartitionKey = key; }
public String getRowKey() {return this.RowKey;}
public void setRowKey(String key) {this.RowKey = key; }
public String getName() {return this.Name;}
public void setName(String name) {this.Name = name; }
}
public class AddPersons {
@FunctionName("addPersons")
public HttpResponseMessage get(
@HttpTrigger(name = "postPersons", methods = {HttpMethod.POST}, authLevel = AuthorizationLevel.FUNCTION, route="persons/") HttpRequestMessage<Optional<Person[]>> request,
@TableOutput(name="person", tableName="%MyTableName%", connection="MyConnectionString") OutputBinding<Person[]> persons,
final ExecutionContext context) {
persons.setValue(request.getBody().get());
return request.createResponseBuilder(HttpStatus.OK)
.header("Content-Type", "application/json")
.body(request.getBody().get())
.build();
}
}
次の例は、複数のテーブル エンティティを書き込むテーブル出力バインドを示しています。
import { app, HttpRequest, HttpResponseInit, InvocationContext, output } from '@azure/functions';
const tableOutput = output.table({
tableName: 'Person',
connection: 'MyStorageConnectionAppSetting',
});
interface PersonEntity {
PartitionKey: string;
RowKey: string;
Name: string;
}
export async function httpTrigger1(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
const rows: PersonEntity[] = [];
for (let i = 1; i < 10; i++) {
rows.push({
PartitionKey: 'Test',
RowKey: i.toString(),
Name: `Name ${i}`,
});
}
context.extraOutputs.set(tableOutput, rows);
return { status: 201 };
}
app.http('httpTrigger1', {
methods: ['POST'],
authLevel: 'anonymous',
extraOutputs: [tableOutput],
handler: httpTrigger1,
});
const { app, output } = require('@azure/functions');
const tableOutput = output.table({
tableName: 'Person',
connection: 'MyStorageConnectionAppSetting',
});
app.http('httpTrigger1', {
methods: ['POST'],
authLevel: 'anonymous',
extraOutputs: [tableOutput],
handler: async (request, context) => {
const rows = [];
for (let i = 1; i < 10; i++) {
rows.push({
PartitionKey: 'Test',
RowKey: i.toString(),
Name: `Name ${i}`,
});
}
context.extraOutputs.set(tableOutput, rows);
return { status: 201 };
},
});
次の例は、関数からテーブルに複数のエンティティを書き込む方法を示しています。
function.json のバインド構成:
{
"bindings": [
{
"name": "InputData",
"type": "manualTrigger",
"direction": "in"
},
{
"tableName": "Person",
"connection": "MyStorageConnectionAppSetting",
"name": "TableBinding",
"type": "table",
"direction": "out"
}
],
"disabled": false
}
run.ps1 の PowerShell コード:
param($InputData, $TriggerMetadata)
foreach ($i in 1..10) {
Push-OutputBinding -Name TableBinding -Value @{
PartitionKey = 'Test'
RowKey = "$i"
Name = "Name $i"
}
}
次の例では、Table Storage の出力バインドを使用する方法を示します。
table バインドは、値を 、name、tableName、partitionKey に割り当てることで connection で次のように構成されます。
次の関数は、rowKey 値に対して一意の UUI を生成し、メッセージを Table Storage に保持します。
import logging
import uuid
import json
import azure.functions as func
app = func.FunctionApp()
@app.route(route="table_out_binding")
@app.table_output(arg_name="message",
connection="AzureWebJobsStorage",
table_name="messages")
def table_out_binding(req: func.HttpRequest, message: func.Out[str]):
row_key = str(uuid.uuid4())
data = {
"Name": "Output binding message",
"PartitionKey": "message",
"RowKey": row_key
}
table_json = json.dumps(data)
message.set(table_json)
return table_json
属性
インプロセスと分離ワーカー プロセスの C# ライブラリの両方で、属性を使って関数を定義します。 C# スクリプトでは、C# スクリプト ガイドで説明されているように、代わりに function.json 構成ファイルを使用します。
C# クラス ライブラリでは、TableInputAttribute は次のプロパティをサポートしています。
| 属性のプロパティ | 説明 |
|---|---|
| テーブル名 | 書き込むテーブルの名前。 |
| PartitionKey | 書き込むテーブル エンティティのパーティション キー。 |
| RowKey | 書き込むテーブル エンティティの行キー。 |
| 接続 | テーブル サービスへの接続方法を指定するアプリ設定または設定コレクションの名前。 「接続」を参照してください。 |
注釈
Java 関数ランタイム ライブラリで、パラメーターで TableOutput 注釈を使用し、ご使用のテーブルに値を書き込みます。 属性では、次の要素がサポートされています。
| 要素 | 説明 |
|---|---|
| 名前 | テーブルまたはエンティティを表す関数コードに使用される変数の名前。 |
| dataType | Functions ランタイムがパラメーター値をどのように扱うかを定義します。 詳細については、 「データ型」に関するページを参照してください。 |
| tableName | 書き込むテーブルの名前。 |
| partitionKey | 書き込むテーブル エンティティのパーティション キー。 |
| rowKey | 書き込むテーブル エンティティの行キー。 |
| 接続 | テーブル サービスへの接続方法を指定するアプリ設定または設定コレクションの名前。 「接続」を参照してください。 |
構成
構成
次の表は、function.json ファイルで設定したバインド構成のプロパティを説明しています。
| function.json のプロパティ | 説明 |
|---|---|
| タイプ |
table に設定する必要があります。 このプロパティは、Azure Portal でバインドを作成するときに自動で設定されます。 |
| 方向 |
out に設定する必要があります。 このプロパティは、Azure Portal でバインドを作成するときに自動で設定されます。 |
| 名前 | テーブルまたはエンティティを表す関数コードに使用される変数の名前。
$return に設定して、関数の戻り値を参照します。 |
| tableName | 書き込むテーブルの名前。 |
| partitionKey | 書き込むテーブル エンティティのパーティション キー。 |
| rowKey | 書き込むテーブル エンティティの行キー。 |
| 接続 | テーブル サービスへの接続方法を指定するアプリ設定または設定コレクションの名前。 「接続」を参照してください。 |
つながり
connectionプロパティはアプリケーション設定でキーに設定されており、Functionsランタイムが拡張で使用しているストレージアカウントに接続するために使われる値を返します。 接続プロパティ設定の値は接続の種類によって異なります:
-
マネージドID接続:
connectionプロパティは、複数の設定群が共有する<CONNECTION_NAME_PREFIX>であり、これらが共にストレージアカウントへのアイデンティティベースの接続を定義します。 詳細については、「 同一性接続の定義」を参照してください。 -
Key Vault参照:
connectionプロパティ設定は、接続文字列が中央管理されている場所への参照Azure Key Vaultを返します。 詳細については、「Key Vault connectionsの定義」をご覧ください。 -
App Configuration reference:
connectionプロパティ設定は接続文字列またはKey Vault参照を返すAzure App Configuration参照を返します。 詳細については、接続記事のAzure App Configurationをご覧ください。 -
Connection string:
connectionプロパティ設定は実際のストレージアカウント接続文字列を返します。 接続文字列には共有の秘密鍵が含まれているため、可能であれば管理型アイデンティティ接続の使用を検討すべきです。 詳細については、「 接続の定義」を参照してください。
バインディング接続について詳しく知りたい方は、Azure Functionsの「Manage connection in Connection」をご覧ください。 接続文字列を取得するには、Manage ストレージ アカウントアクセス キーに示されている手順に従います。
connectionをキーやAzureWebJobsStorageという名前のキープレフィックスに設定したり、空文字列に設定した場合、バインディング拡張はデフォルトのホストストレージアカウントを使用します。 詳細については 、「ストレージパフォーマンスの最適化」をご覧ください。
使用法
バインディングの使用方法は、拡張機能パッケージのバージョンと、関数アプリで使用される C# のモダリティによって異なり、次のいずれかになります。
分離ワーカー プロセス クラス ライブラリでコンパイルされた C# 関数は、ランタイムから分離されたプロセスで実行されます。
バージョンを選択すると、モードとバージョンの使用状況の詳細が表示されます。
関数を 1 つのエンティティに書き込む場合、Azure Tables 出力バインドは次の型にバインドできます。
| タイプ | 説明 |
|---|---|
| [ITableEntity] を実装する、JSON シリアル化可能な型 | Functions は、Plain Old CLR Object (POCO) 型をエンティティとしてシリアル化しようとします。 この型は、[ITableEntity] を実装するものか、RowKey 文字列プロパティと PartitionKey 文字列プロパティを持つものにする必要があります。 |
関数で複数のエンティティに書き込む場合、Azure テーブルの出力バインドは次の型にバインドできます。
| タイプ | 説明 |
|---|---|
T[] (T は単一のエンティティ型の 1 つ) |
複数のエンティティを含む配列。 各エントリは 1 つのエンティティを表します。 |
その他の出力シナリオでは、 TableClientAzure.Data.Tables の他の型を直接作成して使用 。 依存関係の挿入を使用して Azure SDK からクライアントの種類を作成する例については Azure クライアントの登録に関するページを参照してください。
TableStorageOutput 注釈を使用して関数から Table Storage 行を出力するには、次の 2 つのオプションがあります。
| オプション | 説明 |
|---|---|
| 戻り値 | 関数自体に注釈を適用すると、関数の戻り値が Table Storage 行として永続化されます。 |
| 命令型 | テーブル行を明示的に設定するには、OutputBinding<T> 型の特定のパラメーターに注釈を適用します。この場合、T には PartitionKey と RowKey のプロパティが含まれます。 これらのプロパティは、ITableEntity を実装、または TableEntity を継承することで、付随させることができます。 |
テーブル データに書き込むには、Push-OutputBinding コマンドレットを使用して、-Name TableBinding パラメーターと、行データに等しい -Value パラメーターを設定します。 詳細については、PowerShell の例を参照してください。
関数から Table Storage 行メッセージを出力するには、次の 2 つのオプションがあります。
| オプション | 説明 |
|---|---|
| 戻り値 |
name 内の プロパティを $return に設定します。 この構成では、関数の戻り値は Table Storage 行として永続化されます。 |
| 命令型 |
Out 型として宣言されたパラメーターの set メソッドに値を渡します。
set に渡された値は、テーブル行として保持されます。 |
具体的な使用方法の詳細については、「例」を参照してください。
例外とリターン コード
| バインド | リファレンス |
|---|---|
| テーブル | テーブル エラー コード |
| BLOB、テーブル、キュー | ストレージ エラー コード |
| BLOB、テーブル、キュー | トラブルシューティング |