适用于:✅Azure 数据资源管理器
ipv4_lookup 插件会在查找表中查找 IPv4 值,并返回具有匹配值的行。 该插件通过 evaluate 运算符调用。
语法
T | evaluate ipv4_lookup( LookupTable , SourceIPv4Key , IPv4LookupKey [, ExtraKey1 [.. , ExtraKeyN [, return_unmatched ]]] )
参数
| 客户 | 类型 | 必需 | 说明 | 
|---|---|---|---|
| T | string | 
✔️ | 表格输入,其列 SourceIPv4Key 将用于 IPv4 匹配。 | 
| LookupTable | string | 
✔️ | 具有 IPv4 查找数据的表或表格表达式,其列 LookupKey 将用于 IPv4 匹配。 可以使用 IP 前缀表示法对 IPv4 值进行掩码操作。 | 
| SourceIPv4Key | string | 
✔️ | T 的列,其中包含的 IPv4 字符串需在 LookupTable 中查找。 可以使用 IP 前缀表示法对 IPv4 值进行掩码操作。 | 
| IPv4LookupKey | string | 
✔️ | LookupTable 的列,其中包含的 IPv4 字符串与每个 SourceIPv4Key 值相匹配。 | 
| ExtraKey1 .. ExtraKeyN | string | 
用于查找匹配项的其他列引用。 与 join 运算类似:具有相等值的记录将被视为匹配。 列名引用必须同时存在于源表 T 和 LookupTable 中。 | 
|
| return_unmatched | bool | 
一个布尔标志,用于定义结果是应包含所有行还是仅包含匹配的行(默认值:false - 仅返回匹配的行)。 | 
IP 前缀表示法
IP 前缀表示法(也称 CIDR 表示法)是表示 IP 地址及其关联网络掩码的简明方式。 格式为 <base IP>/<prefix length>,其中,前缀长度是网络掩码中前导 1 位的数目。 前缀长度决定了属于该网络的 IP 地址范围。
对于 IPv4,前缀长度是介于 0 和 32 之间的数字。 因此,表示法 192.168.2.0/24 表示 IP 地址 192.168.2.0,且网络掩码为 255.255.255.0。 此网络掩码具有 24 个前导 1 位,或者说前缀长度为 24。
对于 IPv6,前缀长度是介于 0 和 128 之间的数字。 因此,表示法 fe80::85d:e82c:9446:7994/120 表示 IP 地址 fe80::85d:e82c:9446:7994 且网络掩码为 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00。 此网络掩码具有 120 个前导 1 位,或者说前缀长度为 120。
返回
ipv4_lookup 插件会返回基于 IPv4 密钥进行联接(查找)的结果。 该表的架构是源表与查找表的并集,类似于 lookup 运算符的结果。
如果将 return_unmatched 参数设置为 true,则生成的表包含匹配行和不匹配行(用 null 填充)。
如果将 return_unmatched 参数设置为 false 或省略此参数(使用默认值 false),则生成的表的记录数将与匹配的结果数相同。 与 return_unmatched=true 执行相比,此查找变体具有更好的性能。
注意
- 此插件涵盖基于 IPv4 的联接的方案,假设查找表较小(10-20 万行),输入表可以较大。
 - 插件的性能将取决于查找表和数据源表的大小、列数和匹配记录数。
 
