LoginSignup
3
2

More than 3 years have passed since last update.

Azure Queue Storage triggerでキューの情報をDBに非同期で書き込む(Azure Strage テーブル)

Last updated at Posted at 2020-02-24

以下のサイトを参考(そのままコピペ)にして、Functionsで受け付けた情報をAzure Storage キューに書き込み出来るようになりました。
Functions を使用して Azure Storage キューにメッセージを追加する

ちょっとステップアップして、非同期テーブル書き込み処理を実現してみました。
1.HTTPのGETリクエストのパラメータをAzure Storage キューに書き込み
2.Azure Storage キューをトリガーにAzure Storage テーブルに書き込み

1.HTTPのGETリクエストのパラメータをAzure Storage キューに書き込み

<参考にした手順はこちら>
Functions を使用して Azure Storage キューにメッセージを追加する

1. Azure ポータルから関数アプリ(Functions)を作成し、HTTPトリガーのテンプレートを利用すると自動でコードが作成される

2. さらにブレードから統合を選択
image.png

3. 出力から「+新しい出力」をAzure Queue Storageを選択
image.png

image.png
4. 便利なのでStorage Explorerをインストール(接続するときは接続文字列を使うと便利、ストレージ表示名は接続文字列を入力すると自動補完してくれた
image.png
5. HTTP Triger1のコードに以下を追加

実行するとHTTPのGETリクエストのクエリパラメータをキューに保存される。

  • ブラウザを打鍵した結果

image.png

  • Storage Explorerの内容

image.png

<一部抜粋>
public static async Task Run(HttpRequest req, ICollector outputQueueItem, ILogger log)
outputQueueItem.Add(name);

<全量>

run.csx
#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ICollector<string> outputQueueItem, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    // パラメーターを使用してキュー メッセージを作成するコードを追加
    outputQueueItem.Add(name);

    return name != null
        ? (ActionResult)new OkObjectResult($"Hello, {name}")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

2.Azure Storage キューをトリガーにAzure Storage テーブルに書き込み

1. 関数(Azure Queue Storage trigger)の追加を行う

image.png

※キュー名はINPUTとするトリガーキューの名前なので、先ほどHTTP Trigger1で出力したキュー名を指定する必要有り
image.png
2. 統合>出力からAzure Table Storageを選択

image.png
3. Storage Explorerからテーブル「outTable」を作成

image.png
4. 以下のコードに書き換え
<全量>

run.csx
using System;

public static void Run(string myQueueItem, ICollector<Person> outputTable, ILogger log)
{
    log.LogInformation($"C# Queue trigger function processed: {myQueueItem}");

    log.LogInformation($"Adding Person entity {myQueueItem}");
    outputTable.Add(
        new Person() { 
            PartitionKey = "Test", 
            RowKey = $"{myQueueItem}"}
        );
}

public class Person
{
    public string PartitionKey { get; set; }
    public string RowKey { get; set; }
}

5. 実行するとHTTPのGETリクエストのクエリパラメータがキューに保存されて、DBに格納される。キューの情報は取り出したら自動で削除してくれるっぽい

  • ブラウザを打鍵した結果

image.png

  • Storage Explorerの内容

image.png

image.png

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2