使用笔记本规范化数据
了解如何创建一个规范化数据的笔记本。 数据规范化是修改或清理数据的过程,以使其更易于处理。例如,包括将数值调整为统一的尺度和修改字符串。
通过下面的示例来创建一个查询 NOAA NDBC 示例数据的笔记本,将基于度的风向标准化为基本方向,然后将标准化数据写入一个存储桶。
在构建查询单元格中:
- In the FROM column under Sample, select NOAA National Buoy Data.
- In the next FILTER column, select _measurement from the drop-down list and select the ndbc measurement in the list of measurements.
- In the next FILTER column, select _field from the drop-down list, and select the wind_dir_degt field from the list of fields.
在您的 构建查询 单元格之后点击 以添加一个新单元格并选择 Flux 脚本.
在Flux脚本单元中:
定义一个自定义函数 (
cardinalDir()),该函数将数值度数转换为方位方向(北、北东北、东北等)。使用
__PREVIOUS_RESULT__将上一个笔记本单元格的输出加载到Flux脚本中。使用
map()遍历每一行输入,更新字段键为wind_dir_cardinal,并使用自定义cardinalDir()函数将_value列规范化为一个方位。
import "array" cardinalDir = (d) => { _cardinal = if d >= 348.7 or d < 11.25 then "N" else if d >= 11.25 and d < 33.75 then "NNE" else if d >= 33.75 and d < 56.25 then "NE" else if d >= 56.25 and d < 78.75 then "ENE" else if d >= 78.75 and d < 101.25 then "E" else if d >= 101.25 and d < 123.75 then "ESE" else if d >= 123.75 and d < 146.25 then "SE" else if d >= 146.25 and d < 168.75 then "SSE" else if d >= 168.75 and d < 191.25 then "S" else if d >= 191.25 and d < 213.75 then "SSW" else if d >= 213.75 and d < 236.25 then "SW" else if d >= 236.25 and d < 258.75 then "WSW" else if d >= 258.75 and d < 281.25 then "W" else if d >= 281.25 and d < 303.75 then "WNW" else if d >= 303.75 and d < 326.25 then "NW" else if d >= 326.25 and d < 348.75 then "NNW" else "" return _cardinal } __PREVIOUS_RESULT__ |> map(fn: (r) => ({r with _field: "wind_dir_cardinal", _value: cardinalDir(d: r._value), }))点击 在你的 Flux Script 单元格之后,添加一个新单元格并选择 。从 选择存储桶 下拉列表中选择一个存储桶。
(可选) 点击 并选择 注释 来添加一个包含有关此笔记本功能的注释的单元格。例如,该单元格可能会说:“此笔记本将十进制度风向值转换为方位方向。”
点击 预览 在左上角以验证您的笔记本是否运行并预览输出。
点击 运行 来运行笔记本并将规范化的数据写入您的存储桶。
持续运行一个笔记本
要持续运行您的笔记本,请将笔记本导出为任务:
点击 添加一个新单元格,然后选择 任务.
提供以下内容:
- 每个: 任务应运行的时间间隔。
- 偏移量: (可选) 在定义的时间间隔后等待的时间以执行任务。 这允许任务捕获迟到的数据。
点击 导出为任务。