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

sample operator

Applies to: ✅Microsoft FabricAzure Data ExplorerAzure MonitorMicrosoft Sentinel

从输入表返回最大指定随机行数。

Note

  • sample 适用于速度而不是对值的均匀分配。 具体来说,这意味着如果在联合了 2 个不同大小的数据集的运算符(如 unionjoin 运算符)之后使用,它将不会产生“公平”的结果。 建议在表格引用和筛选器后立即使用 sample
  • sample 是一个不确定运算符,每次在查询期间计算结果集时返回不同的结果集。 例如,以下查询将产生两个不同的行(即使其中一个预期会返回相同的行两次)。

Syntax

T| sampleNumberOfRows

Learn more about syntax conventions.

Parameters

Name 类型 Required Description
T string ✔️ 输入表格表达式。
NumberOfRows int、long 或 real ✔️ 要返回的行数。 可以指定任何数值表达式。

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. 可能需要修改示例查询中的表名称以匹配工作区中的表。

生成示例

此查询创建一系列数字、采样一个值,然后复制该样本。

let _data = range x from 1 to 100 step 1;
let _sample = _data | sample 1;
union (_sample), (_sample)

Output

x
74
63

To ensure that in example above _sample is calculated once, one can use materialize() function:

let _data = range x from 1 to 100 step 1;
let _sample = materialize(_data | sample 1);
union (_sample), (_sample)

Output

x
24
24

生成特定百分比数据的示例

若要对一定百分比的数据(而不是指定的行数)进行采样,则可以使用

StormEvents | where rand() < 0.1

Output

该表包含输出的前几行。 运行查询以查看完整结果。

StartTime EndTime EpisodeId EventId State EventType
2007-01-01T00:00:00Z 2007-01-20T10:24:00Z 2403 11914 INDIANA Flood
2007-01-01T00:00:00Z 2007-01-24T18:47:00Z 2408 11930 INDIANA Flood
2007-01-01T00:00:00Z 2007-01-01T12:00:00Z 1979 12631 DELAWARE Heavy Rain
2007-01-01T00:00:00Z 2007-01-01T00:00:00Z 2592 13208 NORTH CAROLINA Thunderstorm Wind
2007-01-01T00:00:00Z 2007-01-31T23:59:00Z 1492 7069 MINNESOTA Drought
2007-01-01T00:00:00Z 2007-01-31T23:59:00Z 2240 10858 TEXAS Drought
... ... ... ... ... ...

生成密钥示例

若要对键(例如-示例 10 ID 和获取这些 ID 的所有行)进行采样,可以将 sample-distinctin 运算符结合使用。

let sampleEpisodes = StormEvents | sample-distinct 10 of EpisodeId;
StormEvents
| where EpisodeId in (sampleEpisodes)

Output

该表包含输出的前几行。 运行查询以查看完整结果。

StartTime EndTime EpisodeId EventId State EventType
2007-09-18T20:00:00Z 2007-09-19T18:00:00Z 11074 60904 FLORIDA Heavy Rain
2007-09-20T21:57:00Z 2007-09-20T22:05:00Z 11078 60913 FLORIDA Tornado
2007-09-29T08:11:00Z 2007-09-29T08:11:00Z 11091 61032 ATLANTIC SOUTH Waterspout
2007-12-07T14:00:00Z 2007-12-08T04:00:00Z 13183 73241 AMERICAN SAMOA Flash Flood
2007-12-11T21:45:00Z 2007-12-12T16:45:00Z 12826 70787 KANSAS Flood
2007-12-13T09:02:00Z 2007-12-13T10:30:00Z 11780 64725 KENTUCKY Flood
... ... ... ... ... ...