使用 HTTP 输入插件
本示例演示如何使用 Telegraf HTTP 输入插件来收集纽约市花旗单车站点的实时指标。实时站点数据以 JSON 格式从 NYC OpenData 提供。
配置 influxdb
输出插件 以将指标写入您的 InfluxDB 2.7 实例。
在您的 Telegraf 配置文件中配置 HTTP 输入插件
要从花旗单车 URL 端点检索数据,请在您的 Telegraf 配置文件中启用 inputs.http
输入插件。
指定以下选项
urls
从中读取指标的一个或多个 URL。对于本示例,请使用 https://gbfs.citibikenyc.com/gbfs/en/station_status.json
。
data_format
Telegraf 将摄取的 HTTP 端点中数据的格式。对于本示例,请使用 JSON。
将解析器信息添加到您的 Telegraf 配置
指定以下 JSON 特定的选项。
JSON
json_query
要仅解析 JSON 数据的相关部分,请使用 GJSON 路径设置 json_query
选项。查询结果应包含 JSON 对象或对象数组。在本例中,我们不想解析数据开头的 JSON 查询的 executionTime
,因此我们将限制它仅包含 stationBeanList
数组中的数据。
tag_keys
应添加为标签的一个或多个 JSON 键的列表。对于本示例,我们将使用标签键 id
、stationName
、city
和 postalCode
。
json_string_fields
以字符串格式存在的字段的键列表,以便可以将它们解析为字符串。这里,字符串字段为 statusValue
、stAddress1
、stAddress2
、location
和 landMark
。
json_time_key
JSON 文件中创建时间戳指标的键。在本例中,我们希望使用上次报告站点数据的时间,即 lastCommunicationTime
。如果您未指定键,则 Telegraf 读取数据的时间将成为时间戳。
json_time_format
用于解释指定的 json_time_key
的格式。本示例使用 Go 参考时间格式。例如,Mon Jan 2 15:04:05 MST 2006
。
json_timezone
时区。我们将此设置为我们的单车数据发生地的 Unix TZ 值,America/New_York
。
配置示例
[[inputs.http]]
#URL for NYC's Citi Bike station data in JSON format
urls = ["https://feeds.citibikenyc.com/stations/stations.json"]
#Overwrite measurement name from default `http` to `citibikenyc`
name_override = "citibikenyc"
#Exclude url and host items from tags
tagexclude = ["url", "host"]
#Data from HTTP in JSON format
data_format = "json"
#Parse `stationBeanList` array only
json_query = "stationBeanList"
#Set station metadata as tags
tag_keys = ["id", "stationName", "city", "postalCode"]
#Do not include station landmark data as fields
fielddrop = ["landMark"]
#JSON values to set as string fields
json_string_fields = ["statusValue", "stAddress1", "stAddress2", "location", "landMark"]
#Latest station information reported at `lastCommunicationTime`
json_time_key = "lastCommunicationTime"
#Time is reported in Golang "reference time" format
json_time_format = "2006-01-02 03:04:05 PM"
#Time is reported in Eastern Standard Time (EST)
json_timezone = "America/New_York"
启动 Telegraf 并验证数据是否出现
要测试数据是否已发送到 InfluxDB,请运行以下命令(将 telegraf.conf
替换为您的配置文件的路径)
telegraf -config ~/telegraf.conf -test
此命令应返回类似于以下内容的 Line Protocol
citibikenyc,id=3443,stationName=W\ 52\ St\ &\ 6\ Ave statusKey=1,location="",totalDocks=41,availableDocks=32,latitude=40.76132983124814,longitude=-73.97982001304626,availableBikes=8,stAddress2="",stAddress1="W 52 St & 6 Ave",statusValue="In Service" 1581533519000000000
citibikenyc,id=367,stationName=E\ 53\ St\ &\ Lexington\ Ave availableBikes=8,stAddress1="E 53 St & Lexington Ave",longitude=-73.97069431,latitude=40.75828065,stAddress2="",statusKey=1,location="",statusValue="In Service",totalDocks=34,availableDocks=24 1581533492000000000
citibikenyc,id=359,stationName=E\ 47\ St\ &\ Park\ Ave totalDocks=64,availableBikes=15,statusValue="In Service",location="",latitude=40.75510267,availableDocks=49,stAddress1="E 47 St & Park Ave",longitude=-73.97498696,statusKey=1,stAddress2="" 1581533535000000000
citibikenyc,id=304,stationName=Broadway\ &\ Battery\ Pl statusValue="In Service",availableDocks=11,stAddress1="Broadway & Battery Pl",statusKey=1,stAddress2="",location="",totalDocks=33,latitude=40.70463334,longitude=-74.01361706,availableBikes=22 1581533499000000000
现在,您可以在 InfluxDB 中探索和查询花旗单车数据。
此页内容对您有帮助吗?
感谢您的反馈!