UDF-Java提取身份证内信息

1.使用方法

#获取省份ID
create temporary function getProvIdByCardIdUDF as 'com.st.PersonInfoByCardIdUDF.getProvIdByCardIdUDF' 
using jar 'hdfs:///HIVE_UDF/PersonInfoByCardIdUDF.jar';
#获取省份名称
create temporary function getProvNameByCardIdUDF as 'com.st.PersonInfoByCardIdUDF.getProvNameByCardIdUDF' using jar 'hdfs:///HIVE_UDF/PersonInfoByCardIdUDF.jar';
#获取年龄
create temporary function getAgeByCardIdUDF as 'com.st.PersonInfoByCardIdUDF.getAgeByCardIdUDF' using jar 'hdfs:///HIVE_UDF/PersonInfoByCardIdUDF.jar';
#获取生日(YYYY-MM-DD)
create temporary function getBirthDayByCardIdUDF as 'com.st.PersonInfoByCardIdUDF.getBirthDayByCardIdUDF' using jar 'hdfs:///HIVE_UDF/PersonInfoByCardIdUDF.jar';
#获取性别(男、女、null)
create temporary function getGenderByCardIdUDF as 'com.st.PersonInfoByCardIdUDF.getGenderByCardIdUDF' using jar 'hdfs:///HIVE_UDF/PersonInfoByCardIdUDF.jar';

 

2.获取省份ID  

package com.st.PersonInfoByCardIdUDF;

import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.hive.ql.exec.UDF;

/*
 * Name:getProvIdByCardIdUDF
 * Author:Jim
 * Date:2018-06-29
 * Email:764309404@qq.com
 * Description:根据身份证获取省份ID
 * Example: 
 *         
 * */

public class getProvIdByCardIdUDF extends UDF{
    
    public static Map<String, String> map = new HashMap();
    
    //构造函数,获取省份名称
    public getProvIdByCardIdUDF() {
        map.put("11", "北京市");
        map.put("12", "天津市");
        map.put("13", "河北省");
        map.put("14", "山西省");
        map.put("15", "内蒙古自治区");
        map.put("21", "辽宁省");
        map.put("22", "吉林省");
        map.put("23", "黑龙江省");
        map.put("31", "上海市");
        map.put("32", "江苏省");
        map.put("33", "浙江省");
        map.put("34", "安徽省");
        map.put("35", "福建省");
        map.put("36", "江西省");
        map.put("37", "山东省");
        map.put("41", "河南省");
        map.put("42", "湖北省");
        map.put("43", "湖南省");
        map.put("44", "广东省");
        map.put("45", "广西壮族自治区");
        map.put("46", "海南省");
        map.put("50", "重庆市");
        map.put("51", "四川省");
        map.put("52", "贵州省");
        map.put("53", "云南省");
        map.put("54", "西藏自治区");
        map.put("61", "陕西省");
        map.put("62", "甘肃省");
        map.put("63", "青海省");
        map.put("64", "宁夏回族自治区");
        map.put("65", "新疆维吾尔自治区");
        map.put("71", "台湾省");
        map.put("81", "香港特别行政区");
        map.put("82", "澳门特别行政区");
    }
    
    public String evaluate(String cardid) throws Exception {
        
        String provId = null ;
        
        cardid = cardid.trim(); 
        
        if(cardid.length()>=15) {
            try {
                
            String key = cardid.substring(0, 2);
            
            provId = key;
            
            }catch(Exception e) {
            
                provId = null;
                
            }
        }
        
        return provId;
    }
    
    
}

2.获取省份名称

package com.st.PersonInfoByCardIdUDF;

import java.util.HashMap;
import java.util.Map;

import org.apache.hadoop.hive.ql.exec.UDF;

/*
 * Name:getProvNameByCardIdUDF
 * Author:Jim
 * Date:2018-06-29
 * Email:764309404@qq.com
 * Description:根据身份证获取省份名称
 * Example: 
 *         
 * */

public class getProvNameByCardIdUDF extends UDF{
    
    public static Map<String, String> map = new HashMap();
    
    //构造函数,获取省份名称
    public getProvNameByCardIdUDF() {
        map.put("11", "北京市");
        map.put("12", "天津市");
        map.put("13", "河北省");
        map.put("14", "山西省");
        map.put("15", "内蒙古自治区");
        map.put("21", "辽宁省");
        map.put("22", "吉林省");
        map.put("23", "黑龙江省");
        map.put("31", "上海市");
        map.put("32", "江苏省");
        map.put("33", "浙江省");
        map.put("34", "安徽省");
        map.put("35", "福建省");
        map.put("36", "江西省");
        map.put("37", "山东省");
        map.put("41", "河南省");
        map.put("42", "湖北省");
        map.put("43", "湖南省");
        map.put("44", "广东省");
        map.put("45", "广西壮族自治区");
        map.put("46", "海南省");
        map.put("50", "重庆市");
        map.put("51", "四川省");
        map.put("52", "贵州省");
        map.put("53", "云南省");
        map.put("54", "西藏自治区");
        map.put("61", "陕西省");
        map.put("62", "甘肃省");
        map.put("63", "青海省");
        map.put("64", "宁夏回族自治区");
        map.put("65", "新疆维吾尔自治区");
        map.put("71", "台湾省");
        map.put("81", "香港特别行政区");
        map.put("82", "澳门特别行政区");
    }
    
