Saturday, 25 November 2017

Search the word and Get its Position


import java.io.IOException;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.Counters;



  
public class ws
{
 
  public static class wsMapper extends Mapper 
  {
   
   String keyword;
    String found="";
   public void setup(Context context) throws IOException,InterruptedException
   {
    Configuration conf = context.getConfiguration();
    keyword = conf.get("keyword");   
   } 
   public void map(LongWritable key,Text value,Context context) throws     IOException,InterruptedException
   {
    Integer WordPos=0;
 //pos++;
    if(value.toString().contains(keyword))
    {
     WordPos = value.find(keyword);
     found="is Found";
   
    }
    else
    {
     found="is not found";
    }
    context.write(new Text("Pos ="+""+WordPos.toString()),new Text(keyword+" "+found));
   }
  }
   public static class wsReduce extends Reducer 
  {
//       int bjp=0;
 
//     int cong = 0;
       public void reduce(Text key, Text values, Context context) throws     IOException, InterruptedException
        {
      
      
     
      context.write(key,values);
       }
  }
  
  
  public static void main(String args[]) throws Exception
  {
     Path input = new Path(args[0]);
        Path output = new Path(args[1]);

      Configuration conf=new Configuration();
  Job job =new Job(conf);
  job.setJobName("ws");
  job.setJar("ws.jar");
  job.setJarByClass(ws.class);
  job.setOutputKeyClass(Text.class);
  job.setOutputValueClass(Text.class);
  job.setMapperClass(wsMapper.class);
  job.setReducerClass(wsReduce.class);
  job.setInputFormatClass(TextInputFormat.class);
  job.setOutputFormatClass(TextOutputFormat.class);
  job.setNumReduceTasks(1);
  job.getConfiguration().set("keyword",args[2]);
  FileInputFormat.setInputPaths(job,input);
  FileOutputFormat.setOutputPath(job,output);
  
  System.exit(job.waitForCompletion(true)?0:1);

  }
}

Output :


administrator@dev:/usr/local/hadoop/bin$ hadoop fs -cat /mir/part-r-00000
17/09/08 14:43:25 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
Pos =0           MIR is not found
Pos =0           MIR is not found
Pos =0           MIR is not found
Pos =0           MIR is not found
Pos =0           MIR is not found
Pos =0           MIR is not found
Pos =7           MIR is Found

0 comments:

Post a Comment