我有一个pysparkDataframe,ipv4值是字符串,我想把它们转换成整数值。最好没有可能对性能有很大影响的udf。
输入示例:
+---------------+
| IP_str|
+---------------+
| 4.8.1.235|
| 50.17.11.18|
| 48.104.99.80|
+---------------+
输出示例:
+---------------+
| IP_int|
+---------------+
| 67633643|
| 839977746|
| 812147536|
+---------------+
1条答案
按热度按时间ddarikpa1#
你可以
multiply
与number of addresses
每个网络(即。16777216,65536,256,1
).分裂
ip_address
与.
和地址数相乘。Example:
```df.show()
+------------+
| IP_str|
+------------+
| 4.8.1.235|
| 50.17.11.18|
|48.104.99.80|
+------------+
df.withColumn("IP_int",split(col("IP_str"),".")[0]*16777216 +split(col("IP_str"),".")[1]*65536+ split(col("IP_str"),".")[2]*256 + split(col("IP_str"),".")[3]).
show()
+------------+---------+
| IP_str| IP_int|
+------------+---------+
| 4.8.1.235| 67633643|
| 50.17.11.18|839977746|
|48.104.99.80|812147536|
+------------+---------+