歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Hive UDF 過濾字符串中的中英文標點符號

Hive UDF 過濾字符串中的中英文標點符號

日期:2017/3/1 9:27:57   编辑:Linux編程

使用Hive 過程中 需要做一些UDF的開發,簡單貼一個UDF 是用來去除字符串中的所有中英文符號

本人Java菜鳥 代碼可能不好看勿噴啊。僅供參考

package com.fccs.utils;

import java.text.ParseException;

import org.apache.Hadoop.hive.ql.exec.UDF;

/***
* 本類是用於字符串替換支持正則表達
* @author [email protected]
* @date 2015-5-28
* @version 1.0
*
*/
public class F_str_replace extends UDF{

public String evaluate(String str,String ...args) {
if(str != null){
str = str.trim();
}
if(str == null || "".equals(str)){
return "null";
}


return get_str_replace( str,args);

}

/**
* 傳入一個字符串,把所有符合條件的字符串和空字符轉換為“null”,不符合條件的就返回原字符串
* 比如:get_str_replace("金成·江南春城 (·竹海水韻)")
* 轉換後:"金成江南春城竹海水韻"
* @param subject
* @param args 多參數 args[0] 要替換成的字符串, args[1] 正則表達式
* @use:get_str_replace(subject,replacement,pattern)
* @return result
*/
private static String get_str_replace(String subject,String...args){

String text = "null";
if(subject != null){
subject = subject.trim();
}
if(subject == null || "".equals(subject)){
return "null";
}


if(args.length==0){
text = subject.replaceAll( "\\p{Punct}","" );
text = text.replaceAll("\\pP" , "");
text = text.replaceAll("\\p{P}" ,"");
text = text.replaceAll( "[\\p{P}+~$`^=|<>~`$^+=|<>¥×]" , "");

}else if (args.length==1){
String replacement = args[0].length()>0?args[0]:"";
text = subject.replaceAll( "\\p{Punct}",replacement );
text = text.replaceAll("\\pP" , replacement);
text = text.replaceAll("\\p{P}" ,replacement);
text = text.replaceAll( "[\\p{P}+~$`^=|<>~`$^+=|<>¥×]" , replacement);

}else{
String pattern = args[1];
String replacement = args[0] ;
text = subject.replaceAll( pattern,replacement);

}

text = text.replaceAll("\\s+", ""); //過濾多余空格
return text;
}

}

Hive編程指南 PDF 中文高清版 http://www.linuxidc.com/Linux/2015-01/111837.htm

基於Hadoop集群的Hive安裝 http://www.linuxidc.com/Linux/2013-07/87952.htm

Hive內表和外表的區別 http://www.linuxidc.com/Linux/2013-07/87313.htm

Hadoop + Hive + Map +reduce 集群安裝部署 http://www.linuxidc.com/Linux/2013-07/86959.htm

Hive本地獨立模式安裝 http://www.linuxidc.com/Linux/2013-06/86104.htm

Hive學習之WordCount單詞統計 http://www.linuxidc.com/Linux/2013-04/82874.htm

Hive運行架構及配置部署 http://www.linuxidc.com/Linux/2014-08/105508.htm

Hive 的詳細介紹:請點這裡
Hive 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved