如何在分割JSON后使用Apache NiFi检索属性并在invokeHttp处理器中递归使用这些属性?

时间:2016-04-08 05:03:27

标签: json hortonworks-data-platform apache-nifi

我是否从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流程与此问题相关,但它无效。有人可以帮我弄清楚我在这里做错了什么吗?

Hortonworks Dataflow on HDP 2.4 Sandbox

SplitJson Processor Configuration

1 个答案:

答案 0 :(得分:2)

回答评论中暴露的问题:

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

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