    public String evaluate(String cardid) throws Exception {
        
        String provname = null ;
        
        cardid = cardid.trim(); 
        
        if(cardid.length()>=15) {
            try {
                
            String key = cardid.substring(0, 2);
            
            provname = map.get(key);
            
            }catch(Exception e) {
            
                provname = null;
                
            }
        }
        
        return provname;
    }
    
    
}

3.获取年龄

    

package com.st.PersonInfoByCardIdUDF;

import java.time.LocalDate;
import java.time.Period;

import org.apache.hadoop.hive.ql.exec.UDF;

/*
 * Name:getAgeByCardIdUDF
 * Author:Jim
 * Date:2018-06-29
 * Email:764309404@qq.com
 * Description:根据身份证获取年龄
 * Example:
 * 
 * */
public class getAgeByCardIdUDF extends UDF {

public int evaluate(String cardId) throws Exception {
        
        int age = 0 ;
        
        try {
        
        LocalDate today = LocalDate.now();
        
        cardId = cardId.trim();
        
        if(cardId.length()==18) {
            
            String year_month_date_id = cardId.substring(6,14);
            String year = year_month_date_id.substring(0, 4);
            String month = year_month_date_id.substring(4, 6);
            String date = year_month_date_id.substring(6,8);
            LocalDate birthDate = LocalDate.parse(year+"-"+month+"-"+date);
            Period p = Period.between(birthDate, today);
            age = p.getYears() + 1;
            
        }else if (cardId.length()==15) {
            
            String year_month_date_id = cardId.substring(6,12);
            String year = "19" + year_month_date_id.substring(0, 2);
            String month = year_month_date_id.substring(2, 4);
            String date = year_month_date_id.substring(4,6);
            LocalDate birthDate = LocalDate.parse(year+"-"+month+"-"+date);
            Period p = Period.between(birthDate, today);
            age = p.getYears() + 1 ;
            
        }else {
            age = 0;
        }
        
        }catch(Exception e) {
            
            age = 0;
            
        }
        return age;
        
    }
    
}

 

4.获取生日

package com.st.PersonInfoByCardIdUDF;
import java.time.LocalDate;
import java.time.Period;

import org.apache.hadoop.hive.ql.exec.UDF;

/*
 * Name:getBirthDayByCardIdUDF
 * Author:Jim
 * Date:2018-06-29
 * Email:764309404@qq.com
 * Description:根据身份证获取生日 YYYY-MM-DD
 * Example: 
 *         
 * */

public class getBirthDayByCardIdUDF extends UDF{
    
public String evaluate(String cardId) throws Exception {
        
        String birthday = null;
        
        try
        {
        cardId = cardId.trim();
        
        if(cardId.length()==18) {
            String year_month_date_id = cardId.substring(6,14);
            String year = year_month_date_id.substring(0, 4);
            String month = year_month_date_id.substring(4, 6);
            String date = year_month_date_id.substring(6,8);
            //LocalDate birthDate = LocalDate.parse(year+"-"+month+"-"+date);
            birthday = year+"-"+month+"-"+date;
            
        }else if (cardId.length()==15) {
            String year_month_date_id = cardId.substring(6,12);
            String year = "19" + year_month_date_id.substring(0, 2);
            String month = year_month_date_id.substring(2, 4);
            String date = year_month_date_id.substring(4,6);
            //LocalDate birthDate = LocalDate.parse(year+"-"+month+"-"+date);
            birthday = year+"-"+month+"-"+date;
            
        }else {
            birthday = null;
        }
        }catch(Exception e) {
            
            birthday = null;
            
        }
        return birthday;
        
    }

}

 

5.获取性别

    

package com.st.PersonInfoByCardIdUDF;

import org.apache.hadoop.hive.ql.exec.UDF;

/*
 * Name:getBirthDayByCardIdUDF
 * Author:Jim
 * Date:2018-06-29
 * Email:764309404@qq.com
 * Description:根据身份证获取生日 YYYY-MM-DD
 * Example:       
 * 返回值:
 *     1:男
 *     2:女
 *     3.NULL
 * */

public class getGenderByCardIdUDF extends UDF{

    public String evaluate(String cardId) throws Exception {
        
        String gender = null;
        try {
            cardId = cardId.trim();
            if(cardId.length()==15 || cardId.length() == 18 ) {
                 int s = cardId.charAt(cardId.length()-2) - '0';
                 if(s%2==0) {
                     gender = "女";
                 }else {
                     gender = "男";
                 }   
            }
        }catch(Exception e) {
            gender = null;
        }
        
        return gender;
        
    }

}

 

posted @ 2018-07-23 10:52  Blue眼泪2016  阅读(601)  评论(0编辑  收藏  举报