你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

mv-expand operator

Applies to: ✅Microsoft FabricAzure Data ExplorerAzure MonitorMicrosoft Sentinel

将多值动态数组或属性包扩展为多个记录。

mv-expand can be described as the opposite of the aggregation operators that pack multiple values into a single dynamic-typed array or property bag, such as summarize ... make-list() and make-series. (标量)数组或属性包中的每个元素都在运算符的输出中生成一个新记录。 输入中未扩展的所有列都将复制到输出中的所有记录中。

Syntax

T|mv-expand [kind=(bag | array)] [with_itemindex=IndexColumnName] ColumnName [to typeof(Typename)] [,ColumnName ...] [limitRowlimit]

T|mv-expand [kind=(bag | array)] [Name=] ArrayExpression [to typeof(Typename)] [, [Name=] ArrayExpression [to typeof(Typename)] ...] [limitRowlimit]

Learn more about syntax conventions.

Parameters

Name 类型 Required Description
ColumnName, ArrayExpression string ✔️ 一个列引用或标量表达式,含 dynamic 类型的值,带有数组或属性包。 数组或属性包的各个顶级元素扩展为多个记录。
When ArrayExpression is used and Name doesn't equal any input column name, the expanded value is extended into a new column in the output. Otherwise, the existing ColumnName is replaced.
Name string 新列的名称。
Typename string ✔️ 指示数组元素的基础类型,该类型将成为 mv-expand 运算符生成的列的类型。 应用类型的操作仅限强制转换,不包括分析或类型转换。 不符合声明类型的数组元素将成为 null 值。
RowLimit int 从每个原始行生成的最大行数。 默认值为 2147483647。 mvexpandmv-expand 运算符的已过时的旧形式。 旧版本的默认行限制为 128 行。
IndexColumnName string If with_itemindex is specified, the output includes another column named IndexColumnName that contains the index starting at 0 of the item in the original expanded collection.

Returns

对于输入中的每个记录,运算符会在输出中返回零个、一个或多个记录,具体通过以下方式确定:

  1. 未扩展的输入列在输出中显示其原始值。 如果单个输入记录扩展为多个输出记录,会将其值复制到所有记录中。

  2. For each ColumnName or ArrayExpression that is expanded, the number of output records is determined for each value as explained in modes of expansion. 对于每个输入记录,将计算输出记录的最大数量。 将“并行”扩展所有数组或属性包,以便将缺少的值(如果有)替换为 null 值。 元素已按其在原始数组/包中显示的顺序扩展到行中。

  3. 如果动态值为 null,则为该值 (null) 生成单个记录。 如果动态值为空数组或属性包,则不会为该值生成任何记录。 否则,将生成多个记录,因为动态值中有元素。

扩展的列的类型为 dynamic,除非使用 to typeof() 子句显式指定其类型。

扩展模式

支持两种模式的属性包扩展:

  • kind=bagbagexpansion=bag:将属性包扩展为单个条目属性包。 此模式是默认模式。
  • kind=array or bagexpansion=array: Property bags are expanded into two-element [key,value] array structures, allowing uniform access to keys and values. 使用此模式还可以(例如)对属性名称运行非重复计数聚合。

Examples

The examples in this article use publicly available tables in the help cluster, such as the StormEvents table in the Samples database.

The examples in this article use publicly available tables, such as the Weather table in the Weather analytics sample gallery. 可能需要修改示例查询中的表名称以匹配工作区中的表。

本节中的示例演示如何使用语法帮助你入门。

单列 - 数组扩展

datatable (a: int, b: dynamic)
[
    1, dynamic([10, 20]),
    2, dynamic(['a', 'b'])
]
| mv-expand b

Output

a b
1 10
1 20
2 a
2 b

单列 - 包扩展

单个列的简单展开:

datatable (a: int, b: dynamic)
[
    1, dynamic({"prop1": "a1", "prop2": "b1"}),
    2, dynamic({"prop1": "a2", "prop2": "b2"})
]
| mv-expand b

Output

a b
1 {"prop1": "a1"}
1 {"prop2": "b1"}
2 {"prop1": "a2"}
2 {"prop2": "b2"}

单列 - 包扩展为键值对

简单包扩展为键值对:

datatable (a: int, b: dynamic)
[
    1, dynamic({"prop1": "a1", "prop2": "b1"}),
    2, dynamic({"prop1": "a2", "prop2": "b2"})
]
| mv-expand kind=array b 
| extend key = b[0], val=b[1]

Output

a b 关键值 val
1 ["prop1","a1"] prop1 a1
1 ["prop2","b1"] prop2 b1
2 ["prop1","a2"] prop1 a2
2 ["prop2","b2"] prop2 b2

压缩的两个列

首先展开两列以“zip”适用的列,然后展开它们:

datatable (a: int, b: dynamic, c: dynamic)[
    1, dynamic({"prop1": "a", "prop2": "b"}), dynamic([5, 4, 3])
]
| mv-expand b, c

Output

a b c
1 {"prop1":"a"} 5
1 {"prop2":"b"} 4
1 3

两个列的笛卡尔乘积

如果要在展开两列时获取笛卡尔乘积,请将其逐个展开:

datatable (a: int, b: dynamic, c: dynamic)
[
    1, dynamic({"prop1": "a", "prop2": "b"}), dynamic([5, 6])
]
| mv-expand b
| mv-expand c

Output

a b c
1 { “prop1”: “a”} 5
1 { “prop1”: “a”} 6
1 { “prop2”: “b”} 5
1 { “prop2”: “b”} 6

Convert output

若要将 mv-expand 的输出强制为某个特定类型(默认为动态),请使用 to typeof

datatable (a: string, b: dynamic, c: dynamic)[
    "Constant", dynamic([1, 2, 3, 4]), dynamic([6, 7, 8, 9])
]
| mv-expand b, c to typeof(int)
| getschema 

Output

ColumnName ColumnOrdinal DateType ColumnType
a 0 System.String string
b 1 System.Object dynamic
c 2 System.Int32 int

请注意 b 列以 dynamic 的形式返回,而 cint 的形式返回。

Using with_itemindex

通过 with_itemindex 展开数组:

range x from 1 to 4 step 1
| summarize x = make_list(x)
| mv-expand with_itemindex=Index x

Output

x Index
1 0
2 1
3 2
4 3