package udf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
public class IpAddressTransaction extends UDF {
public Text evaluate(Text ip) {
String ipAdd = ip.toString();
String result = String.valueOf(ipToLong(ipAdd));
return new Text(result);
}
//ip转十进制
public static long ipToLong(String ip) {
String[] arr = ip.split("\\.");
long result = 0;
for (int i = 0; i <= 3; i++) {
long ipadd = Long.parseLong(arr[i]);
result |= ipadd << ((3-i) << 3);
}
return result;
}
public static void main(String[] args) {
//测试
System.out.println(ipToLong("192.168.1.2"));
System.out.println(ipToLong("192.246.99.61"));
}
}