table()(范围函数)table() (scope function)

table() 函数通过以 string 类型表达式的形式提供表的名称来引用表。The table() function references a table by providing its name as an expression of type string.

table('StormEvent')

语法Syntax

table ( TableName [, DataScope] )table ( TableName [, DataScope] )

自变量Arguments

  • TableName:string 类型的表达式,提供要引用的表的名称。TableName: An expression of type string that provides the name of the table being referenced. 在调用该函数时,此表达式的值必须是常数(即,它不会随数据上下文变化)。The value of this expression must be constant at the point of call to the function (i.e. it cannot vary by the data context).

  • DataScopestring 类型的可选参数,可用于根据数据按照表的有效 缓存策略归类的方式来限制表对此数据的引用。DataScope: An optional parameter of type string that can be used to restrict the table reference to data according to how this data falls under the table's effective cache policy. 如果使用该参数,则实际参数必须是具有以下可能值之一的常数 string 表达式:If used, the actual argument must be a constant string expression having one of the following possible values:

    • "hotcache":只有划分为热缓存类型的数据才会被引用。"hotcache": Only data that is categorized as hot cache will be referenced.
    • "all":该表中的所有数据都会被引用。"all": All the data in the table will be referenced.
    • "default":与 "all" 相同,群集管理员已将群集设置为使用 "hotcache" 作为默认值的情况除外。"default": This is the same as "all", except if the cluster has been set to use "hotcache" as the default by the cluster admin.
  • TableName:string 类型的表达式,提供要引用的表的名称。TableName: An expression of type string that provides the name of the table being referenced. 在调用该函数时,此表达式的值必须是常数(即,它不会随数据上下文变化)。The value of this expression must be constant at the point of call to the function (i.e. it cannot vary by the data context).

  • DataScopestring 类型的可选参数,可用于根据数据按照表的有效缓存策略归类的方式限制表对此数据的引用。DataScope: An optional parameter of type string that can be used to restrict the table reference to data according to how this data falls under the table's effective cache policy. 如果使用该参数,则实际参数必须是具有以下可能值之一的常数 string 表达式:If used, the actual argument must be a constant string expression having one of the following possible values:

    • "hotcache":只有划分为热缓存类型的数据才会被引用。"hotcache": Only data that is categorized as hot cache will be referenced.
    • "all":该表中的所有数据都会被引用。"all": All the data in the table will be referenced.
    • "default":这与 "all" 相同。"default": This is the same as "all".

示例Examples

使用 table() 访问当前数据库的表Use table() to access table of the current database

table('StormEvent') | count
计数Count
5906659066

在 let 语句中使用 table()Use table() inside let statements

可以重写与上述示例相同的查询,以使用接收参数 tableName 的内联函数(let 语句),该参数会传递到 table() 函数中。The same query as above can be rewritten to use inline function (let statement) that receives a parameter tableName - which is passed into the table() function.

let foo = (tableName:string)
{
    table(tableName) | count
};
foo('help')
计数Count
5906659066

在函数中使用 table()Use table() inside Functions

可以重写与上述示例相同的查询,以便在一个接收参数 tableName 的函数中使用,该参数会传递到 table() 函数中。The same query as above can be rewritten to be used in a function that receives a parameter tableName - which is passed into the table() function.

.create function foo(tableName:string)
{
    table(tableName) | count
};

请注意: 此类函数只能在本地使用,而不能在跨群集查询中使用。Note: such functions can be used only locally and not in the cross-cluster query.

将 table() 与非常数参数配合使用Use table() with non-constant parameter

不是标量常数字符串的参数不能作为参数传递到 table() 函数。A parameter, which is not scalar constant string can't be passed as parameter to table() function.

下面提供这种情况下的解决方法示例。Below, given an example of workaround for such case.

let T1 = print x=1;
let T2 = print x=2;
let _choose = (_selector:string)
{
    union
    (T1 | where _selector == 'T1'),
    (T2 | where _selector == 'T2')
};
_choose('T2')

xx
22