LoginSignup
0
0

Synapse studioのパイプラインでSQLスクリプトを扱う

Posted at

この記事に書いてあること

Synapse Studioでパイプラインを構築する際のTips。
主に「スクリプト」アクティビティと「変数の設定」アクティビティの使い方になります。

スクリプトアクティビティの出力を利用する

スクリプトの出力を別のスクリプトで読み込む

スクリプトでちょっとした処理を行い、その結果を別のスクリプトで利用したいというときは以下のように設定します。
image.png
ここではScript1とScript2を用意してScript1の出力をそのままScript2で読み込んで出力します。クエリ内容は以下の通りです。

-- Script1
select
    1 as result1
    ,2 as result2
;
-- Script2
select
    @{activity('Script1').output.resultSets[0].rows[0].result1} as result
;

Script1の出力

{
	"resultSetCount": 1,
	"recordsAffected": 0,
	"resultSets": [
		{
			"rowCount": 1,
			"rows": [
				{
					"result1": 1,
					"result2": 2
				}
			]
		}
	],
	以下略
}

Script2の出力

{
	"resultSetCount": 1,
	"recordsAffected": 0,
	"resultSets": [
		{
			"rowCount": 1,
			"rows": [
				{
					"result": 1
				}
			]
		}
	],
	以下略
}

出力が複数行にわたる場合はrowsの中に増えていきます。

文字列と数値の違い

先ほどのScript1ではresult1,result2に格納されていたデータは数値でした。
これを文字列に変更するとScript2実行時に以下のようなエラーが発生します。

-- Script1
select
    'test1' as result1
    ,'test2' as result2
;

-- Script2
select
    @{activity('Script1').output.resultSets[0].rows[0].result1} as result
;

エラー文

{
    "errorCode": "2011",
    "message": "Invalid column name 'test1'.",
    "failureType": "UserError",
    "target": "Script2",
    "details": []
}

これはScript2の中で ' ' によって囲われていない文字が列名として認識されていることが原因です。Script2を以下のように変更する必要があります。

-- Script2
select
    '@{activity('Script1').output.resultSets[0].rows[0].result1}' as result
;

Script2の出力

{
	"resultSetCount": 1,
	"recordsAffected": 0,
	"resultSets": [
		{
			"rowCount": 1,
			"rows": [
				{
					"result": "test1"
				}
			]
		}
	],
	以下略
}

スクリプトの出力を変数に格納する

今度は以下のようにスクリプトの出力を変数に格納して、別のスクリプトでその変数を利用します。
image.png
Set variable1ではScript1の出力をtestというパイプライン変数に格納しています。

ちなみにパイプライン変数に似たものでパイプラインパラメーターというものがあります。パイプラインパラメーターはパイプライン変数と似たような形で使えますがパイプライン実行後に上書きすることができないため、スクリプトの結果を格納できるのはパイプライン変数になります。

-- Script1
select
    1 as result1
    ,2 as result2
;
-- Script2
select
+    @{varibles('test')} as result
-    @{activity('Script1').output.resultSets[0].rows[0].result1} as result
;
-- Set variable1
@activity('Script1').output.ResultSets[0].rows[0].result1

Script1の実行結果

{
	"resultSetCount": 1,
	"recordsAffected": 0,
	"resultSets": [
		{
			"rowCount": 1,
			"rows": [
				{
					"result1": 1,
					"result2": 2
				}
			]
		}
	],
	以下略
}

Script2の実行結果

{
	"resultSetCount": 1,
	"recordsAffected": 0,
	"resultSets": [
		{
			"rowCount": 1,
			"rows": [
				{
					"result": 1
				}
			]
		}
	],
	以下略
}

文字列と数値の違い

Script1の出力を文字列にしてみます。

select
    'test1' as result1
    ,'test2' as result2
;
-- Script2
select
    @{variables('test')} as result
;
-- Set variable1
@activity('Script1').output.ResultSets[0].rows[0].result1

Set variables1に変更はありませんがパイプライン変数の型をintegerからstringに変更します。
Script2でScript1の結果を直接受け取った時と同様のエラーが出ました。

{
    "errorCode": "2011",
    "message": "Invalid column name 'test1'.",
    "failureType": "UserError",
    "target": "Script2",
    "details": []
}

ちなみに、set variables1の出力は以下のようになっています。

{
	"name": "test",
	"value": "test1"
}

先ほどと同様にScript2で文字列を受け取れるように変更します。

-- Script2
select
+    '@{variables('test')}' as result
-    @{variables('test')} as result
;

無事にScript2で結果を取得することができました。

{
	"resultSetCount": 1,
	"recordsAffected": 0,
	"resultSets": [
		{
			"rowCount": 1,
			"rows": [
				{
					"result": "test1"
				}
			]
		}
	],
	以下略
}

注意しなければならないのは、Set variable1で変数に格納する値が文字列であっても ' ' で囲う必要はないというところです。
例えば以下のように書いてしまうと、'Script1'のところでSynax Errorになります。

-- set variable1
'@activity('Script1').output.ResultSets[0].rows[0].result1'
0
0
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
0
0