Join data with Flux

Use the Flux join package to join two data sets based on common values using the following join methods:

Inner join

Join - 图1

Left outer join

Join - 图2

Right outer join

Join - 图3

Full outer join

Join - 图4

The join package lets you join data from different data sources such as InfluxDB, SQL database, CSV, and others.

Use join functions to join your data

Inner join Left join Right join Full outer join Join on time

  1. Import the join package.

  2. Define the left and right data streams to join:

    • Each stream must have one or more columns with common values. Column labels do not need to match, but column values do.
    • Each stream should have identical group keys.

    For more information, see join data requirements.

  3. Use join.inner() to join the two streams together. Provide the following required parameters:

    • left: Stream of data representing the left side of the join.
    • right: Stream of data representing the right side of the join.
    • on: Join predicate. For example: (l, r) => l.column == r.column.
    • as: Join output function that returns a record with values from each input stream. For example: (l, r) => ({l with column1: r.column1, column2: r.column2}).
  1. import "join"
  2. import "sql"
  3. left =
  4. from(bucket: "example-bucket-1")
  5. |> range(start: "-1h")
  6. |> filter(fn: (r) => r._measurement == "example-measurement")
  7. |> filter(fn: (r) => r._field == "example-field")
  8. right =
  9. sql.from(
  10. driverName: "postgres",
  11. dataSourceName: "postgresql://username:password@localhost:5432",
  12. query: "SELECT * FROM example_table",
  13. )
  14. join.inner(
  15. left: left,
  16. right: right,
  17. on: (l, r) => l.column == r.column,
  18. as: (l, r) => ({l with name: r.name, location: r.location}),
  19. )

For more information and detailed examples, see Perform an inner join in the Flux documentation.

  1. Import the join package.

  2. Define the left and right data streams to join:

    • Each stream must have one or more columns with common values. Column labels do not need to match, but column values do.
    • Each stream should have identical group keys.

    For more information, see join data requirements.

  3. Use join.left() to join the two streams together. Provide the following required parameters:

    • left: Stream of data representing the left side of the join.
    • right: Stream of data representing the right side of the join.
    • on: Join predicate. For example: (l, r) => l.column == r.column.
    • as: Join output function that returns a record with values from each input stream. For example: (l, r) => ({l with column1: r.column1, column2: r.column2}).
  1. import "join"
  2. import "sql"
  3. left =
  4. from(bucket: "example-bucket-1")
  5. |> range(start: "-1h")
  6. |> filter(fn: (r) => r._measurement == "example-measurement")
  7. |> filter(fn: (r) => r._field == "example-field")
  8. right =
  9. sql.from(
  10. driverName: "postgres",
  11. dataSourceName: "postgresql://username:password@localhost:5432",
  12. query: "SELECT * FROM example_table",
  13. )
  14. join.left(
  15. left: left,
  16. right: right,
  17. on: (l, r) => l.column == r.column,
  18. as: (l, r) => ({l with name: r.name, location: r.location}),
  19. )

For more information and detailed examples, see Perform a left outer join in the Flux documentation.

  1. Import the join package.

  2. Define the left and right data streams to join:

    • Each stream must have one or more columns with common values. Column labels do not need to match, but column values do.
    • Each stream should have identical group keys.

    For more information, see join data requirements.

  3. Use join.right() to join the two streams together. Provide the following required parameters:

    • left: Stream of data representing the left side of the join.
    • right: Stream of data representing the right side of the join.
    • on: Join predicate. For example: (l, r) => l.column == r.column.
    • as: Join output function that returns a record with values from each input stream. For example: (l, r) => ({l with column1: r.column1, column2: r.column2}).
  1. import "join"
  2. import "sql"
  3. left =
  4. from(bucket: "example-bucket-1")
  5. |> range(start: "-1h")
  6. |> filter(fn: (r) => r._measurement == "example-measurement")
  7. |> filter(fn: (r) => r._field == "example-field")
  8. right =
  9. sql.from(
  10. driverName: "postgres",
  11. dataSourceName: "postgresql://username:password@localhost:5432",
  12. query: "SELECT * FROM example_table",
  13. )
  14. join.right(
  15. left: left,
  16. right: right,
  17. on: (l, r) => l.column == r.column,
  18. as: (l, r) => ({l with name: r.name, location: r.location}),
  19. )

