mirror of
https://github.com/alibaba/DataX.git
synced 2025-05-02 07:21:48 +08:00
feature: 新增DigestTransformer
This commit is contained in:
parent
3225f02bf9
commit
24e80b55bb
@ -0,0 +1,87 @@
|
||||
package com.alibaba.datax.core.transport.transformer;
|
||||
|
||||
import com.alibaba.datax.common.element.Column;
|
||||
import com.alibaba.datax.common.element.Record;
|
||||
import com.alibaba.datax.common.element.StringColumn;
|
||||
import com.alibaba.datax.common.exception.DataXException;
|
||||
import com.alibaba.datax.transformer.Transformer;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* no comments.
|
||||
*
|
||||
* @author XuDaojie
|
||||
* @since 2021-08-16
|
||||
*/
|
||||
public class DigestTransformer extends Transformer {
|
||||
|
||||
private static final String MD5 = "md5";
|
||||
private static final String SHA1 = "sha1";
|
||||
private static final String TO_UPPER_CASE = "toUpperCase";
|
||||
private static final String TO_LOWER_CASE = "toLowerCase";
|
||||
|
||||
public DigestTransformer() {
|
||||
setTransformerName("dx_digest");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Record evaluate(Record record, Object... paras) {
|
||||
|
||||
int columnIndex;
|
||||
String type;
|
||||
String charType;
|
||||
|
||||
try {
|
||||
if (paras.length != 3) {
|
||||
throw new RuntimeException("dx_digest paras length must be 3");
|
||||
}
|
||||
|
||||
columnIndex = (Integer) paras[0];
|
||||
type = (String) paras[1];
|
||||
charType = (String) paras[2];
|
||||
|
||||
if (!StringUtils.equalsIgnoreCase(MD5, type) && !StringUtils.equalsIgnoreCase(SHA1, type)) {
|
||||
throw new RuntimeException("dx_digest paras index 1 must be md5 or sha1");
|
||||
}
|
||||
if (!StringUtils.equalsIgnoreCase(TO_UPPER_CASE, charType) && !StringUtils.equalsIgnoreCase(TO_LOWER_CASE, charType)) {
|
||||
throw new RuntimeException("dx_digest paras index 2 must be toUpperCase or toLowerCase");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_ILLEGAL_PARAMETER, "paras:" + Arrays.asList(paras) + " => " + e.getMessage());
|
||||
}
|
||||
|
||||
Column column = record.getColumn(columnIndex);
|
||||
|
||||
try {
|
||||
String oriValue = column.asString();
|
||||
|
||||
// 如果字段为空,作为空字符串处理
|
||||
if (oriValue == null) {
|
||||
oriValue = "";
|
||||
}
|
||||
String newValue;
|
||||
if (MD5.equals(type)) {
|
||||
newValue = DigestUtils.md5Hex(oriValue);
|
||||
} else {
|
||||
newValue = DigestUtils.sha1Hex(oriValue);
|
||||
}
|
||||
|
||||
if (TO_UPPER_CASE.equals(charType)) {
|
||||
newValue = newValue.toUpperCase();
|
||||
} else {
|
||||
newValue = newValue.toLowerCase();
|
||||
}
|
||||
|
||||
record.setColumn(columnIndex, new StringColumn(newValue));
|
||||
|
||||
} catch (Exception e) {
|
||||
throw DataXException.asDataXException(TransformerErrorCode.TRANSFORMER_RUN_EXCEPTION, e.getMessage(), e);
|
||||
}
|
||||
return record;
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +1,18 @@
|
||||
package com.alibaba.datax.core.transport.transformer;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
/**
|
||||
* GroovyTransformer的帮助类,供groovy代码使用,必须全是static的方法
|
||||
* Created by liqiang on 16/3/4.
|
||||
*/
|
||||
public class GroovyTransformerStaticUtil {
|
||||
|
||||
public static String md5(final String data) {
|
||||
return DigestUtils.md5Hex(data);
|
||||
}
|
||||
|
||||
public static String sha1(final String data) {
|
||||
return DigestUtils.sha1Hex(data);
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ public class TransformerRegistry {
|
||||
registTransformer(new ReplaceTransformer());
|
||||
registTransformer(new FilterTransformer());
|
||||
registTransformer(new GroovyTransformer());
|
||||
registTransformer(new DigestTransformer());
|
||||
}
|
||||
|
||||
public static void loadTransformerFromLocalStorage() {
|
||||
|
Loading…
Reference in New Issue
Block a user