Hive清洗emoji及\\0

让人想犯罪 __ 提交于 2020-02-02 09:50:41

写的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();
		}
	}
}

  

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!