我正在从 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 流程已附在这个问题上,但它不起作用。有人可以帮我弄清楚我在这里做错了什么吗?
最佳答案
回答评论中暴露的问题:
对于 SplitJSON,您要使用的 JSON 路径表达式是 *.results.*
这会将 JSON 拆分为每个结果的 FlowFile。您拥有的 EvaluateJSONPath 配置应该与更新的 SplitJSON 一起使用(刚刚在模板中进行了测试)。
您为 InvokeHttp URL 提供的表达式语言 (EL) 已基本完成。您需要做的一件事是使用 EL“urlEncode” 对城市和州进行 URL 编码,如下所示:api.wunderground.com/api/key/conditions/q/${state:urlEncode()}/${城市:urlEncode()}.json
关于json - 如何使用 Apache NiFi 在拆分 JSON 后检索属性并在 invokeHttp 处理器中递归使用这些属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36491901/