我有成千上万的IP,并希望确定哪些属于某个范围。 范围:
64.233.160.0 / 8192
66.102.0.0 / 4096
66.249.64.0 / 8192
72.14.192.0 / 16384
74.125.0.0 / 65536
209.85.128.0 / 32768
216.239.32.0 / 8192
所以我将这些范围转换为以下内容:
64.233.160.0 - 64.233.192.0
66.102.0.0 - 66.102.16.0
66.249.64.0 - 66.249.96.0
72.14.192.0 - 72.15.0.0
74.125.0.0 - 74.126.0.0
209.85.128.0 - 209.86.0.0
216.239.32.0 - 216.239.64.0
所以现在我想查询IP地址是否在这些范围内。 SQL不会理解八位字节,所以我不知道该怎么做。
可以使用一些Hex2Dec / Dec2Hex转换吗?
我认为这应该是之前已经完成的事情,我确定我不是第一个尝试使用ip范围识别特定ip的人。
我将查看多个IP地址,因此有些可能是20.0.1.123而另一个可能是124.123.123.1,即八位字节的格式不一样
答案 0 :(得分:3)
IP号码实际上只是整数。你在这里做的是你已经将它们保存为人类可读的字符串。您需要将它们转换回原始的整数表示形式,以便您可以使用正常的BETWEEN来声明查询。
答案 1 :(得分:3)
您可以IP functions为此:
Error creating bean with name 'org.springframework.data.repository.core.support.RepositoryInterfaceAwareBeanPostProcessor#0': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.config.internalEhCacheCachingAdvisor': Cannot resolve reference to bean 'com.googlecode.ehcache.annotations.impl.CacheStaticMethodMatcherPointcut#0' while setting bean property 'pointcut'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.googlecode.ehcache.annotations.impl.CacheStaticMethodMatcherPointcut#0': BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.config.internalTransactionAdvisor': Cannot resolve reference to bean 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0' while setting bean property 'transactionAttributeSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0': BeanPostProcessor before instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/BCException
然后你可以将它们包裹在BETWEEN状态。
答案 2 :(得分:0)
Pentium10的答案对于Legacy Bigquery语法是正确的。对于恰好使用StandardSQL Bigquery语法的任何人,您正在寻找:
NET.IP_FROM_STRING(' 64.233.160.0')返回1089052672
答案 3 :(得分:0)
对于BigQuery Standard SQL,将IP转换为整数的方法如下
#standardSQL
SELECT NET.IPV4_TO_INT64(NET.IP_FROM_STRING('64.233.160.0'))
与SQL UDF一起,用法可以简化为下面的内容
#standardSQL
CREATE TEMP FUNCTION ip2int(ip STRING) AS (
NET.IPV4_TO_INT64(NET.IP_FROM_STRING(ip))
);
WITH Ranges AS (
SELECT '64.233.160.0' AS IP1, '64.233.192.0' AS IP2 UNION ALL
SELECT '66.102.0.0', '66.102.16.0' UNION ALL
SELECT '66.249.64.0', '66.249.96.0' UNION ALL
SELECT '72.14.192.0', '72.15.0.0' UNION ALL
SELECT '74.125.0.0', '74.126.0.0' UNION ALL
SELECT '209.85.128.0', '209.86.0.0' UNION ALL
SELECT '216.239.32.0', '216.239.64.0'
),
IPs AS (
SELECT '64.233.160.2' AS IP UNION ALL
SELECT '72.14.192.101'
)
SELECT *
FROM IPs AS i
JOIN Ranges AS r
ON ip2int(IP) BETWEEN ip2int(IP1) AND ip2int(IP2)
输出为
IP IP1 IP2
72.14.192.101 72.14.192.0 72.15.0.0
64.233.160.2 64.233.160.0 64.233.192.0
此处有关NET functions
和SQL UDF