gpt4 book ai didi

google-bigquery - 如何在 BigQuery 标准 SQL 中将 IP 地址转换为地理位置?

转载 作者:行者123 更新时间:2023-12-02 21:58:52 25 4
gpt4 key购买 nike

所以我读过https://cloudplatform.googleblog.com/2014/03/geoip-geolocation-with-google-bigquery.html

但我想知道是否有一种#standardSQL 方法可以做到这一点。到目前为止,我在转换 PARSE_IP 和 NTH() 时遇到了很多挑战,因为迁移文档中建议的更改有局限性。

PARSE_IP(contributor_ip)NET.IPV4_TO_INT64(NET.SAFE_IP_FROM_STRING(contributor_ip)) 不适用于 IPv6 IP 地址。

NTH(1, latitude) latlatitude[SAFE_ORDINAL(1)] 不起作用,因为纬度被视为字符串。

而且可能还有更多我还没有遇到的迁移问题。有谁知道如何在 BigQuery 标准 SQL 中将 IP 地址转换为地理位置?

附注我如何从地理位置到确定时区?

编辑:那么这有什么区别

#legacySQL
SELECT
COUNT(*) c,
city,
countryLabel,
NTH(1, latitude) lat,
NTH(1, longitude) lng
FROM (
SELECT
INTEGER(PARSE_IP(contributor_ip)) AS clientIpNum,
INTEGER(PARSE_IP(contributor_ip)/(256*256)) AS classB
FROM
[publicdata:samples.wikipedia]
WHERE
contributor_ip IS NOT NULL ) AS a
JOIN EACH
[fh-bigquery:geocode.geolite_city_bq_b2b] AS b
ON
a.classB = b.classB
WHERE
a.clientIpNum BETWEEN b.startIpNum
AND b.endIpNum
AND city != ''
GROUP BY
city,
countryLabel
ORDER BY
1 DESC

SELECT
COUNT(*) c,
city,
countryLabel,
ANY_VALUE(latitude) lat,
ANY_VALUE(longitude) lng
FROM (
SELECT
CASE
WHEN BYTE_LENGTH(contributor_ip) < 16 THEN SAFE_CAST(NET.IPV4_TO_INT64(NET.SAFE_IP_FROM_STRING(contributor_ip)) AS INT64)
ELSE NULL
END AS clientIpNum,
CASE
WHEN BYTE_LENGTH(contributor_ip) < 16 THEN SAFE_CAST(NET.IPV4_TO_INT64(NET.SAFE_IP_FROM_STRING(contributor_ip)) / (256*256) AS INT64)
ELSE NULL
END AS classB
FROM
`publicdata.samples.wikipedia`
WHERE
contributor_ip IS NOT NULL ) AS a
JOIN
`fh-bigquery.geocode.geolite_city_bq_b2b` AS b
ON
a.classB = b.classB
WHERE
a.clientIpNum BETWEEN b.startIpNum
AND b.endIpNum
AND city != ''
GROUP BY
city,
countryLabel
ORDER BY
1 DESC

edit2:看来我通过不正确地转换 float 来解决问题。目前,标准 SQL 返回 41815 行,而不是旧 SQL 中的 56347 行,这可能是由于标准 SQL 缺乏从 IPv6 到 int 的转换,但也可能是其他原因。此外,旧版 SQL 查询的性能要好得多,运行时间约为 10 秒,而不是标准 SQL 的整整一分钟。

最佳答案

根据https://gist.github.com/matsukaz/a145c2553a0faa59e32ad7c25e6a92f7

#standardSQL
SELECT
id,
IFNULL(city, 'Other') AS city,
IFNULL(countryLabel, 'Other') AS countryLabel,
latitude,
longitude
FROM (
SELECT
id,
NET.IPV4_TO_INT64(NET.IP_FROM_STRING(ip)) AS clientIpNum,
TRUNC(NET.IPV4_TO_INT64(NET.IP_FROM_STRING(ip))/(256*256)) AS classB
FROM
`<project>.<dataset>.log` ) AS a
LEFT OUTER JOIN
`fh-bigquery.geocode.geolite_city_bq_b2b` AS b
ON
a.classB = b.classB
AND a.clientIpNum BETWEEN b.startIpNum AND b.endIpNum
ORDER BY
id ASC

关于google-bigquery - 如何在 BigQuery 标准 SQL 中将 IP 地址转换为地理位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46062598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com