1
我如何使用SPARK SQL(hiveish)轉換爲IPv4的IP地址從字符串轉換爲長表示轉換的IPv4字符串的BIGINT表示火花SQL
我如何使用SPARK SQL(hiveish)轉換爲IPv4的IP地址從字符串轉換爲長表示轉換的IPv4字符串的BIGINT表示火花SQL
這可以用乘法來完成:
sqlContext.sql("""SELECT
(cast(split(l.klientip,'\\.')[0] as bigint) * 16777216)
+ (cast(split(l.klientip,'\\.')[1] as bigint) * 65536)
+ (cast(split(l.klientip,'\\.')[2] as bigint) * 256)
+ (cast(split(l.klientip,'\\.')[3] as bigint)) ip_add_int
from loggik l
""").show()
或與偏移(與乘法相同)
sqlContext.sql("""SELECT
(shiftLeft(cast(split(l.klientip,'\\.')[0] as bigint),24)
+shiftLeft(cast(split(l.klientip,'\\.')[1] as bigint),16)
+shiftLeft(cast(split(l.klientip,'\\.')[2] as bigint),8)
+cast(split(l.klientip,'\\.')[3] as bigint)) ip_shift_int
from loggik l
""").show()