labels(预览版中的图形函数)

适用于:✅Azure 数据资源管理器

注释

此功能目前处于公开预览状态。 正式发布之前,功能和语法可能会更改。

图形 labels() 函数检索与图形中的节点或边缘关联的标签。 它可用于基于其标签筛选元素,以及用于在查询结果中投影标签信息。

标签在 图形模型中 定义,可以是静态的(分配给节点或边缘类型的固定标签)或动态(在图形构造期间派生自数据属性的标签)。 该 labels() 函数访问这些预定义标签,以实现对图形元素的高效筛选和分析。

注释

此函数与 图形匹配图形最短路径 运算符一起使用。

重要

labels()当函数用于使用make-graph运算符(即暂时图形而不是持久图形模型)创建的图形上时,它始终为所有节点和边缘返回一个空数组(动态数据类型),因为暂时性图形没有标签元数据。

语法

labels([element])

参数

名称 类型 必选 DESCRIPTION
元素 string 在图形模式中引用图形节点或边缘变量。 在 all()any()map() 图形函数中使用时,不要传递任何参数,inner_nodes()。 有关详细信息,请参阅 图形模式表示法

退货

返回一个动态数组,其中包含与指定节点或边缘关联的标签。 对于没有标签的节点和边缘,返回一个空数组。

all()any()map() 中使用时,使用 inner_nodes()时, labels() 调用不带参数分别返回所有内部节点或边缘的标签。

标记源

标签在 图形模型中 定义,可以源自两个源:

  • 静态标签:修复了在图形模型定义期间分配给特定节点或边缘类型的标签。 对于特定类型的所有实例,这些标签保持不变。
  • 动态标签:图形构造期间从数据属性派生的标签。 这些标签可能因实际数据值和计算表达式而异。

labels() 函数通过图形模型架构和定义检索与图形元素关联的静态标签和动态标签。

例子

按标签筛选节点

以下示例演示如何使用 labels() 函数根据分配的标签筛选节点。 该示例包括完整的图形模型定义,以阐明如何分配静态和动态标签。

图形模型定义

.create-or-alter graph_model AppProcessGraph ```
{
  "Schema": {
    "Nodes": {
      "Application": {"AppName": "string", "Type": "string"},
      "Process": {"ProcessName": "string"}
    },
    "Edges": {
      "CONNECTS_TO": {}
    }
  },
  "Definition": {
    "Steps": [
      {
        "Kind": "AddNodes",
        "Query": "Applications | project AppId, AppName, Type, NodeLabels",
        "NodeIdColumn": "AppId",
        "Labels": ["Application"],
        "LabelsColumn": "NodeLabels"
      },
      {
        "Kind": "AddNodes",
        "Query": "Processes | project ProcId, ProcessName",
        "NodeIdColumn": "ProcId",
        "Labels": ["Process"]
      },
      {
        "Kind": "AddEdges",
        "Query": "AppConnections | project SourceAppId, TargetProcId",
        "SourceColumn": "SourceAppId",
        "TargetColumn": "TargetProcId",
        "Labels": ["CONNECTS_TO"]
      }
    ]
  }
}
```

查询示例

graph('AppProcessGraph')
| graph-match cycles=none (app)-[e*1..3]->(process)
    where process.ProcessName contains "nginx" and labels(app) has "Application"
    project app=app.AppName
| summarize c=count() by app
| top 10 by c desc

输出

应用程序 c
WebApp1 15
WebApp2 12
APIService 8

结果中的项目标签

以下示例演示如何使用 labels() project 子句中的函数在查询结果中包含标签信息。 此查询查找不同类型的网络组件之间的连接,并包括用于分析的标签。

图形模型定义

.create-or-alter graph_model NetworkGraph ```
{
  "Schema": {
    "Nodes": {
      "NetworkComponent": {"ComponentName": "string", "ComponentType": "string"}
    },
    "Edges": {
      "CONNECTED_TO": {"ConnectionType": "string"}
    }
  },
  "Definition": {
    "Steps": [
      {
        "Kind": "AddNodes",
        "Query": "NetworkComponentsTable | project Id, ComponentName, ComponentType, NodeLabels",
        "NodeIdColumn": "Id",
        "Labels": ["NetworkComponent"],
        "LabelsColumn": "NodeLabels"
      },
      {
        "Kind": "AddEdges",
        "Query": "ConnectionsTable | project SourceId, TargetId, ConnectionType, EdgeLabels",
        "SourceColumn": "SourceId",
        "TargetColumn": "TargetId",
        "Labels": ["CONNECTED_TO"],
        "LabelsColumn": "EdgeLabels"
      }
    ]
  }
}
```

查询示例

