8

尝试在 sqlActivity 中使用脚本参数时:

 {
"id" : "ActivityId_3zboU",
  "schedule" : { "ref" : "DefaultSchedule" },
  "scriptUri" : "s3://location_of_script/unload.sql",
  "name" : "unload",
  "runsOn" : { "ref" : "Ec2Instance" },
  "scriptArgument" : [ "'s3://location_of_unload/#format(minusDays(@scheduledStartTime,1),'YYYY/MM/dd/hhmm/')}'", "'aws_access_key_id=????;aws_secret_access_key=*******'" ],
  "type" : "SqlActivity",
  "dependsOn" : { "ref" : "ActivityId_YY69k" },
  "database" : { "ref" : "RedshiftCluster" }
}

unload.sql 脚本包含:

 unload ('
    select *
    from tbl1 
 ')  
 to ?
 credentials  ?
 delimiter ',' GZIP;

或者 :

 unload ('
    select *
    from tbl1 
 ')  
 to ?::VARCHAR(255)
 credentials  ?::VARCHAR(255) 
 delimiter ',' GZIP;

过程失败:

syntax error at or near "$1" Position

知道我做错了什么吗?

4

3 回答 3

5

这是在 psql shell 中运行良好的脚本:

insert into tempsdf select * from source where source.id = '123';

以下是我使用 Data-Pipelines对SqlActivity进行的一些测试:


测试 1:使用

insert into mytable select * from source where source.id = ?;- 如果通过 SqlActivity 对象上的 'script' 和 'scriptURI' 选项使用,则工作正常。

在哪里"ScriptArgument" : "123"

这里 ?可以替换条件的值,但不能替换条件本身。


测试 2:仅使用“脚本”选项指定命令时使用参数有效

insert into #{myTable} select * from source where source.id = ?;- 如果仅通过“脚本”选项使用,则可以正常工作

insert into #{myTable} select * from source where source.id = #{myId};
  • 如果仅通过“脚本”选项使用,则可以正常工作

其中#{myTable}, #{myId}是可以在模板中声明其值的参数。

http://docs.aws.amazon.com/datapipeline/latest/DeveloperGuide/dp-custom-templates.html

(当您只使用参数时,请确保删除未使用的 scriptArguments - 否则它仍然会抛出错误)


失败的测试和推论:

插入 ?select * from source where source.id = ?;

插入 ?select * from source where source.id = '123';

以上两个命令都不起作用,因为

表名不能用作脚本参数的占位符。'?' 只能用于传递比较条件和列值的值。


插入#{myTable} select * from source where source.id = #{myId}; - 如果用作“SciptURI”则不起作用

插入 tempsdf select * from source where source.id = #{myId}; - 与“ScriptURI”一起使用时不起作用

以上 2 个命令不起作用,因为

如果脚本存储在 S3 中,则无法评估参数。


插入 tempsdf select * from source where source.id = $1 ; - 不适用于“scriptURI”

插入 tempsdf 值 ($1,$2,$3); - 不工作。

使用 $'s - 在任何组合中都不起作用


其他测试:

“ScriptArgument”:“123” “ScriptArgument”:“456” “ScriptArgument”:“789”

insert into tempsdf values (?,?,?);- 作为 scriptURI , script 和转换为insert into tempsdf values ('123','456','789');

scriptArguments 将按照您插入的顺序并替换“?” 在脚本中。


于 2016-03-02T23:39:20.250 回答
1

在 shellcommand 活动中,我们使用 shell script(.sh) 中的 $1 $2 指定两个 scriptArguments 来访问

"scriptArgument" : "'s3://location_of_unload/#format(minusDays(@scheduledStartTime,1),'YYYY/MM/dd/hhmm/')}'", # 可以使用 $1 "scriptArgument" : "' aws_access_key_id=????;aws_secret_access_key=*******'" # 可以使用 $2 访问

我不知道这对你有用。

于 2014-12-23T08:34:56.310 回答
0

我相信您正在将此 sql 活动用于 Redshift。您能否修改您的 sql 脚本以使用它们的位置符号来引用参数。要引用 sql 语句本身中的参数,使用 $1、$2 等。

http://www.postgresql.org/docs/9.1/static/sql-prepare.html

于 2014-12-16T01:23:35.627 回答