
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