regex - 级联-正则表达式解析器-错​​误的字段数

标签 regex hadoop cascading

开始在Amazon EMR上使用Cascading,并设法使其运行但以一个相当简单的障碍下降,而我希望有人能对此有所了解。

我的代码:

import java.util.Properties;

import cascading.flow.Flow;
import cascading.flow.FlowDef;
import cascading.flow.hadoop.HadoopFlowConnector;
import cascading.pipe.Pipe;
import cascading.property.AppProps;
import cascading.scheme.hadoop.TextLine;
import cascading.tap.Tap;
import cascading.tap.hadoop.Hfs;
import cascading.tuple.Fields;
import cascading.operation.regex.RegexParser;
import cascading.pipe.Each;
import cascading.tap.SinkMode;

public class Main
  {
  public static void
  main( String[] args )
    {
    String inPath = args[ 0 ];
    String outPath = args[ 1 ];

    Properties properties = new Properties();
    AppProps.setApplicationJarClass( properties, Main.class );
    HadoopFlowConnector flowConnector = new HadoopFlowConnector( properties );

    // create the source tap
    TextLine sourceScheme = new TextLine(new Fields("line"));
    Tap inTap = new Hfs( sourceScheme, inPath );

    // create the sink tap
    TextLine sinkScheme = new TextLine( new Fields("custid", "movieids"));
    Tap outTap = new Hfs( sinkScheme, outPath, SinkMode.REPLACE );

    Fields filmFields = new Fields("custid", "movieids");

    String filmRegex = "([0-9]:*[,.]*)";

    RegexParser parser = new RegexParser(filmFields, filmRegex);

    Pipe importPipe = new Each("import", new Fields("line"), parser, Fields.RESULTS );

    // connect the taps, pipes, etc., into a flow
    Flow parsedFlow = new HadoopFlowConnector(properties).connect(inTap, outTap, importPipe);

    // run the flow
    parsedFlow.start();
    parsedFlow.complete();
    }
  }

我的输入(无空行):

1:2

2:4

5:1

3:9

我的输出:
Task TASKID="task_201305241444_0003_m_000000" TASK_TYPE="MAP" TASK_STATUS="FAILED" FINISH_TIME="1369408133954" ERROR="cascading\.tuple\.TupleException: operation added the wrong number of fields, expected: ['custid', 'movieids'], got result size: 1
    at cascading\.tuple\.TupleEntryCollector\.add(TupleEntryCollector\.java:82)
    at cascading\.operation\.regex\.RegexParser\.onFoundGroups(RegexParser\.java:168)
    at cascading\.operation\.regex\.RegexParser\.operate(RegexParser\.java:151)
    at cascading\.flow\.stream\.FunctionEachStage\.receive(FunctionEachStage\.java:99)
    at cascading\.flow\.stream\.FunctionEachStage\.receive(FunctionEachStage\.java:39)
    at cascading\.flow\.stream\.SourceStage\.map(SourceStage\.java:102)
    at cascading\.flow\.stream\.SourceStage\.run(SourceStage\.java:58)
    at cascading\.flow\.hadoop\.FlowMapper\.run(FlowMapper\.java:127)
    at org\.apache\.hadoop\.mapred\.MapTask\.runOldMapper(MapTask\.java:441)
    at org\.apache\.hadoop\.mapred\.MapTask\.run(MapTask\.java:377)
    at org\.apache\.hadoop\.mapred\.Child$4\.run(Child\.java:255)
    at java\.security\.AccessController\.doPrivileged(Native Method)
    at javax\.security\.auth\.Subject\.doAs(Subject\.java:396)
    at org\.apache\.hadoop\.security\.UserGroupInformation\.doAs(UserGroupInformation\.java:1132)
    at org\.apache\.hadoop\.mapred\.Child\.main(Child\.java:249)

reg ex在http://regexpal.com/处罚款

非常感谢

邓肯

最佳答案

您会得到一个异常(exception),因为您的正则表达式产生一个结果,其中两个结果字段除外(即“custid”和“movieids”),因为正则表达式仅包含一个组(...)。

如果只想在冒号处分割,则可以使用带有两个组的表达式,例如:

String filmRegex = "(\\d):(\\d)";

\d+,如果您的数字可以超过一位。

或者,更容易地,使用TextDelimited输入方案从文件中读取数据时,只需将输入数据自动拆分到其字段中即可:
Scheme sourceScheme = new TextDelimited(new Fields("custid", "movieids"), ":");

关于regex - 级联-正则表达式解析器-错​​误的字段数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16738794/

相关文章:

php - 关于如何解析此数据集的任何优雅想法?

Python 正则表达式 : To capture all words within nested parentheses

Javascript:如何剪切字符串的特定部分?

hadoop - 命令 "hadoop namenode -format"将做什么

java - org.hibernate.TransientObjectException : how to properly define one-to-one relationship?

regex - Google 电子表格中的转义元字符 [ ] re2

scala - 连接 hive 和spark时发生异常HDFS上的根暂存目录:/tmp/hive应该是可写的。当前权限是:rwxrwxr-x

json - 使用 Apache Drill 查询压缩的 gz 文件

java - 级联中的简单日期过滤器

java - 如何读取由特殊字符分隔的hadoop中的文本源