执行全外连接
使用 join.full() 执行两个数据流的完全外连接。
完全外连接会输出左侧和右侧输入流中的所有行,并根据 on 谓词连接匹配的行。
使用 join.full 来连接你的数据
导入
join包。定义要连接的 左 和 右 数据流:
- 每个数据流必须有一个或多个具有共同值的列。 列标签不需要匹配,但列值必须匹配。
- 每个流应该具有相同的 group keys。
有关更多信息,请参见 join data requirements。
使用
join.full()将两个流连接在一起。
提供以下必需参数:left: 表示连接左侧的数据流。right: 表示连接右侧的数据流。on: 连接条件. For example:(l, r) => l.column == r.column.as: 连接输出函数 返回包含来自每个输入流的值的记录。考虑缺失的非分组键值
在全外连接中,左侧 (
l) 或右侧 (r) 可能对用于连接操作的列包含 null 值,并默认为一个默认记录(组键列被填充,其他列是 null)。l和r不会同时使用默认记录。为了确保在非分组键列的输出中包含非空值,检查
l或r记录中是否存在值,并返回存在的值:(l, r) => { id = if exists l.id then l.id else r.id return {_time: l.time, location: r.location, id: id} }
以下示例使用来自
machineProduction 示例数据集
的过滤选择作为左数据流,并使用array.from()
创建的临时表作为右数据流。
示例数据分组
下面的示例将左流解组,以匹配右流的分组。在将两个流连接在一起后,连接的数据按stationID分组,并按_time排序。
import "array"
import "influxdata/influxdb/sample"
import "join"
left =
sample.data(set: "machineProduction")
|> filter(fn: (r) => r.stationID == "g1" or r.stationID == "g2" or r.stationID == "g3")
|> filter(fn: (r) => r._field == "oil_temp")
|> limit(n: 5)
right =
array.from(
rows: [
{station: "g1", opType: "auto", last_maintained: 2021-07-15T00:00:00Z},
{station: "g2", opType: "manned", last_maintained: 2021-07-02T00:00:00Z},
{station: "g4", opType: "auto", last_maintained: 2021-08-04T00:00:00Z},
],
)
join.full(
left: left |> group(),
right: right,
on: (l, r) => l.stationID == r.station,
as: (l, r) => {
stationID = if exists l.stationID then l.stationID else r.station
return {
stationID: stationID,
_time: l._time,
_field: l._field,
_value: l._value,
opType: r.opType,
maintained: r.last_maintained,
}
},
)
|> group(columns: ["stationID"])
|> sort(columns: ["_time"])