【问题标题】:Amazon Data Pipeline: How to use a script argument in a SqlActivity?Amazon Data Pipeline:如何在 SqlActivity 中使用脚本参数?
【发布时间】:2015-02-13 09:14:14
【问题描述】:

当尝试在 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

知道我做错了什么吗?

【问题讨论】:

  • 嘿,你能让它工作吗?我也面临一个类似的问题,我想将多个参数传递给一个 sql 文件。如果解决了,请告诉我参数的格式和在 sql 文件中的访问方式。
  • @marnun 你最后是怎么让它工作的?我正在尝试做同样的事情,但无法正常工作。
  • @ChiragAgrawal 查看 AravindR 的答案

标签: amazon-web-services amazon-s3 amazon-redshift amazon-data-pipeline


【解决方案1】:

这是在 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';

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

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


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

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

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

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


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

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

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


其他测试:

“ScriptArgument”:“123” “脚本参数”:“456” “脚本参数”:“789”

insert into tempsdf values (?,?,?); - 既可以用作 scriptURI ,也可以用作 script 并转换为 insert into tempsdf values ('123','456','789');

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


【讨论】:

  • 我发现这篇文章比公认的更准确。
【解决方案2】:

在 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 访问

我不知道这对你有用。

【讨论】:

  • 它会的,因为它是第一个答案的解决方案
【解决方案3】:

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

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

【讨论】:

  • 您能否展示一些示例代码,说明如何为 redshift 中的卸载命令实现这一点?
  • 我使用了to $1,我得到了The column index is out of range: 1, number of columns: 0.
  • 这对我不起作用。我也得到 The column index is out of range: 1, number of columns: 0.
  • @aravindr 你有卸载脚本的工作示例吗?当我尝试使用 $1 或 ? 引用参数时,我收到错误 The column index is out of range: 1, number of columns: 0
  • 我能够完成这项工作的唯一方法是在 sql 脚本本身中使用 ?。此外,我只能让参数在 SELECT, INSERT, UPDATE, and DELETE 语句中工作。我无法让unload 语句接受参数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-27
  • 1970-01-01
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多