跳转至

Reduce array elements(减少数组元素(Reduce array elements))

Supported in: Batch, Streaming

Reduces array elements using an expression.

Expression categories: Array

Declared arguments

  • Array: Array to reduce.
    Expression\ | Short | String | Timestamp> | Boolean | Byte | Date | Double | Float | Integer | Long | Map\\ | Short | String | Timestamp>>
  • Expression to reduce: The expression to apply once per element of the array.
    Expression\
  • Initial value: This is the start value used to initialize the accumulator, if the array has a length of 0, this value will be returned.
    Expression\

Type variable bounds: T accepts Array\ | Short | String | Timestamp> | Boolean | Byte | Date | Double | Float | Integer | Long | Map\ | Short | String | Timestamp

Output type: T

Examples

Example 1: Base case

Argument values:

  • Array: miles
  • Expression to reduce:
    add(
     expressions: [accumulator, element],
    )
  • Initial value: 0
miles Output
[ 12300, 12342 ] 24642

Example 2: Base case

Description: Return the first non null within the array.

Argument values:

  • Array: miles
  • Expression to reduce:
    firstNonNull(
     expressions: [accumulator, element],
    )
  • Initial value: init
miles init Output
[ null, null, 12300, 12111 ] null 12300

Example 3: Base case

Argument values:

  • Array: miles
  • Expression to reduce:
    concatStrings(
     expressions: [accumulator,
    cast(
     expression: element,
     type: String,
    )],
     separator: -,
    )
  • Initial value: empty string
miles Output
[ 12300, 12342 ] -12300-12342

Example 4: Null case

Description: Null arrays will return null outputs.

Argument values:

  • Array: miles
  • Expression to reduce:
    firstNonNull(
     expressions: [accumulator, element],
    )
  • Initial value: init
miles init Output
null null null

Example 5: Edge case

Description: Empty array will return the initial value.

Argument values:

  • Array: miles
  • Expression to reduce:
    add(
     expressions: [accumulator, element],
    )
  • Initial value: 0
miles Output
[ ] 0


中文翻译

减少数组元素(Reduce array elements)

支持:批处理(Batch)、流处理(Streaming)

使用表达式减少数组元素。

表达式类别: 数组(Array)

声明的参数

  • 数组(Array): 要减少的数组。
    表达式\<数组\<数组\<布尔型(Boolean) | 字节型(Byte) | 日期型(Date) | 双精度浮点型(Double) | 浮点型(Float) | 整型(Integer) | 长整型(Long) | 映射\<任意类型(AnyType), 任意类型(AnyType)> | 短整型(Short) | 字符串(String) | 时间戳(Timestamp)> | 布尔型(Boolean) | 字节型(Byte) | 日期型(Date) | 双精度浮点型(Double) | 浮点型(Float) | 整型(Integer) | 长整型(Long) | 映射\<任意类型(AnyType), 任意类型(AnyType)> | 短整型(Short) | 字符串(String) | 时间戳(Timestamp)>>
  • 减少表达式(Expression to reduce): 对数组每个元素应用一次的表达式。
    表达式\
  • 初始值(Initial value): 用于初始化累加器的起始值,如果数组长度为0,则返回此值。
    表达式\

类型变量边界: T 接受 数组\<布尔型(Boolean) | 字节型(Byte) | 日期型(Date) | 双精度浮点型(Double) | 浮点型(Float) | 整型(Integer) | 长整型(Long) | 映射\<任意类型(AnyType), 任意类型(AnyType)> | 短整型(Short) | 字符串(String) | 时间戳(Timestamp)> | 布尔型(Boolean) | 字节型(Byte) | 日期型(Date) | 双精度浮点型(Double) | 浮点型(Float) | 整型(Integer) | 长整型(Long) | 映射\<任意类型(AnyType), 任意类型(AnyType)> | 短整型(Short) | 字符串(String) | 时间戳(Timestamp)

输出类型: T

示例

示例1:基本情况

参数值:

  • 数组(Array): miles
  • 减少表达式(Expression to reduce):
    add(
     expressions: [accumulator, element],
    )
  • 初始值(Initial value): 0
miles 输出(Output)
[ 12300, 12342 ] 24642

示例2:基本情况

描述: 返回数组中的第一个非空值。

参数值:

  • 数组(Array): miles
  • 减少表达式(Expression to reduce):
    firstNonNull(
     expressions: [accumulator, element],
    )
  • 初始值(Initial value): init
miles init 输出(Output)
[ null, null, 12300, 12111 ] null 12300

示例3:基本情况

参数值:

  • 数组(Array): miles
  • 减少表达式(Expression to reduce):
    concatStrings(
     expressions: [accumulator,
    cast(
     expression: element,
     type: String,
    )],
     separator: -,
    )
  • 初始值(Initial value): 空字符串
miles 输出(Output)
[ 12300, 12342 ] -12300-12342

示例4:空值情况

描述: 空数组将返回空输出。

参数值:

  • 数组(Array): miles
  • 减少表达式(Expression to reduce):
    firstNonNull(
     expressions: [accumulator, element],
    )
  • 初始值(Initial value): init
miles init 输出(Output)
null null null

示例5:边界情况

描述: 空数组将返回初始值。

参数值:

  • 数组(Array): miles
  • 减少表达式(Expression to reduce):
    add(
     expressions: [accumulator, element],
    )
  • 初始值(Initial value): 0
miles 输出(Output)
[ ] 0