在pyspark中将字符串ip列转换为整数

oxosxuxt  于 2021-05-27  发布在  Spark
关注(0)|答案(1)|浏览(543)

我有一个pysparkDataframe,ipv4值是字符串,我想把它们转换成整数值。最好没有可能对性能有很大影响的udf。
输入示例:

+---------------+
|         IP_str|
+---------------+
|      4.8.1.235|
|    50.17.11.18|
|   48.104.99.80|
+---------------+

输出示例:

+---------------+
|         IP_int|
+---------------+
|       67633643|
|      839977746|
|      812147536|
+---------------+
ddarikpa

ddarikpa1#

你可以 multiplynumber 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|

+------------+---------+

相关问题