利用otter做mysql資料實時脫敏

czxin788發表於2021-01-12


java程式碼:

package com.alibaba.otter;
import java.util.Objects;
import org.apache.commons.lang.StringUtils;
import com.alibaba.otter.node.extend.processor.AbstractEventProcessor;
import com.alibaba.otter.shared.etl.model.EventColumn;
import com.alibaba.otter.shared.etl.model.EventData;
import com.alibaba.otter.shared.etl.model.EventType;
public class TransferProcessor extends AbstractEventProcessor{
     
    public boolean process(EventData eventData) {
        String eventType = eventData.getEventType().getValue();
        if(eventType.equals(EventType.QUERY.getValue())) {
            return true;
        }
         
        ColumnInfoEnum[] values = ColumnInfoEnum.values();
        for(ColumnInfoEnum cie : values) {
            EventColumn column = getColumn(eventData, cie.getColumnKey());
            if(Objects.nonNull(column)) {
                if(StringUtils.isNotBlank(column.getColumnValue())) {
                    column.setColumnValue(replace(column.getColumnValue(), cie.getPrefixLength(), cie.getSubffixLength()));
                }
            }
        }
        return true;
    }
     
    public static void main(String[] args) {
        System.out.println(replace("內蒙古呼和浩特市巴林右旗罕吐柏 村6組", 6, 0));
    }
     
    private static String replace(String sourceValue,int prefixLength, int subffixLength) {
        sourceValue = sourceValue.trim();
        if(StringUtils.isBlank(sourceValue)) {
            return "";
        }
        int length = sourceValue.length();
        if(length < (prefixLength + subffixLength)) {
            return "";
        }
        sourceValue = sourceValue.replaceAll("(\\s)", "*");
        System.out.println(sourceValue);
        int placeHolderLenth = length - prefixLength - subffixLength;
        String pattern = "(\\S{"+(prefixLength)+"})\\S{"+placeHolderLenth+"}(\\S{"+subffixLength+"})";
        StringBuilder placeHolder = new StringBuilder("$1");
        for(int i = 0; i < placeHolderLenth; i ++) {
            placeHolder.append("*");
        }
        placeHolder.append("$2");
         
        return sourceValue.replaceAll(pattern, placeHolder.toString());
    }
     
    enum ColumnInfoEnum {
        NAME("name",1,0),
        BIRTHDAY("birthday",4,0),
        ID_NUM("id_num",6,2),
        SELFPHONE("selfphone",3,4),
        FAMILYPHONE("familyphone",3,4);
         
        ColumnInfoEnum(String columnKey, int prefixLength, int subffixLength) {
            this.columnKey = columnKey;
            this.prefixLength = prefixLength;
            this.subffixLength = subffixLength;
        }
        private String columnKey;
        private int prefixLength;
        private int subffixLength;
        public String getColumnKey() {
            return columnKey;
        }
        public int getPrefixLength() {
            return prefixLength;
        }
        public int getSubffixLength() {
            return subffixLength;
        }
    }
}



最終效果:

mysql> select name,birthday,id_num,familyphone ,selfphone  from patient where familyphone is not null limit 10
    -> ;
+-------+----------+-------------------------------+---------------+--------------+
| name  | birthday | id_num                        | familyphone   | selfphone    |
+-------+----------+-------------------------------+---------------+--------------+
| 測**  | 1980**   | jshhsh*****sh                 | 148*****4774  | 137*****9944 |
| 何**  | 1988**   | 320908*****61                 | 139*****5632  | 139*****5632 |
| 大**  | 1983**   | 偷摸咯了了啃*****咳咳         | ¥89*****6699  | 135*****4578 |
| 預**  | 1974**   | hdhdhd*****dd                 | 136*****4646  | 134*****4646 |
| 好**  | 1978**   | hk3562*****66                 | 135*****5555  | 132*****5555 |
| 預**  | 1978**   | 646464*****46                 | 135*****7855  | 135*****5555 |
| 我**  | 1980**   | 還不行八點半*****點半         | 139*****6464  | 135*****4664 |
| 李**  | 2017**   | NULL                          | 199*****4372  | NULL         |
| 韓**  | 1965**   | 640122*****38                 | 151*****3918  | 150*****9975 |
| 德**  | 2008**   | NULL                          | 177*****4400  | NULL         |
+-------+----------+-------------------------------+---------------+--------------+



注意:用這個脫敏功能,一定要配置JAVA_HOME變數,如下:

 cat .bash_profile 
JAVA_HOME=/usr/lib/jvm/java-1.8.0
CLASS_PATH=.:$JAVA_HOME/lib
export JAVA_HOME
export CLASS_PATH



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28916011/viewspace-2749425/,如需轉載,請註明出處,否則將追究法律責任。

相關文章