For more information and detailed examples, see Perform a right outer join in the Flux documentation.

  1. Import the join package.

  2. Define the left and right data streams to join:

    • Each stream must have one or more columns with common values. Column labels do not need to match, but column values do.
    • Each stream should have identical group keys.

    For more information, see join data requirements.

  3. Use join.full() to join the two streams together. Provide the following required parameters:

    • left: Stream of data representing the left side of the join.
    • right: Stream of data representing the right side of the join.
    • on: Join predicate. For example: (l, r) => l.column == r.column.
    • as: Join output function that returns a record with values from each input stream. For example: (l, r) => ({l with column1: r.column1, column2: r.column2}).

Full outer joins must account for non-group-key columns in both l and r records being null. Use conditional logic to check which record contains non-null values for columns not in the group key. For more information, see Account for missing, non-group-key values.

  1. import "join"
  2. import "sql"
  3. left =
  4. from(bucket: "example-bucket-1")
  5. |> range(start: "-1h")
  6. |> filter(fn: (r) => r._measurement == "example-measurement")
  7. |> filter(fn: (r) => r._field == "example-field")
  8. right =
  9. sql.from(
  10. driverName: "postgres",
  11. dataSourceName: "postgresql://username:password@localhost:5432",
  12. query: "SELECT * FROM example_table",
  13. )
  14. join.full(
  15. left: left,
  16. right: right,
  17. on: (l, r) => l.id== r.id,
  18. as: (l, r) => {
  19. id = if exists l.id then l.id else r.id
  20. return {name: l.name, location: r.location, id: id}
  21. },
  22. )

For more information and detailed examples, see Perform a full outer join in the Flux documentation.

  1. Import the join package.

  2. Define the left and right data streams to join:

    • Each stream must also have a _time column.
    • Each stream must have one or more columns with common values. Column labels do not need to match, but column values do.
    • Each stream should have identical group keys.

    For more information, see join data requirements.

  3. Use join.time() to join the two streams together based on time values. Provide the following parameters:

    • left: (Required) Stream of data representing the left side of the join.
    • right: (Required) Stream of data representing the right side of the join.
    • as: (Required) Join output function that returns a record with values from each input stream. For example: (l, r) => ({r with column1: l.column1, column2: l.column2}).
    • method: Join method to use. Default is inner.
  1. import "join"
  2. import "sql"
  3. left =
  4. from(bucket: "example-bucket-1")
  5. |> range(start: "-1h")
  6. |> filter(fn: (r) => r._measurement == "example-m1")
  7. |> filter(fn: (r) => r._field == "example-f1")
  8. right =
  9. from(bucket: "example-bucket-2")
  10. |> range(start: "-1h")
  11. |> filter(fn: (r) => r._measurement == "example-m2")
  12. |> filter(fn: (r) => r._field == "example-f2")
  13. join.time(method: "left", left: left, right: right, as: (l, r) => ({l with f2: r._value}))

For more information and detailed examples, see Join on time in the Flux documentation.


When to use union and pivot instead of join functions

We recommend using the join package to join streams that have mostly different schemas or that come from two separate data sources. If you’re joining two datasets queried from InfluxDB, using union() and pivot() to combine the data will likely be more performant.

For example, if you need to query fields from different InfluxDB buckets and align field values in each row based on time:

  1. f1 =
  2. from(bucket: "example-bucket-1")
  3. |> range(start: "-1h")
  4. |> filter(fn: (r) => r._field == "f1")
  5. |> drop(columns: "_measurement")
  6. f2 =
  7. from(bucket: "example-bucket-2")
  8. |> range(start: "-1h")
  9. |> filter(fn: (r) => r._field == "f2")
  10. |> drop(columns: "_measurement")
  11. union(tables: [f1, f2])
  12. |> pivot(rowKey: ["_time"], columnKey: ["_field"], valueColumn: "_value")

View example input and output data

Input

f1
_time_field_value
2020-01-01T00:01:00Zf11
2020-01-01T00:02:00Zf12
2020-01-01T00:03:00Zf11
2020-01-01T00:04:00Zf13
f2
_time_field_value
2020-01-01T00:01:00Zf25
2020-01-01T00:02:00Zf212
2020-01-01T00:03:00Zf28
2020-01-01T00:04:00Zf26

Output

_timef1f2
2020-01-01T00:01:00Z15
2020-01-01T00:02:00Z212
2020-01-01T00:03:00Z18
2020-01-01T00:04:00Z36

join flux