跨数据库和跨群集查询Cross-database and cross-cluster queries

每个 Kusto 查询都在当前群集的上下文中以及在默认数据库中运行。Every Kusto query operates in the context of the current cluster, and the default database.

查询Queries

若要访问除默认值以外的任何其他数据库中的表,必须使用“限定的名称”语法。To access tables from any database other than the default, the qualified name syntax must be used.

访问当前群集中的数据库。To access database in the current cluster.

database("<database name>").<table name>

远程群集中的数据库。Database in remote cluster.

cluster("<cluster name>").database("<database name>").<table name>

“数据库名称”区分大小写database name is case-sensitive

“集群名称”不区分大小写且可以是以下形式之一:cluster name is case-insensitive and can be of one of the following forms:

  • 格式标准的 URL,例如 http://contoso.kusto.chinacloudapi.cn:1234/Well-formed URL, such as http://contoso.kusto.chinacloudapi.cn:1234/. 仅支持 HTTP 和 HTTPS 方案。Only HTTP and HTTPS schemes are supported.
  • 完全限定的域名 (FQDN),例如 contoso.kusto.chinacloudapi.cnFully qualified domain name (FQDN), such as contoso.kusto.chinacloudapi.cn. 该字符串等效于 https://contoso.kusto.chinacloudapi.cn :443/This string is equivalent to https://contoso.kusto.chinacloudapi.cn:443/.
  • 短名称(不包含域部分的主机名 [和区域]),例如 contosocontoso.chinaeast2Short name (host name [and region] without the domain part), such as contoso or contoso.chinaeast2. 这些字符串转译为 https://contoso .kusto.chinacloudapi.cn:443/https://contoso.chinaeast2 .kusto.chinacloudapi.cn:443/These strings are interpreted as https://contoso.kusto.chinacloudapi.cn:443/ and https://contoso.chinaeast2.kusto.chinacloudapi.cn:443/.

备注

跨数据库访问需遵守常规权限检查。Cross-database access is subject to the usual permission checks. 若要执行查询,必须对默认数据库和查询中引用的每个其他数据库(在当前和远程群集中)具有读取权限。To execute a query, you must have read permission to the default database and to every other database referenced in the query (in the current and remote clusters).

“限定的名称”可以在任何可使用表名的上下文中使用。Qualified name can be used in any context in which a table name can be used.

以下均为有效选项。All of the following are valid.

database("OtherDb").Table | where ...

union Table1, cluster("OtherCluster").database("OtherDb").Table2 | project ...

database("OtherDb1").Table1 | join cluster("OtherCluster").database("OtherDb2").Table2 on Key | join Table3 on Key | extend ...

“限定的名称”显示为 联合运算符 的操作数时,可以使用通配符来指定多个表和多个数据库。When qualified name appears as an operand of the union operator, then wildcards can be used to specify multiple tables and multiple databases. 群集名称中不允许使用通配符。Wildcards aren't permitted in cluster names.

union withsource=TableName *, database("OtherDb*").*Table, cluster("OtherCluster").database("*").*

备注

  • 默认数据库的名称也是潜在匹配项,因此数据库 ("*") 指定所有数据库(包括默认数据库)的所有表。The name of the default database is also a potential match, so database("*")specifies all tables of all databases including the default.
  • 要详细了解架构更改如何影响跨群集查询,请参阅跨群集查询和架构更改For more ionformation on how schema changes affect cross-cluster queries, see Cross-cluster queries and schema changes

访问限制Access restriction

限定的名称或模式也可以包含在限制访问语句中,不允许在群集名称中使用通配符。Qualified names or patterns can also be included in restrict access statement, Wildcards in cluster names aren't permitted.

restrict access to (my*, database("MyOther*").*, cluster("OtherCluster").database("my2*").*);

以上将限制对以下实体的查询访问:The above will restrict the query access to the following entities:

  • 从默认数据库中的“my...”开始的任何实体名称。Any entity name starting with my... in the default database.
  • 当前群集的所有名为“MyOther...”的数据库中的任何表。Any table in all the databases named MyOther... of the current cluster.
  • “OtherCluster.kusto.chinacloudapi.cn”集群中所有名为“my2...”的数据库中的任何表 。Any table in all the databases named my2... in the cluster OtherCluster.kusto.chinacloudapi.cn.

