将hive函数转换为java - translate和regexp_replace

时间:2013-07-27 09:35:47

标签: java regex parsing hadoop hive

1)如何将下面的hive部分转换为java map reduce?

 translate(regexp_replace(colA,"(\\\\=)","\\\\equalto"),"\[\]\(\)\{\}\^\?\+\*\$","____________") 

在regexp_replace中,我替换所有=,在外部翻译中,我替换所有影响未来regexp_replace解析的字符。(如果我不替换这些字符,他们会提出一个异常后来)。

2)我是否必须使用replaceChars(),如果是,那么如何?

示例字符串格式为:

tag1=573 tag2=ABC 0nuif6d Saturn 0i899 AA 0 (WORD) LOWER 0 (WORD2) HH 0 BB 0 CC 1 LL 0 D 0 FF 0 AB 0 UPPER 0 (ONCOLD) UPPER 1 PART: Sold \= 88vb JJ number\= 0 String "String_here" ANDND JUJFNG fill EXTRA SUNSET: empty tag3=/Informational tag4=/Value tag5=Value1/Value2 tag6=/AB/Acs Sy/Api Afg Hold Cones/HHH+11: 4.3.2-4.3.4 tag6=11123 tag7=Hello World tag8=a-dfdAds\=\= tag9=Value3 tag.9=Space separated words \= 88 , cold 87 Goal Run\=2, LOT OF SPACE SEPARATED GARBAGE WORDS tag.a=0( tag.b=02

注意:标签不是硬编码为标签。它们可以是任何英语单词,如serial_number或website.address,如serial_no=hello world website.address=\SO.com=/question,其中serial_nowebsite.address是标记。

1 个答案:

答案 0 :(得分:2)

描述

此表达式将:

  • 假设标签名称不包含空格
  • 假设代码名称与其各自的值相隔==符号两侧没有空格,\
  • 假设标签名称与带有空格的前一个字符串分开,同样值将通过空格与下一个标签名称分开
  • 捕获标记名称和值
  • 将避免在嵌入字符串值侧的等号上打破字符串

(\S*?)(?<!\\)=(\S*.*?)(?=\S*(?<!\\)=|\Z)

enter image description here

然后,您可以根据需要重新组合或进一步处理字符串的各个组件。

实施例

Live Demo

示例文字

来自评论中包含的示例文本。对于将名称与值分开的标记或等号的定义并不是很清楚:

serial_no=hello world website.address=\SO.com=/question tag1=573 tag2=ABC 0nuif6d Saturn 0i899 AA 0 (WORD) LOWER 0 (WORD2) HH 0 BB 0 CC 1 LL 0 D 0 FF 0 AB 0 UPPER 0 (ONCOLD) UPPER 1 PART: Sold \= 88vb JJ number\= 0 String "String_here" ANDND JUJFNG fill EXTRA SUNSET: empty tag3=/Informational tag4=/Value tag5=Value1/Value2 tag6=/AB/Acs Sy/Api Afg Hold Cones/HHH+11: 4.3.2-4.3.4 tag6=11123 tag7=Hello World tag8=a-dfdAds\=\= tag9=Value3 tag.9=Space separated words \= 88 , cold 87 Goal Run\=2, LOT OF SPACE SEPARATED GARBAGE WORDS tag.a=0( tag.b=02

示例代码

import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
  public static void main(String[] asd){
  String sourcestring = "source string to match with pattern";
    Pattern re = Pattern.compile("(\\S*?)(?<!\\\\)=(\\S*.*?)(?=\\S*(?<!\\\\)=|\\Z)",Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);

  Matcher m = re.matcher(sourcestring);
  int mIdx = 0;
    while (m.find()){
      for( int groupIdx = 0; groupIdx < m.groupCount()+1; groupIdx++ ){
        System.out.println( "[" + mIdx + "][" + groupIdx + "] = " + m.group(groupIdx));
      }
      mIdx++;
    }
  }
}

<强>匹配

组0将拥有整个子字符串 第1组将具有名称字段 第2组将具有值字段

[0][0] = serial_no=hello world 
[0][1] = serial_no
[0][2] = hello world 

[1][0] = website.address=\SO.com=/question
[1][1] = website.address
[1][2] = \SO.com=/question

[2][0] = tag1=573 
[2][1] = tag1
[2][2] = 573 

[3][0] = tag2=ABC 0nuif6d Saturn 0i899 AA 0 (WORD) LOWER 0 (WORD2) HH 0 BB 0 CC 1 LL 0 D 0 FF 0 AB 0 UPPER 0 (ONCOLD) UPPER 1 PART: Sold \= 88vb JJ number\= 0 String "String_here" ANDND JUJFNG fill EXTRA SUNSET: empty 
[3][1] = tag2
[3][2] = ABC 0nuif6d Saturn 0i899 AA 0 (WORD) LOWER 0 (WORD2) HH 0 BB 0 CC 1 LL 0 D 0 FF 0 AB 0 UPPER 0 (ONCOLD) UPPER 1 PART: Sold \= 88vb JJ number\= 0 String "String_here" ANDND JUJFNG fill EXTRA SUNSET: empty 

[4][0] = tag3=/Informational 
[4][1] = tag3
[4][2] = /Informational 

[5][0] = tag4=/Value 
[5][1] = tag4
[5][2] = /Value 

[6][0] = tag5=Value1/Value2 
[6][1] = tag5
[6][2] = Value1/Value2 

[7][0] = tag6=/AB/Acs Sy/Api Afg Hold Cones/HHH+11: 4.3.2-4.3.4 
[7][1] = tag6
[7][2] = /AB/Acs Sy/Api Afg Hold Cones/HHH+11: 4.3.2-4.3.4 

[8][0] = tag6=11123 
[8][1] = tag6
[8][2] = 11123 

[9][0] = tag7=Hello World 
[9][1] = tag7
[9][2] = Hello World 

[10][0] = tag8=a-dfdAds\=\= 
[10][1] = tag8
[10][2] = a-dfdAds\=\= 

[11][0] = tag9=Value3 
[11][1] = tag9
[11][2] = Value3 

[12][0] = tag.9=Space separated words \= 88 , cold 87 Goal Run\=2, LOT OF SPACE SEPARATED GARBAGE WORDS 
[12][1] = tag.9
[12][2] = Space separated words \= 88 , cold 87 Goal Run\=2, LOT OF SPACE SEPARATED GARBAGE WORDS 

[13][0] = tag.a=0( 
[13][1] = tag.a
[13][2] = 0( 

[14][0] = tag.b=02
[14][1] = tag.b
[14][2] = 02