001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.mapred.lib;
020    
021    import java.io.IOException;
022    import java.util.regex.Matcher;
023    import java.util.regex.Pattern;
024    
025    import org.apache.hadoop.classification.InterfaceAudience;
026    import org.apache.hadoop.classification.InterfaceStability;
027    import org.apache.hadoop.io.LongWritable;
028    import org.apache.hadoop.io.Text;
029    import org.apache.hadoop.mapred.JobConf;
030    import org.apache.hadoop.mapred.MapReduceBase;
031    import org.apache.hadoop.mapred.Mapper;
032    import org.apache.hadoop.mapred.OutputCollector;
033    import org.apache.hadoop.mapred.Reporter;
034    
035    
036    /** A {@link Mapper} that extracts text matching a regular expression.
037     * @deprecated Use {@link org.apache.hadoop.mapreduce.lib.map.RegexMapper}
038     */
039    @Deprecated
040    @InterfaceAudience.Public
041    @InterfaceStability.Stable
042    public class RegexMapper<K> extends MapReduceBase
043        implements Mapper<K, Text, Text, LongWritable> {
044    
045      private Pattern pattern;
046      private int group;
047    
048      public void configure(JobConf job) {
049        pattern = Pattern.compile(job.get(org.apache.hadoop.mapreduce.lib.map.
050                    RegexMapper.PATTERN));
051        group = job.getInt(org.apache.hadoop.mapreduce.lib.map.
052                  RegexMapper.GROUP, 0);
053      }
054    
055      public void map(K key, Text value,
056                      OutputCollector<Text, LongWritable> output,
057                      Reporter reporter)
058        throws IOException {
059        String text = value.toString();
060        Matcher matcher = pattern.matcher(text);
061        while (matcher.find()) {
062          output.collect(new Text(matcher.group(group)), new LongWritable(1));
063        }
064      }
065    
066    }