2

我正在从 api 访问天气数据,并想使用 Apache nifi 来获取第一个 api 返回的 json 数组中所有城市的天气数据?

基本上我通过 2 个 API 访问数据:

这个 api 返回一个 JSON 数组,其中包含 JSON 数组元素中美国所有城市的列表:

{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "conditions": 1
  }
        , "results": [
        {
        "name": "Keyhole",
        "city": "Keyhole",
        "state": "WY",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "82721.7.99999",
        "l": "/q/zmw:82721.7.99999"
        }
        ,
        {
        "name": "Cuchara Valley Airport At La Veta",
        "city": "Cuchara Valley Airport At La Veta",
        "state": "CO",
        "country": "US",
        "country_iso3166":"US",
        "country_name":"USA",
        "zmw": "81055.6.99999",
        "l": "/q/zmw:81055.6.99999"
        }
        ,

现在我想遍历这个数组并使用要在另一个 API 中使用的州和城市信息,如下所示:

{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "conditions": 1
  }
    }
  , "current_observation": {
        "image": {
        "url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
        "title":"Weather Underground",
        "link":"http://www.wunderground.com"
        },
        "display_location": {
        "full":"San Francisco, CA",
        "city":"San Francisco",
        "state":"CA",
        "state_name":"California",
        "country":"US",
        "country_iso3166":"US",
        "zip":"94101",
        "magic":"1",
        "wmo":"99999",
        "latitude":"37.77500916",
        "longitude":"-122.41825867",
        "elevation":"47.00000000"
        },
        "observation_location": {
        "full":"SOMA - Near Van Ness, San Francisco, California",
        "city":"SOMA - Near Van Ness, San Francisco",
        "state":"California",
        "country":"US",
        "country_iso3166":"US",
        "latitude":"37.773285",
        "longitude":"-122.417725",
        "elevation":"49 ft"
        },...

所以基本上我必须为我从第一个 API 获得的所有州和城市组合循环运行第二个 api。

到目前为止,我所做的基本 NiFi 流程与这个问题一样,但它不起作用。有人可以帮我弄清楚我在这里做错了什么吗?

HDP 2.4 沙盒上的 Hortonworks 数据流

SplitJson 处理器配置

4

1 回答 1

2

回答评论中暴露的问题:

对于 SplitJSON,您要使用的 JSON 路径表达式是*.results.*将 JSON 拆分为每个结果的 FlowFile。您拥有的 EvaluateJSONPath 配置应该与更新的 SplitJSON 一起使用(刚刚在模板中测试过)。

您为 InvokeHttp URL 提供的表达式语言 (EL) 几乎就在那里。您需要做的一件事是使用 EL“urlEncode”对城市和州进行 URL 编码,如下所示:api.wunderground.com/api/key/conditions/q/${state:urlEncode()}/${city:urlEncode()}.j‌​son

于 2016-04-13T15:06:37.800 回答