|
@@ -0,0 +1,112 @@
|
|
|
+package com.winhc.bigdata.udf;
|
|
|
+
|
|
|
+import com.aliyun.odps.udf.UDF;
|
|
|
+import com.aliyun.odps.utils.StringUtils;
|
|
|
+
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: π
|
|
|
+ * @Description: 身份证15转18位
|
|
|
+ */
|
|
|
+public class id_trans extends UDF {
|
|
|
+
|
|
|
+ public String evaluate(String id) {
|
|
|
+ if (StringUtils.isBlank(id)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if (isNumeric(id.substring(0,id.length() - 1))) {
|
|
|
+ if (id.length() == 18) {
|
|
|
+ return id;
|
|
|
+ } else if (id.length() == 15) {
|
|
|
+ return trans15bitTo18bit(id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String trans15bitTo18bit(String in) {
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ String[] input = in.split("", -1);
|
|
|
+ String[] result = new String[18];
|
|
|
+ for (int i = 0; i < input.length; i++) {
|
|
|
+ if (i <= 5) {
|
|
|
+ result[i] = input[i];
|
|
|
+ } else {
|
|
|
+ result[i + 2] = input[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result[6] = "1";
|
|
|
+ result[7] = "9";
|
|
|
+ //计算最后一位(前十七位乘以系数)
|
|
|
+ String[] xs = {"7", "9", "10", "5", "8", "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"};
|
|
|
+ int sum = 0;
|
|
|
+ for (int i = 0; i < 17; i++) {
|
|
|
+ sum += Integer.valueOf(result[i]) * Integer.valueOf(xs[i]);
|
|
|
+ }
|
|
|
+ //对11求余,的余数 0 - 10
|
|
|
+ int rod = sum % 11;
|
|
|
+ //所得余数映射到对应数字即可
|
|
|
+ if (rod == 0) {
|
|
|
+ result[17] = "1";
|
|
|
+ } else if (rod == 1) {
|
|
|
+ result[17] = "0";
|
|
|
+ } else if (rod == 2) {
|
|
|
+ result[17] = "X";
|
|
|
+ } else if (rod == 3) {
|
|
|
+ result[17] = "9";
|
|
|
+ } else if (rod == 4) {
|
|
|
+ result[17] = "8";
|
|
|
+ } else if (rod == 5) {
|
|
|
+ result[17] = "7";
|
|
|
+ } else if (rod == 6) {
|
|
|
+ result[17] = "6";
|
|
|
+ } else if (rod == 7) {
|
|
|
+ result[17] = "5";
|
|
|
+ } else if (rod == 8) {
|
|
|
+ result[17] = "4";
|
|
|
+ } else if (rod == 9) {
|
|
|
+ result[17] = "3";
|
|
|
+ } else if (rod == 10) {
|
|
|
+ result[17] = "2";
|
|
|
+ }
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (String c : result) {
|
|
|
+ sb.append(c);
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ } catch (Exception e) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 利用正则表达式判断字符串是否是数字
|
|
|
+ *
|
|
|
+ * @param str
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean isNumeric(String str) {
|
|
|
+ Pattern pattern = Pattern.compile("[0-9]*");
|
|
|
+ Matcher isNum = pattern.matcher(str);
|
|
|
+ if (!isNum.matches()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ String id = "5101025409306126";
|
|
|
+ System.out.println(isNumeric(id));
|
|
|
+ id_trans n = new id_trans();
|
|
|
+ System.out.println(n.evaluate(id));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|