graph('NetworkGraph')
| graph-match (source)-[conn]->(target)
    where labels(source) has "Network" and labels(target) has "Compute"
    project 
        SourceComponent = source.ComponentName,
        TargetComponent = target.ComponentName,
        SourceLabels = labels(source),
        TargetLabels = labels(target),
        ConnectionType = conn.ConnectionType

输出

SourceComponent TargetComponent SourceLabels TargetLabels 连接类型
Switch1 Server1 [“Network”, “Access”] [“Compute”, “Production”] 以太网

按多个标签条件进行筛选

以下示例演示如何合并多个标签条件,以查找网络拓扑中的复杂模式。 此查询通过中间件层标识从前端组件到后端组件的路径。

图形模型定义

.create-or-alter graph_model AppComponentGraph ```
{
  "Schema": {
    "Nodes": {
      "Frontend": {"ComponentName": "string"},
      "Middleware": {"ComponentName": "string"},
      "Backend": {"ComponentName": "string"}
    },
    "Edges": {
      "DEPENDS_ON": {"DependencyType": "string"}
    }
  },
  "Definition": {
    "Steps": [
      {
        "Kind": "AddNodes",
        "Query": "ComponentsTable | project Id, ComponentName, NodeLabels",
        "NodeIdColumn": "Id",
        "LabelsColumn": "NodeLabels"
      },
      {
        "Kind": "AddEdges",
        "Query": "DependenciesTable | project SourceId, TargetId, DependencyType, EdgeLabels",
        "SourceColumn": "SourceId",
        "TargetColumn": "TargetId",
        "Labels": ["DEPENDS_ON"],
        "LabelsColumn": "EdgeLabels"
      }
    ]
  }
}
```

查询示例

graph('AppComponentGraph')
| graph-match (frontend)-[dep1]->(middleware)-[dep2]->(backend)
    where labels(frontend) has "Frontend" 
          and labels(middleware) has "Middleware" 
          and labels(backend) has "Backend"
    project 
        Path = strcat(frontend.ComponentName, " -> ", middleware.ComponentName, " -> ", backend.ComponentName),
        FrontendLabels = labels(frontend),
        MiddlewareLabels = labels(middleware),
        BackendLabels = labels(backend)

输出

路径 FrontendLabels MiddlewareLabels BackendLabels
WebUI -> APIGateway -> 数据库 [“Frontend”, “UserInterface”] [“中间件”, “API”] [“Backend”, “Storage”]
WebUI -> APIGateway -> 缓存 [“Frontend”, “UserInterface”] [“中间件”, “API”] [“Backend”, “Cache”]

将 labels() 与 all() 和 any() 函数一起使用

以下示例演示如何在 labels() 函数中使用不带参数的 all() 函数以及 any() 具有可变长度路径的函数。 此查询在服务网格中查找路径,其中所有中间服务都有“生产”标签,并且至少有一个中间服务具有“关键”标签。

图形模型定义

.create-or-alter graph_model ServiceMeshGraph ```
{
  "Schema": {
    "Nodes": {
      "Service": {"ServiceName": "string", "Environment": "string"}
    },
    "Edges": {
      "CALLS": {"Protocol": "string"}
    }
  },
  "Definition": {
    "Steps": [
      {
        "Kind": "AddNodes",
        "Query": "ServicesTable | project Id, ServiceName, Environment, NodeLabels",
        "NodeIdColumn": "Id",
        "Labels": ["Service"],
        "LabelsColumn": "NodeLabels"
      },
      {
        "Kind": "AddEdges",
        "Query": "ServiceCallsTable | project SourceId, TargetId, Protocol, EdgeLabels",
        "SourceColumn": "SourceId",
        "TargetColumn": "TargetId",
        "Labels": ["CALLS"],
        "LabelsColumn": "EdgeLabels"
      }
    ]
  }
}
```

查询示例

graph('ServiceMeshGraph')
| graph-match (source)-[calls*2..4]->(destination)
    where source.ServiceName == "UserService" and
          destination.ServiceName == "DatabaseService" and
          all(inner_nodes(calls), labels() has "Production") and
          any(inner_nodes(calls), labels() has "Critical")
    project 
        Path = strcat_array(map(inner_nodes(calls), ServiceName), " -> "),
        IntermediateLabels = map(inner_nodes(calls), labels()),
        CallProtocols = map(calls, Protocol)

输出

路径 IntermediateLabels CallProtocols
AuthService -> PaymentService [[“Production”, “Auth”], [“Production”, “Critical”, “Payment”]] [“HTTPS”, “gRPC”]
CacheService -> PaymentService [[“Production”, “Cache”], [“Production”, “Critical”, “Payment”]] [“Redis”, “gRPC”]