函数和视图Functions and Views

函数和视图(永久的和内联创建的)可以跨数据库和群集边界引用表。Functions and views (persistent and created inline) can reference tables across database and cluster boundaries. 以下代码是有效的。The following code is valid.

let MyView = Table1 join database("OtherDb").Table2 on Key | join cluster("OtherCluster").database("SomeDb").Table3 on Key;
MyView | where ...

可以从同一集群中的另一个数据库访问永久函数和视图。Persistent functions and views can be accessed from another database in the same cluster.

OtherDb 中的表格函数(视图)。Tabular function (view) in OtherDb.

.create function MyView(v:string) { Table1 | where Column1 has v ...  }  

OtherDb 中的标量函数。Scalar function in OtherDb.

.create function MyCalc(a:double, b:double, c:double) { (a + b) / c }  

在默认数据库中。In default database.

database("OtherDb").MyView("exception") | extend CalCol=database("OtherDb").MyCalc(Col1, Col2, Col3) | limit 10

跨群集函数调用的限制Limitations of cross-cluster function calls

可以跨群集引用的表格函数或视图。Tabular functions or views can be referenced across clusters. 以下限制适用:The following limitations apply:

  • 远程函数必须返回表格架构。Remote function must return tabular schema. 标量函数仅能在同一个集群中访问。Scalar functions can only be accessed in the same cluster.
  • 远程函数仅能接受标量参数。Remote function can accept only scalar parameters. 获取一个或多个表参数的函数只能在同一群集中访问。Functions that get one or more table arguments can only be accessed in the same cluster.
  • 远程函数的架构必须是已知的,并且参数不变。The schema of the remote function must be known and invariant of its parameters. 有关详细信息,请参阅跨群集查询和架构更改For more information, see Cross-cluster queries and schema changes.

以下跨群集调用有效。The following cross-cluster call is valid.

cluster("OtherCluster").database("SomeDb").MyView("exception") | count

以下查询调用远程标量函数 MyCalcThe following query calls a remote scalar function MyCalc. 此调用违反规则 #1,因此无效。This call violates rule #1, so it's not valid.

MyTable | extend CalCol=cluster("OtherCluster").database("OtherDb").MyCalc(Col1, Col2, Col3) | limit 10

以下查询调用远程函数 MyCalc 并提供表格参数。The following query calls remote function MyCalc and provides a tabular parameter. 此调用违反规则 #2,因此无效。This call violates rule #2, so it's not valid.

cluster("OtherCluster").database("OtherDb").MyCalc(datatable(x:string, y:string)["x","y"] )

以下查询调用远程函数 SomeTable,该函数具有基于参数 tablename 的变量架构输出。The following query calls remote function SomeTable that has a variable schema output based on the parameter tablename. 此调用违反规则 #3,因此无效。This call violates rule #3, so it's not valid.

OtherDb 中的表格函数。Tabular function in OtherDb.

.create function SomeTable(tablename:string) { table(tablename)  }  

在默认数据库中。In default database.

cluster("OtherCluster").database("OtherDb").SomeTable("MyTable")

以下查询调用远程函数 GetDataPivot,该函数具有基于数据(pivot() plugin 具有动态输出)的可变架构输出。The following query calls remote function GetDataPivot that has a variable schema output based on the data (pivot() plugin has dynamic output). 此调用违反规则 #3,因此无效。This call violates rule #3, so it's not valid.

OtherDb 中的表格函数。Tabular function in OtherDb.

.create function GetDataPivot() { T | evaluate pivot(PivotColumn) }  

默认数据库中的表格函数。Tabular function in the default database.

cluster("OtherCluster").database("OtherDb").GetDataPivot()

显示数据Displaying data

返回的记录数将隐式限制将数据返回到客户端的语句,即使没有特定使用 take 运算符。Statements that return data to the client are implicitly limited by the number of records returned, even if there's no specific use of the take operator. 若要提升此限制,请使用 notruncation 客户端请求选项。To lift this limit, use the notruncation client request option.

要以图形形式显示数据,请使用 呈现运算符To display data in graphical form, use the render operator.

Azure Monitor 中不支持跨数据库查询和跨群集查询。Cross-database and cross-cluster queries aren't supported in Azure Monitor.