第8页
斯潘4
本文将继续探索使用FME进行API调用。我们以前通过制作API呼叫来工作JSON Reader. 这种方法是在FME中使用api的最简单方法,它使用硬编码的请求URL来检索FME中的空气质量和污染数据AirVisual的REST API. 由于读卡器没有输入端口,我们无法使用其他数据集的属性值进行动态API调用。要创建提供更大灵活性和功能的API工作流,我们建议使用几个转换器。亚搏在线
以下演示将提供有关如何使用三种不同的变压器访问AirVisual的API的说明,所有这些都会产生与其相同的输出KML文件以前的教程. 本演示的目的是比较允许用户访问API的转换器。它们都提供了不同的好处,并且根据API的需求和期望的输出具有最佳的用例。
我们将通过它来调用APIFeatureReader,这JSONExtractor,和HTTPCaller。我们将通过最简单到最复杂的每种方法,描述每个变压器的主要差异和推荐用例。每个方法的可用性取决于响应格式和API的要求。您可以从这些变压器中的每一个访问AIRVISUAL的API,因为:
To create the KML file, the first few steps for each of the three methods will be the same. If you have already retrieved an API key from AirVisual, you may continue with this demonstration. If not, follow steps 1 and 2 from the如何使用JSON阅读器访问API文章检索您自己的API密钥。如果您想使用Safe软件总部的坐标进行跟踪,请使用以下请求URL:亚搏在线
http://api.airvisual.com/v2/nearest_city?lat=49.178&lon=-122.842&key=
从“启动”页面中选择“新建工作区”以打开空白画布。
参数可以留下默认,因为此变形器仅用于启动工作区而无需输入数据。如果要创建动态请求URL,则可以使用包含有效GPS坐标和API密钥的现有工作流替换创建者,因为这些是所需的请求URL的唯一三个元素。亚搏在线出于本演示的目的,我们将使用创建者确保每种方法都是可比的和一致的。
The FeatureReader is a transformer that acts similarly to a reader but is incorporated into the workflow mid-translation. This means that there is an input port that requires initiation for the feature to be read. Like a typical reader, the FeatureReader can also read JSON, outputting values into an attribute with a specified coordinate system. The main difference between a FeatureReader and a reader is the presence of the input port, which allows for reading data mid-translation. The best use cases for this API approach would include situations where:
featureereader亚搏在线工作流程
将Creator连接到FeatureReader的Initiator端口。在参数中,将格式设置为JSON(JavaScript对象表示法)和坐标系到LL84。这将确保在创建点功能时,我们的纬度和经度值将被正确解释。数据集将是具有API键的请求URL替换其中
在启用功能缓存的情况下运行工作区,以便在可视化预览中查看输出。选择JSONFeature输出端口并在visualpreview(Ctrl+Alt+F)中打开featureinformation窗口以查看属性列表。与JSON读取器一样,响应中的每个值都被压平为一个属性。
The remaining steps to create geometry, manage attributes and write to KML are the same as steps 5 to 9 covered in the如何使用JSON阅读器访问APIarticle. For a detailed description of each of the following steps, please refer to the linked article.
从FeatureReader将vertexcator连接到JSONFeature输出端口。AirVisual以JSON数组返回最近监控站的坐标。因为这些值作为列出属性,您必须指定您打算通过其索引使用的值,这些值在Crly括号之间指示。请注意,JSON索引以0开始,而不是1。
In the parameters of your VertexCreator, set the X Value as the first object in the coordinate array (Longitude), data.location.coordinates{0}, and the Y Value as the second (Latitude), data.location.coordinates{1}.
Add an AttributeManager and open the parameters. Double-click the cells under Output Attribute to rename your attributes. Rename the following attributes:
使用表下方的黑色箭头排列输出属性的顺序。要删除额外的属性,请使用Ctrl或Shift键选择它们的行,然后单击表下方的减号按钮。删除其余属性。
为output.kml文件选择一个文件目录和名称。如果尚未设置要素类型定义,请将其更改为“自动”,然后单击“确定”。当“要素类型”窗口提示时,将要素类型名称重命名为MonitoringStation\U FeatureReader。将AttributeManager输出端口连接到KML编写器功能类型。
Click the green Run button in the toolbar and if the translation is successful, go to your file directory to make sure the .kml file was written.
在googleearth中,进入File→Open→Your.kml文件
The view should automatically zoom to the location of your nearest monitoring station. Click on the pin to view your attribute data.
FME Desktop has a variety of JSON-specific transformers. Most of them allow the JSON input source to be a URL, meaning these tools are perfect for reading, parsing, and validating JSON data obtained from the web. The JSONExtractor in particular provides the best parsing capability, allowing users to easily create feature attributes out of the nested values buried in JSON format. Unlike the JSON Reader or the FeatureReader, the JSONExtractor will not automatically create attributes out of every value in the JSON text. Users have to specify their desired output attributes by selecting them from a JSON query. The benefit of using this transformer is that you will only output what you need from the API response. This is a handy feature that limits the amount of attribute handling later in the workspace, and is especially useful when an API response returns many attribute values. The best use cases for this API approach would include situations where:
JSONExtractor workflow
Connect your Creator to the JSONExtractor input port and open the parameters. Change the Input Source to File/URL and then copy your request URL into the File/URL parameter, substituting your AirVisual key where
JSON查询(JavaScript)属性访问器in this case) describe the path to your attribute by fragmenting your data into its nested levels. If the value you need is deeply nested, your JSON query will be longer. Rather than manually typing in your query, we will select each one from the JSON tree.
首先填写JSONExtractor参数中的Target属性列。这包括我们之前使用的八个属性名,以及纬度和经度。
Next, select the blank space under the JSON Query column to expose the ellipses. Select the ellipses to view the JSON tree. Use the arrows to expand your JSON until you find the right attributes. For example, to find “city”, we would only need to expand “data”. When you click OK, the blank space will automatically populate with your JSON query.
有些属性更难识别,因为它们不是城市,州和国家的自我解释。对于这些缩写的值,您必须引用API文档解码你的反应。其他属性将在数组中返回,例如坐标值。要指定所需的数组值,首先选择
与JSON读取器和FeatureReader不同,纬度和经度属性可以直接输入到vertexcator中,而不必担心数组,因为我们已经在上一步中分离了坐标。将X值设置为经度属性,将Y值设置为纬度。
Since there is no option to set a coordinate system in the JSONExtractor, we have to use a CoordinateSystemSetter to specify how the X and Y values will be read. Set the coordinate system to LL84.
添加AttributeManager并打开其参数。您将注意到,只有您提取的10个原始属性,加上\u creation\u实例,它是创建者的产品。使用出现在“操作”列下单元格中的下拉菜单,删除“创建实例”、“纬度”和“经度”属性。
为output.kml文件选择一个文件目录和名称。如果尚未设置要素类型定义,请将其更改为“自动”,然后单击“确定”。当出现“要素类型”窗口提示时,将要素类型名称重命名为MonitoringStation\u JSONExtractor。将AttributeManager输出端口连接到KML编写器功能类型。
Click the green Run button in the toolbar and if the translation is successful, go to your file directory to make sure the .kml file was written.
在googleearth中,进入File→Open→Your.kml文件
The view should automatically zoom to the location of your nearest monitoring station. Click on the pin to view your attribute data.
The HTTPCaller is well-equipped to handle thousands of APIs without the need for coding. It accesses a request URL using a specified HTTP/HTTPS method, with the option to authenticate by username/password, named connection, headers, or uploaded bodies. With requests, users have the option to modify their query string parameters, headers, and if they were to PUT, POST, PATCH or DELETE, there is the option to upload a body. Responses can be saved to an attribute or file, while also providing error and status codes in the response header. The use cases for this API approach would include situations where:
HTTPCaller工亚搏在线作流
将创建者连接到输入端口并打开参数。要提高可读性,请使用查询字符串参数分解请求URL的组件。输入以下URL作为请求URL,并将HTTP方法设置为GET。
http://api.airvisual.com/v2/nearest_city?
HttpCaller允许您将URL查询字符串添加为变压器参数。在名称列中输入LAT,LON和键,然后输入49.178,-122.842和您的API键>出现的值列下的API键。查询字符串参数是设置动态API呼叫的好地方,因为您可以使用属性或包含属性替换值列中的任何单元格用户参数. 包含用户参数的动态调用包含在下一篇文章这个系列的。其余参数可以保留为默认值。
在启用功能缓存的情况下运行工作区,以便在可视化预览中查看响应正文。如果使用提供的坐标,\u response\u body属性应返回类似于以下内容的内容:
{“status”:“success”,“data”:{“city”:“New Westminster”,“state”:“British Columbia”,“country”:“Canada”,“location”:{“type”:“Point”,“coordinates”:[-122.9109199999999999,49.20678]},“current”:{“weather”:{“ts”:“2019-05-17T16:00:00.000Z”,“hu”:71,“ic”:“01d”,“pr”:1008,“tp”:12,“wd”:160,“ws”:3.6},“pollution”:{“ts”:“2019-05-17T15:00.000Z”,“aqius”:12,“mainus”:“p2”,“aqicn”:24,“maincn”:“n2”}}
我们必须手动创建查询,因为当从属性读取JSON文档时,JSONExtractor不显示JSON树。因此,我们应该格式化JSON,以便读取其结构来构造查询。
我们可以用jsonformatter.。连接HTTPCaller JSONFormatte输出r and set the JSON Document as your _response_body attribute. Leave the remaining parameters as default, and click OK. Run the workspace with Feature Caching enabled to view the reformatted JSON in Visual Preview. You should see something similar to this:
{“status”:“success”,“data”:{“city”:“New Westminster”,“state”:“British Columbia”,“country”:“Canada”,“location”:{“type”:“Point”,“coordinates”:[-122.9109199999999999,49.20678]},“current”:{“weather”:{“ts”:“2019-05-17T16:00:00.000Z”,“hu”:71,“ic”:“01d”,“pr”:1008,“tp”:12,“wd”:160,“ws”:3.6},“污染”:{“ts”:“2019-05-17T15:00:00.000Z”,“aqius”:12,“缅因州”:“p2”,“aqicn”:24,“缅因州”:“n2”}}
JSON文本现在用缩进和空格格式化,以提高可读性。嵌套结构布局清晰,便于构造json查询。例如,如果我们想根据美国的空气质量指数查询主要污染物,我们可以看到“母羊”嵌套在“污染”下嵌套在“当前”下嵌套在“数据”下。使用此逻辑,我们可以创建JSON查询:
JSON [“数据”] [“当前”] [“污染”] [“凸起”]
要查询数组中的值(如监视站的坐标),可以使用相同的逻辑,但要指定数组中的对象,必须指示位号。例如,对经度的JSON查询是:
json[“数据”][“位置”][“坐标”][0]
我们使用Zero表示经度,因为它是数组中的第一个值。如果我们想指定纬度,我们将使用1作为方括号之间的位置编号。
In the JSONExtractor, set the JSON Document as _response_body or _formatted. These both contain the same JSON text, but one is formatted with indentation and spaces. Reformatting will not impact how JSON queries are executed.
使用每个输出属性的名称填写目标属性列,如下所示:
Under the JSON Query column, populate each cell with its corresponding query using the logic described in the previous step. It may be helpful to have the reformatted response body open in another window such as Notepad. Refer to theAPI文档解码响应体中的缩写值。
The Latitude and Longitude attributes can be directly inputted into the VertexCreator without having to worry about arrays because we have already separated the coordinates in the previous step. Set the X Value as your Longitude attribute and the Y Value as your Latitude.
因为在HTTPCaller中没有设置坐标系的选项,所以我们必须使用CoordinateSystemSetter来指定如何读取X和Y值。将坐标系设置为LL84。
添加attributemanager并打开其参数。通过使用“操作”列下的下拉菜单选择“删除,删除_Creation_instance,_Response_Body,_http_status_code,经度,纬度和_formatted。
Select a file directory and name for your output .kml file. Change the Feature Type Definition to Automatic if it’s not already set and click OK. Rename the Feature Type Name to MonitoringStation_HTTPCaller when prompted with the Feature Type window. Connect the AttributeManager Output port to the KML writer feature type.
Click the green Run button and if the translation is successful, go to your file directory to make sure the .kml file was saved.
在googleearth中,进入File→Open→Your.kml文件
The view should automatically zoom to the location of your nearest monitoring station. Click on the pin to view your attribute data.
所描述的三种方法中的每一种都将生成相同的.kml文件,因此如果使用提供的坐标,则在所有三种情况下都会看到类似的内容:
kml文件包含八个属性,这些属性在FME中使用不同的转换器进行解析、重命名和重新排序。在考虑最终产品时,最有效的方法是JSONExtractor,因为它允许我们过滤掉select值,从而限制了以后在工作流中处理属性的数量。此外,JSON树允许我们轻松地创建查询,而不需要任何JSON格式的知识。如果我们希望每个响应值都是输出属性,那么FeatureReader将是最佳选择。尽管HTTPCaller方法也包含一个JSONExtractor,但是我们必须手动输入查询,因为当输入数据源是动态的时,JSON树不是一个可用的特性。使用这种方法,需要同时具备API和JSON的工作知识,因为有更多的手动输入。亚搏在线
HTTPCaller在访问API时提供了最多的用户控制,但是在本文中,这些特性并没有突出显示。有关如何利用HTTPCaller特性的演示,请访问下一篇文章在API系列文章“How to Access an API using the HTTPCaller”中。您将学习如何访问Movebank REST API,该API返回一个包含活体动物运动数据的.csv文件。
©2019 S亚搏在线afe Software Inc |法律