组织机构代码校验udf关键代码
/*
* Copyright 2023 tu.cn All right reserved. This software is the
* confidential and proprietary information of tu.cn ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with Tu.cn
*/
package com.audaque.udf;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author DELL
* @date 2023/11/20 - 18:11
*/
public class OrganizationCodeVerification extends UDF {
private static final String YES = "0";
private static final String NO = "1";
private static final String REGEX = "^[0-9a-zA-Z]{8}[^0-9a-zA-Z]{0,1}[0-9a-zA-Z]{1}$";
private static final Pattern PATTERN = Pattern.compile("^[0-9a-zA-Z]{8}[^0-9a-zA-Z]{0,1}[0-9a-zA-Z]{1}$");
/*加权因子*/
private static final List<Integer> FACTORS;
private static final Map<Character, Integer> CODE_MAPPING;
private static final Map<Integer, Character> VALUE_MAPPING;
public Text evaluate(Text s) {
if (s == null)
return null;
String text = s.toString();
if (text.length() != 9 && text.length() != 10)
return new Text(NO);
Matcher matcher = PATTERN.matcher(text);
if (!matcher.matches())
return new Text(NO);
String getText = text.replaceAll("[^0-9A-Za-z]","");
String text1_8 = getText.substring(0, 8);
System.out.println(text1_8);
int value9 = calculateChekeCode(text1_8);
int text9 = Character.toUpperCase(getText.charAt(8));
if (value9 != text9)
return new Text(NO);
return new Text(YES);
}
private int calculateChekeCode(String text) {
char[] charArray = text.toCharArray();
List<Integer> nums = new ArrayList<>();
for (char c : charArray) {
Integer num = CODE_MAPPING.get(Character.valueOf(Character.toUpperCase(c)));
nums.add(num);
}
int sum = 0;
for (int i = 0; i < nums.size(); i++) {
int value = ((Integer)nums.get(i)).intValue() * ((Integer)FACTORS.get(i)).intValue();
sum += value;
}
int value18 = VALUE_MAPPING.size() - sum % VALUE_MAPPING.size();
System.out.println(value18);
return ((Character)VALUE_MAPPING.get(Integer.valueOf(value18))).charValue();
}
static {
Map<Integer, Character> map1 = new HashMap<>(16);
map1.put(Integer.valueOf(1), Character.valueOf('1'));
map1.put(Integer.valueOf(2), Character.valueOf('2'));
map1.put(Integer.valueOf(3), Character.valueOf('3'));
map1.put(Integer.valueOf(4), Character.valueOf('4'));
map1.put(Integer.valueOf(5), Character.valueOf('5'));
map1.put(Integer.valueOf(6), Character.valueOf('6'));
map1.put(Integer.valueOf(7), Character.valueOf('7'));
map1.put(Integer.valueOf(8), Character.valueOf('8'));
map1.put(Integer.valueOf(9), Character.valueOf('9'));
map1.put(Integer.valueOf(10), Character.valueOf('X'));
map1.put(Integer.valueOf(11), Character.valueOf('0'));
VALUE_MAPPING = Collections.unmodifiableMap(map1);
Map<Character, Integer> map = new HashMap<>(32);
map = new HashMap<>(64);
map.put(Character.valueOf('0'), Integer.valueOf(0));
map.put(Character.valueOf('1'), Integer.valueOf(1));
map.put(Character.valueOf('2'), Integer.valueOf(2));
map.put(Character.valueOf('3'), Integer.valueOf(3));
map.put(Character.valueOf('4'), Integer.valueOf(4));
map.put(Character.valueOf('5'), Integer.valueOf(5));
map.put(Character.valueOf('6'), Integer.valueOf(6));
map.put(Character.valueOf('7'), Integer.valueOf(7));
map.put(Character.valueOf('8'), Integer.valueOf(8));
map.put(Character.valueOf('9'), Integer.valueOf(9));
map.put(Character.valueOf('A'), Integer.valueOf(10));
map.put(Character.valueOf('B'), Integer.valueOf(11));
map.put(Character.valueOf('C'), Integer.valueOf(12));
map.put(Character.valueOf('D'), Integer.valueOf(13));
map.put(Character.valueOf('E'), Integer.valueOf(14));
map.put(Character.valueOf('F'), Integer.valueOf(15));
map.put(Character.valueOf('G'), Integer.valueOf(16));
map.put(Character.valueOf('H'), Integer.valueOf(17));
map.put(Character.valueOf('I'), Integer.valueOf(18));
map.put(Character.valueOf('J'), Integer.valueOf(19));
map.put(Character.valueOf('K'), Integer.valueOf(20));
map.put(Character.valueOf('L'), Integer.valueOf(21));
map.put(Character.valueOf('M'), Integer.valueOf(22));
map.put(Character.valueOf('N'), Integer.valueOf(23));
map.put(Character.valueOf('O'), Integer.valueOf(24));
map.put(Character.valueOf('P'), Integer.valueOf(25));
map.put(Character.valueOf('Q'), Integer.valueOf(26));
map.put(Character.valueOf('R'), Integer.valueOf(27));
map.put(Character.valueOf('S'), Integer.valueOf(28));
map.put(Character.valueOf('T'), Integer.valueOf(29));
map.put(Character.valueOf('U'), Integer.valueOf(30));
map.put(Character.valueOf('V'), Integer.valueOf(31));
map.put(Character.valueOf('W'), Integer.valueOf(32));
map.put(Character.valueOf('X'), Integer.valueOf(33));
map.put(Character.valueOf('Y'), Integer.valueOf(34));
map.put(Character.valueOf('Z'), Integer.valueOf(35));
CODE_MAPPING = Collections.unmodifiableMap(map);
List<Integer> list = new ArrayList<>(17);
list = new ArrayList<>(8);
list.add(Integer.valueOf(3));
list.add(Integer.valueOf(7));
list.add(Integer.valueOf(9));
list.add(Integer.valueOf(10));
list.add(Integer.valueOf(5));
list.add(Integer.valueOf(8));
list.add(Integer.valueOf(4));
list.add(Integer.valueOf(2));
FACTORS = Collections.unmodifiableList(list);
}
public static void main(String[] args) {
OrganizationCodeVerification abc = new OrganizationCodeVerification();
System.out.println(abc.evaluate(new Text("568528449")));
}
}