Created
July 4, 2016 14:21
-
-
Save OElesin/f0f2c69530a315177b9e0227a140f9c1 to your computer and use it in GitHub Desktop.
This service class converts IP to long and reverse. This class is useful for generating userIDs from IP addresses if user IDs are not present in your data.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ipToLong(ipAddress: String): Long = { | |
ipAddress.split("\\.").reverse.zipWithIndex.map(a=>a._1.toInt*math.pow(256,a._2).toLong).sum | |
} | |
def longToIP(long: Long): String = { | |
(0 until 4).map(a=>long / math.pow(256, a).floor.toInt % 256).reverse.mkString(".") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment