適用対象: Azure Database for PostgreSQL - フレキシブル サーバー
この記事では、Microsoft Fabric Data Factory でスクリプト アクティビティを作成して、カスタム PostgreSQL クエリを実行する方法について説明します。 スクリプト アクティビティを使用すると、パイプラインでさまざまな種類の PostgreSQL コマンドを直接実行できます。 コマンドには、次が含まれます。
- データ操作言語 (DML) ステートメント:
INSERT
、UPDATE
、DELETE
、およびSELECT
。 - データ定義言語 (DDL) ステートメント:
CREATE
、ALTER
、およびDROP
。
[前提条件]
- Azure Database for PostgreSQL フレキシブル サーバー。 詳細については、「 Azure Database for PostgreSQL フレキシブル サーバーの作成」を参照してください。
- Microsoft Fabric Data Factory データ パイプライン。
スクリプト アクティビティを作成する
Microsoft Fabric でワークスペースを選択し、 Data Factory に切り替えて、[ 新しい項目 ] ボタンを選択します。
[ 新しい項目 ] ウィンドウで パイプラインを検索 し、[ データ パイプライン ] タイルを選択します。
[ 新しいパイプライン ] ダイアログで、名前を入力し、[ 作成 ] ボタンを選択してデータ パイプラインを作成します。
[ アクティビティ ] メニューの [ スクリプト ] アイコンを選択します。
データ パイプライン キャンバスでスクリプト アクティビティを選択した状態で、[ 全般 ] タブにアクティビティの名前を入力します。
[ 設定] タブで、Azure Database for PostgreSQL 接続を選択するか、[ その他 ] オプションを使用して新しい接続を作成します。 データ パイプラインの最新のデータ取得エクスペリエンスを使用してデータに接続する方法について説明します。
スクリプトに応じて、[ クエリ] または [NonQuery ] オプションを選択します。
スクリプト アクティビティは、クエリ ステートメントと非クエリ ステートメントの両方をサポートします。
クエリ ステートメントは、結果を返す PostgreSQL ステートメント (多くの場合、
SELECT
ステートメント) を実行します。 クエリ ステートメントは、データのレコードを返します。クエリ ステートメントを含むサンプル ペイロードを次に示します。
{ "name": "Sample of select statement", "type": "Script", "dependsOn": [], "policy": { "timeout": "0.12:00:00", "retry": 0, "retryIntervalInSeconds": 30, "secureOutput": false, "secureInput": false }, "typeProperties": { "scripts": [ { "type": "Query", "text": { "value": "SELECT * FROM sample_table WHERE sample_int =100", "type": "Expression" } } ], "scriptBlockExecutionTimeout": "02:00:00" }, "externalReferences": { "connection": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" } }
1 つのスクリプト アクティビティ内に複数のスクリプトを作成する
[ 設定] タブでは、1 つのスクリプト アクティビティで複数のクエリを構成できます。 新しいスクリプト入力を追加するには、[スクリプト] 領域のプラス (+) ボタンを選択します。
作成するスクリプト入力の数に応じて、 + ボタンを複数回選択できます。 たとえば、2 つの新しいスクリプト入力を追加するには、[ + ] ボタンを 2 回選択します。
クエリ入力ボックスを削除する場合は、そのボックスの [削除 ] アイコンを選択します。
2 つの異なるクエリを含むサンプル ペイロードを次に示します。
{
"name": "Sample of multiple select statements",
"type": "Script",
"dependsOn": [],
"policy": {
"timeout": "0.12:00:00",
"retry": 0,
"retryIntervalInSeconds": 30,
"secureOutput": false,
"secureInput": false
},
"typeProperties": {
"scripts": [
{
"type": "Query",
"text": {
"value": "SELECT * FROM sample_table WHERE sample_int = 100;",
"type": "Expression"
}
},
{
"type": "Query",
"text": {
"value": "SELECT * FROM sample_table WHERE sample_int > 250;",
"type": "Expression"
}
}
],
"scriptBlockExecutionTimeout": "02:00:00"
},
"externalReferences": {
"connection": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
}
スクリプト パラメーターを使用する
Von Bedeutung
出力パラメーターを使用する複数クエリ ステートメントはサポートされていません。 出力クエリをスクリプト アクティビティ内の個別のスクリプト ブロックに分割する必要があります。
スクリプト アクティビティでは、次の 2 種類のスクリプト パラメーターがサポートされています。
- 名前付きパラメーターは、パラメーターの名前に基づいており、クエリで
@<name>
として指定されます。 - 位置指定パラメーターは、パラメーターのインデックスに基づいており、クエリで (順番に)
$<position number>
1
の開始インデックスと共に指定されます。
名前付きパラメーター (推奨)
出力パラメーターとして名前付きパラメーターの場合は、 @
プレフィックスを使用します。 UI で [null として扱う] チェック ボックスをオンにして値をnull
に設定し、ペイロードを空白またはnull
のままにします。 テキスト内の値は null
する必要があります。
出力のプロシージャ内で設定される名前は、 resultSets
データ出力内で使用される名前です。 UI 出力行に設定された名前は、 outputParameters
の名前に使用されます。
UI の実行結果の例を次に示します。
{
"resultSetCount": 1,
"recordsAffected": 0,
"resultSets": [
{
"rowCount": 1,
"rows": [
{
"output1": 10,
"output2": "\"Hello World\""
}
]
}
],
"outputParameters": {
"output10": 10,
"output20": "\"Hello World\""
}
}
出力パラメーターのペイロード サンプルを次に示します。
{
"scripts": [
{
"type": "NonQuery",
"text": "CREATE OR REPLACE PROCEDURE swap_proc (input1 IN TEXT, input2 IN BIGINT, output1 OUT BIGINT, output2 OUT TEXT) LANGUAGE plpgsql AS $$ DECLARE BEGIN output2 := input1; output1 := input2; END $$ "
},
{
"parameters": [
{
"name": "input1",
"type": "String",
"value": "Hello world",
"direction": "Input"
},
{
"name": "input2",
"type": "Int32",
"value": "1234",
"direction": "Input"
},
{
"name": "output1",
"type": "Int32",
"value": "",
"direction": "Output"
},
{
"name": "output2",
"type": "String",
"value": "",
"direction": "Output",
"size": 100
}
],
"type": "Query",
"text": "CALL swap_proc(@input1, @input2, null, null)"
}
],
"scriptBlockExecutionTimeout": "02:00:00"
}
位置指定パラメーター
Von Bedeutung
位置指定パラメーターを使用する複数クエリ ステートメントはサポートされていません。 位置指定パラメーターを持つクエリが、スクリプト アクティビティ内の個別のスクリプト ブロック内にあることを確認します。
位置指定パラメーターを使用するには、クエリで $<positional number>
のプレースホルダーを使用します。 UI の [ スクリプト パラメーター] の [ 名前 ] ボックスは空白のままにする必要があります。 ペイロードでは、 name
フィールドを null
として指定する必要があります。
有効な位置指定パラメーターの例を次に示します。
{
"name": "Sample for valid positional parameter",
"type": "Script",
"dependsOn": [],
"policy": {
"timeout": "0.12:00:00",
"retry": 0,
"retryIntervalInSeconds": 30,
"secureOutput": false,
"secureInput": false
},
"typeProperties": {
"scripts": [
{
"parameters": [
{
"type": "String",
"value": "John",
"direction": "Input"
},
{
"type": "Int32",
"value": "52",
"direction": "Input"
}
],
"type": "Query",
"text": {
"value": "SELECT * FROM customers WHERE first_name = $1 and age = $2;",
"type": "Expression"
}
}
],
"scriptBlockExecutionTimeout": "02:00:00"
},
"externalReferences": {
"connection": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
}
次の例は、無効な位置指定パラメーターを示しています。
{
"name": "Sample for invalid positional parameter",
"type": "Script",
"dependsOn": [],
"policy": {
"timeout": "0.12:00:00",
"retry": 0,
"retryIntervalInSeconds": 30,
"secureOutput": false,
"secureInput": false
},
"typeProperties": {
"scripts": [
{
"parameters": [
{
"type": "String",
"value": "John",
"direction": "Input"
},
{
"type": "Int32",
"value": "52",
"direction": "Input"
}
],
"type": "Query",
"text": {
"value": "SELECT * FROM customers WHERE first_name = $1; SELECT * FROM customers WHERE age = $2;",
"type": "Expression"
}
}
],
"scriptBlockExecutionTimeout": "02:00:00"
},
"externalReferences": {
"connection": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
}
詳細設定を構成する
スクリプト ブロックの実行タイムアウト
実行するスクリプト ブロックごとにタイムアウトを分単位で構成できます。 スクリプト アクティビティ内のスクリプト ブロックが指定されたタイムアウトを超えると、アクティビティ全体が失敗します。
"typeProperties": {
"scripts": [
{
"type": "Query",
"text": {
"value": "SELECT pg_sleep(75);",
"type": "Expression"
}
}
],
"scriptBlockExecutionTimeout": "00:01:00"
},
"externalReferences": {
"connection": "9b351899-a92f-4e00-bc48-200a2c287f4c"
}
ログ
PostgreSQL 通知は、外部の Azure Blob Storage アカウントまたは内部ストレージに記録できます。
外部ストレージ
外部ログを設定するには:
[ 設定] タブで、[ 詳細設定 ] セクションを展開します。
[ログ記録を 有効にする ] チェック ボックスと [外部ストレージ ] オプションを選択します。
Blob Storage アカウントを追加するために、Blob Storage アカウントに対する新しいリンクサービスを作成します。
必要に応じて、フォルダー パスを指定できます。 [フォルダー パス] ボックスを空白のままにすると、ログは
scriptactivity-logs
フォルダーに移動します。
"typeProperties": {
"scripts": [
{
"type": "Query",
"text": "DO $$ BEGIN RAISE Notice 'Hello'; RAISE Notice 'World!'; END $$;"
}
],
"scriptBlockExecutionTimeout": "02:00:00",
"logSettings": {
"logDestination": "ExternalStore",
"logLocationSettings": {
"linkedServiceName": {
"referenceName": "<Azure Blob Storage linked service name>",
"type": "LinkedServiceReference"
},
"path": "<Azure Blob Storage folder path>"
}
}
}
アクティビティの出力
アクティビティ出力でログ記録を設定するには:
[ 設定] タブで、[ 詳細設定 ] セクションを展開します。
[ログ記録を 有効にする ] チェックボックスと [ アクティビティ出力 ] オプションを選択します。
"typeProperties": {
"scripts": [
{
"type": "Query",
"text": "DO $$ BEGIN RAISE Notice 'Hello'; RAISE Notice 'World!'; END $$;"
}
],
"scriptBlockExecutionTimeout": "02:00:00",
"logSettings": {
"logDestination": "ActivityOutput"
}
}