使用HTTP输入插件
本示例演示了如何使用Telegraf HTTP输入插件收集纽约市Citi Bike站点的实时指标。可以从NYC OpenData获取实时站点数据,格式为JSON。
配置 influxdb 输出插件 以将指标写入您的 InfluxDB 2.7 实例。
在你的Telegraf配置文件中配置HTTP输入插件
要从Citi Bike 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
此命令应返回类似于以下内容的行协议:
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中探索和查询Citi Bike数据。