Hive清洗emoji及\0

写的UDF

public class FilterEmojiUDF extends UDF {
	public String evaluate(String str) {
		if (str == null || str == "") {
			return null;
		} else {
			StringBuilder sb = new StringBuilder();
			str = str.replace("\0", "");
			byte[] bytes = str.getBytes(StandardCharsets.UTF_8);
			for (int i = 0; i < bytes.length; i++) {
				byte b = bytes[i];
				if (CharUtils.isAscii((char) b)) {
					sb.append(new String(new byte[] { b }));
				} else if ((b & 0xE0) == 0xC0) {
					sb.append(new String(new byte[] { b, bytes[++i] }));
				} else if ((b & 0xF0) == 0xE0) {
					sb.append(new String(new byte[] { b, bytes[++i], bytes[++i] }));
				} else if ((b & 0xF8) == 0xF0) {
					String str1 = new String(new byte[] { b, bytes[++i], bytes[++i], bytes[++i] });
					try {
						sb.append(URLEncoder.encode(str1, CharEncoding.UTF_8));
					} catch (Exception ignore) {

					}
				}
			}
			return sb.toString();
		}
	}
}

  

posted @ 2020-02-02 08:33  HarkLee  阅读(1523)  评论(0编辑  收藏  举报