示例
IPv4 查找 - 仅匹配行
// IP lookup table: IP_Data
// Partial data from: https://raw.githubusercontent.com/datasets/geoip2-ipv4/master/data/geoip2-ipv4.csv
let IP_Data = datatable(network:string, continent_code:string ,continent_name:string, country_iso_code:string, country_name:string)
[
  "111.68.128.0/17","AS","Asia","JP","Japan",
  "5.8.0.0/19","EU","Europe","RU","Russia",
  "223.255.254.0/24","AS","Asia","SG","Singapore",
  "46.36.200.51/32","OC","Oceania","CK","Cook Islands",
  "2.20.183.0/24","EU","Europe","GB","United Kingdom",
];
let IPs = datatable(ip:string)
[
  '2.20.183.12',   // United Kingdom
  '5.8.1.2',       // Russia
  '192.165.12.17', // Unknown
];
IPs
| evaluate ipv4_lookup(IP_Data, ip, network)
输出
| ip | network | continent_code | continent_name | country_iso_code | country_name | 
|---|---|---|---|---|---|
| 2.20.183.12 | 2.20.183.0/24 | EU | 欧洲 | GB | United Kingdom | 
| 5.8.1.2 | 5.8.0.0/19 | EU | 欧洲 | RU | 俄罗斯 | 
IPv4 查找 - 同时返回匹配行和非匹配行
// IP lookup table: IP_Data
// Partial data from: 
// https://raw.githubusercontent.com/datasets/geoip2-ipv4/master/data/geoip2-ipv4.csv
let IP_Data = datatable(network:string,continent_code:string ,continent_name:string ,country_iso_code:string ,country_name:string )
[
    "111.68.128.0/17","AS","Asia","JP","Japan",
    "5.8.0.0/19","EU","Europe","RU","Russia",
    "223.255.254.0/24","AS","Asia","SG","Singapore",
    "46.36.200.51/32","OC","Oceania","CK","Cook Islands",
    "2.20.183.0/24","EU","Europe","GB","United Kingdom",
];
let IPs = datatable(ip:string)
[
    '2.20.183.12',   // United Kingdom
    '5.8.1.2',       // Russia
    '192.165.12.17', // Unknown
];
IPs
| evaluate ipv4_lookup(IP_Data, ip, network, return_unmatched = true)
输出
| ip | network | continent_code | continent_name | country_iso_code | country_name | 
|---|---|---|---|---|---|
| 2.20.183.12 | 2.20.183.0/24 | EU | 欧洲 | GB | United Kingdom | 
| 5.8.1.2 | 5.8.0.0/19 | EU | 欧洲 | RU | 俄罗斯 | 
| 192.165.12.17 | 
IPv4 查找 - 在 external_data() 中使用源
let IP_Data = external_data(network:string,geoname_id:long,continent_code:string,continent_name:string ,country_iso_code:string,country_name:string,is_anonymous_proxy:bool,is_satellite_provider:bool)
    ['https://raw.githubusercontent.com/datasets/geoip2-ipv4/master/data/geoip2-ipv4.csv'];
let IPs = datatable(ip:string)
[
    '2.20.183.12',   // United Kingdom
    '5.8.1.2',       // Russia
    '192.165.12.17', // Sweden
];
IPs
| evaluate ipv4_lookup(IP_Data, ip, network, return_unmatched = true)
输出
| ip | network | geoname_id | continent_code | continent_name | country_iso_code | country_name | is_anonymous_proxy | is_satellite_provider | 
|---|---|---|---|---|---|---|---|---|
| 2.20.183.12 | 2.20.183.0/24 | 2635167 | EU | 欧洲 | GB | 英国 | 0 | 0 | 
| 5.8.1.2 | 5.8.0.0/19 | 2017370 | EU | 欧洲 | RU | 俄罗斯 | 0 | 0 | 
| 192.165.12.17 | 192.165.8.0/21 | 2661886 | EU | 欧洲 | SE | 瑞典 | 0 | 0 | 
IPv4 查找 - 使用额外的列进行匹配
let IP_Data = external_data(network:string,geoname_id:long,continent_code:string,continent_name:string ,country_iso_code:string,country_name:string,is_anonymous_proxy:bool,is_satellite_provider:bool)
    ['https://raw.githubusercontent.com/datasets/geoip2-ipv4/master/data/geoip2-ipv4.csv'];
let IPs = datatable(ip:string, continent_name:string, country_iso_code:string)
[
    '2.20.183.12',   'Europe', 'GB', // United Kingdom
    '5.8.1.2',       'Europe', 'RU', // Russia
    '192.165.12.17', 'Europe', '',   // Sweden is 'SE' - so it won't be matched
];
IPs
| evaluate ipv4_lookup(IP_Data, ip, network, continent_name, country_iso_code)
输出
| ip | continent_name | country_iso_code | network | geoname_id | continent_code | country_name | is_anonymous_proxy | is_satellite_provider | 
|---|---|---|---|---|---|---|---|---|
| 2.20.183.12 | 欧洲 | GB | 2.20.183.0/24 | 2635167 | EU | 英国 | 0 | 0 | 
| 5.8.1.2 | 欧洲 | RU | 5.8.0.0/19 | 2017370 | EU | 俄罗斯 | 0 | 0 |