您还可以充分利用 uritemplate 表将机场代码转换为一组 URL,然后将这些 URL 提供给 json 表。
机场代码到网址
uritemplate 表采用“模板”和任意数量的值来填充该模板。 (另请参阅URI Template 草稿)。
在这种情况下,需求很简单,只需将机场代码注入到适当位置的路径中即可。
select * from uritemplate where
template="http://airportcode.riobard.com/airport/{airport}?fmt=JSON"
and airport in ("SFO", "LAX", "LHR")
(Try this query in the YQL console)
这给出了以下结果(为简单起见,删除了diagnostics 信息)。
从这些 URL 获取 JSON 结果
现在我们有了一个 URL 列表,使用 json 表通过 sub-select 检索每个 URL 的 JSON 非常简单。事实上,我们已经在上面使用了一个子选择,可以通过 in (…) 语法识别。
select * from json where url in (
select url from uritemplate
where template="http://airportcode.riobard.com/airport/{airport}?fmt=JSON"
and airport in ("SFO", "LAX", "LHR")
)
(Try this query in the YQL console)
这次的结果是这样的。
轻松查询
我非常喜欢 YQL 的 query aliases(以及相关的变量替换,例如 @var),它可以提供简洁的端点供您访问,而无需将 YQL 查询嵌入到网址。上面的链接详细说明了如何创建它们,最终结果是您可以拥有如下所示的 YQL 查询。
select * from json where url in (
select url from uritemplate where
template="http://airportcode.riobard.com/airport/{airport}?fmt=JSON"
and airport in (@ap)
)
使用查询别名(我称之为 airports),可以通过如下 URL 访问查询,并且能够轻松更改或添加更多/更少的机场代码。
http://query.yahooapis.com/v1/public/yql/peter/airports?ap=SFO&ap=LHR&ap=BOB
(Go there。注意:我可能会删除此别名,恕不另行通知。)
阅读: