sparkSession.udf().register("TEXTSEG", new UDF1<Object, String>() {
private static final long serialVersionUID = 1L;
@SuppressWarnings("unchecked")
@Override
public String call(Object segColValue) throws Exception {
if (segColValue == null || StringUtils.isBlank(segColValue.toString())) {
return null;
}
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
// 设置大小写格式
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
String lastStr = "";
// String dateStr = "";
char[] charArray = String.valueOf(segColValue).toCharArray();
for (int i = 0; i < String.valueOf(segColValue).length(); i++) {
char a = charArray[i];
if (((int) a >= 65 && (int) a <= 90) || ((int) a >= 97 && (int) a <= 122)) {
lastStr += String.valueOf(a).toUpperCase();
} else {
if (Character.toString(charArray[i]).matches("[\\u4E00-\\u9FA5]+")) {
String[] hanyuPinyinStringArray = PinyinHelper
.toHanyuPinyinStringArray(charArray[i], defaultFormat);
lastStr += String.valueOf(hanyuPinyinStringArray[0].charAt(0));
} else {
lastStr += a;
}
}
}
return lastStr;
}
}, DataTypes.StringType);
result = result.withColumn(replaceJson.getString(key), functions.callUDF("TEXTSEG", result.col(key)));