为什么我在apache pig中使用replace函数时出错?

vpfxa7rd  于 2021-06-21  发布在  Pig
关注(0)|答案(2)|浏览(481)

我在执行命令

  1. TestData = FOREACH records Generate From as from, MsgId as Msg, REPLACE(toAddress,';' , ',');

我有以下错误
不匹配的字符“”应为“”2014-04-14 12:27:56863[main]error org.apache.pig.tools.grunt.grunt-错误1200:不匹配的字符“”应为“”
可能是因为;性格?如果是这样,那么如何为其应用修补程序。。

njthzxwz

njthzxwz1#

在我看来那只Pig 0.11.0-cdh4.3.0 不包括pig-2507。
您需要修补并重建pig以使其正常工作(从此处下载修补程序:https://issues.apache.org/jira/secure/attachment/12571848/pig_2507.patch)或者作为一种解决方法,您可以基于 org.apache.pig.builtin.REPLACE :
例如:

  1. package com.example;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import org.apache.pig.EvalFunc;
  6. import org.apache.pig.FuncSpec;
  7. import org.apache.pig.PigWarning;
  8. import org.apache.pig.data.Tuple;
  9. import org.apache.pig.data.DataType;
  10. import org.apache.pig.impl.logicalLayer.schema.Schema;
  11. import org.apache.pig.impl.logicalLayer.FrontendException;
  12. public class MyReplace extends EvalFunc<String> {
  13. private String searchString;
  14. public MyReplace(String searchString) {
  15. this.searchString = searchString;
  16. }
  17. @Override
  18. public String exec(Tuple input) throws IOException {
  19. if (input == null || input.size() < 2)
  20. return null;
  21. try {
  22. String source = (String) input.get(0);
  23. String replacewith = (String) input.get(1);
  24. return source.replaceAll(searchString, replacewith);
  25. }
  26. catch (Exception e) {
  27. warn("Failed to process input; error - " + e.getMessage(), PigWarning.UDF_WARNING_1);
  28. return null;
  29. }
  30. }
  31. @Override
  32. public Schema outputSchema(Schema input) {
  33. return new Schema(new Schema.FieldSchema(null, DataType.CHARARRAY));
  34. }
  35. /* (non-Javadoc)
  36. * @see org.apache.pig.EvalFunc#getArgToFuncMapping()
  37. */
  38. @Override
  39. public List<FuncSpec> getArgToFuncMapping() throws FrontendException {
  40. List<FuncSpec> funcList = new ArrayList<FuncSpec>();
  41. Schema s = new Schema();
  42. s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
  43. s.add(new Schema.FieldSchema(null, DataType.CHARARRAY));
  44. funcList.add(new FuncSpec(this.getClass().getName(), s));
  45. return funcList;
  46. }
  47. }

把它装在jar里,然后你就可以使用它了:

  1. register '/path/to/my.jar';
  2. DEFINE myReplace com.example.MyReplace(';');
  3. A = load 'data' as (a:chararray);
  4. B = FOREACH A generate myReplace(a,',');
  5. ...
展开查看全部
gxwragnw

gxwragnw2#

它也可以通过使用pig内置函数来实现。使用unicode转义序列代替replace函数调用中的特殊字符。对于上述问题,请使用:

  1. REPLACE (toAddress,'\\\\u003b','\u002c')

代替:

  1. REPLACE(toAddress,';' , ',')

相关问题