2015-04-05 67 views
-2
import java.io.IOException; 
import org.apache.hadoop.io.LongWritable; 
import org.apache.hadoop.io.Text; 
import org.apache.hadoop.mapred.MapReduceBase; 
import org.apache.hadoop.mapred.Mapper; 
import org.apache.hadoop.mapred.OutputCollector; 
import org.apache.hadoop.mapred.Reporter; 


public class ADDMapper extends MapReduceBase implements Mapper<LongWritable, 
           Text,Text,LongWritable> 
{ @Override 
public void map(LongWritable key, Text value,OutputCollector<Text, LongWritable> output, Reporter r)throws IOException 
    { 
    String s=value.toString(); 
     char[] words=s.toCharArray(); 
        int wno=0; 
        int ino=0; 
     for(int i=0;i<words.length;i++) 
      {  

      String temp=""; 
       for(int j=ino;j<words.length;j++) 
        {       

         if(words[j]!=' ') 
         { temp+=words[j]; 
         } 
         else 
         { 
          wno=j; 
         if(temp!="") 
         {  

          ino=ino + key; //////POINT OF ERROR 

     output.collect(new Text(temp),new LongWritable(ino)); 
         } 

        temp=""; 

         ino=wno+1; 
         break; 
         } 

        } 
     } 
} 

}的Hadoop的map-reduce映射編程

我想每個字符串的索引值,通過串排序。
上面的代碼既沒有給出索引值,也沒有對字符串進行混洗。 讓 輸入文件: 嗨,你好嗎 嗨,我是對的。 你的工作情況如何。 你好嗎。

輸出: 上午50 是7.33 喜0,30,44 如何3,14 。 。

+2

你能(a)正確地格式化你的代碼,並且(b)請不要在CAPS中提出問題嗎?另外,請閱讀[我如何問一個好問題](http://stackoverflow.com/help/how-to-ask)瞭解一些更進一步的技巧。你的問題就這樣,將會收到很少的答案。 – 2015-04-05 09:07:10

回答

1

請運行下面的代碼,它運行良好,並提供您的預期輸出。

提供輸入和輸出路徑在命令行參數。(參數[0],ARGS [1])

import java.io.IOException; 
import java.util.*; 
import java.util.Map.Entry; 
import org.apache.hadoop.fs.Path; 
import org.apache.hadoop.io.*; 
import org.apache.hadoop.mapred.*; 


    public class IndexCount { 

     public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> { 
     public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { 

      String str=value.toString(); 
      String[] tokens = str.split(" "); //split into words 
      //create hashmap for unique word 
      HashMap<String,Integer> uniqueString = new HashMap<String,Integer>(); 
      for(int i=0;i<tokens.length;i++){ 
       uniqueString.put(tokens[i],1); 
      }  
      //for sorting create TreeMap from above hash map 
      TreeMap<String, Integer> map = new TreeMap<String,Integer>(uniqueString); 
      for (Entry<String, Integer> entry : map.entrySet()) { 
       int index=0; 
      //find the index of the word 
       index = str.indexOf((String)entry.getKey()); 
       while (index >= 0) { 
         output.collect(new Text((String)entry.getKey()),new IntWritable(index)); 
         index = str.indexOf((String)entry.getKey(), index + 1); 
       } 
      } 
     } 
    } 
     public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> { 
     public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException { 

      while (values.hasNext()) { 
       output.collect(key, new IntWritable(values.next().get())); 
      } 

     } 
    } 
     public static void main(String[] args) throws Exception { 
     JobConf conf = new JobConf(WordCount.class); 
     conf.setJobName("indexfinder"); 

     conf.setOutputKeyClass(Text.class); 
     conf.setOutputValueClass(IntWritable.class); 
     conf.setMapperClass(Map.class); 
     conf.setCombinerClass(Reduce.class); 
     conf.setReducerClass(Reduce.class);  
     conf.setInputFormat(TextInputFormat.class); 
     conf.setOutputFormat(TextOutputFormat.class); 

     FileInputFormat.setInputPaths(conf, new Path(args[0])); 
     FileOutputFormat.setOutputPath(conf, new Path(args[1])); 

     JobClient.runJob(conf); 
     } 
    } 
+0

他是哈利古普塔 這是古普塔哈里 嗨哈里你好嗎 嗨古普塔你好嗎 看起來不錯Gupta如果有一個換行符然後代碼再次從'0'開始,但是索引值不應該再次從0(零)開始重複......它應該以連續的形式取下一個索引值...這個碼給hi 0,他的0,這個0, hi 0,is 2,is 1,is 4,is 5 但是每一行都應該以其pr當前字符索引值加當前字符索引。 – 2015-04-05 19:07:13

+0

嗨,請舉個小例子。並寫下它的預期輸出。所以我可以很容易理解。以兩到三行爲例。 – 2015-04-05 19:18:09

+0

輸入文件「:::你好,你是怎樣工作的............假設這是一個雙行文本文件,所以輸出這個文件應該是:::: 7,hi 0,hi 15,how 3,how18,...每一行應該以「key」加上「index」值開始(下一行不應該以「0」索引開始,它應該繼續索引第一行意味着第二行應該繼續索引值15 – 2015-04-06 04:07:37

1

嗨Shivendra我寫了下面的映射器的邏輯,這將幫助你找到排序輸出每個字符串的索引。 這段代碼的輸出是用索引對String排序的,然後你可以在這個輸出上運行reducer。

String str=value.toString(); 
String[] tokens = str.split(" "); //split into words 
//create hashmap for unique word 
Map<String,Integer> uniqueString = new HashMap<String,Integer>(); 
for(int i=0;i<tokens.length;i++){ 
    uniqueString.put(tokens[i],1); 
}  
//for sorting create TreeMap from above hash map 
Map<String,Integer> map = new TreeMap<String,Integer>(uniqueString); 
for (Map.Entry entry : map.entrySet()) { 
    int index=0; 
//find the index of the word 
    index = str.indexOf((String)entry.getKey()); 
    while (index >= 0) { 
      output.collect(new Text((String)entry.getKey()),new LongWritable(index)); 
      index = str.indexOf((String)entry.getKey(), index + 1); 
    } 
} 

輸出這個邏輯: AM:20, 是:7, 是:50, 喜:0, 喜:15, 喜:47, 如何:3, 如何: 30, I:1, I:16, I:18, I:24, I:34, I:48, 是:34, job.:42, ok.:58, right:23, you:11, 你:37, 你:54, 你的:37

它可能會幫助你。

+0

其實我是新的地圖縮減器,所以我在寫作ap減少代碼和運行它們時有點難度... – 2015-04-05 16:21:30

+0

把上面的代碼放在mapper中,你只需要創建空的縮減器。減速機中沒有任何代碼。你會得到預期的輸出。祝你一切順利 – 2015-04-05 16:30:03

+0

嘿Chandu,這在一定程度上可以幫助我,但是我是map-reduce編程的新手,並且在我的完整代碼中實現這段代碼並且遇到一些錯誤時遇到了一些困難。您可以讓我知道什麼是必需的庫/頭文件或任何.dll我需要導入或編寫的映射代碼。我正在運行這個代碼沒有reducer。 – 2015-04-05 16:43:49

1

請運行下面的代碼,它的給定期望的輸出。

import java.io.IOException; 
    import java.util.*; 
    import java.util.Map.Entry; 

    import org.apache.hadoop.fs.Path; 
    import org.apache.hadoop.conf.*; 
    import org.apache.hadoop.io.*; 
    import org.apache.hadoop.mapreduce.*; 
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 
    import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; 
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 
    import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; 

    public class Index { 

     public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> { 


     public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { 
      String str=value.toString(); 
       String[] tokens = str.split(" "); //split into words 
       //create hashmap for unique word 
       HashMap<String,Integer> uniqueString = new HashMap<String,Integer>(); 
       for(int i=0;i<tokens.length;i++){ 
        uniqueString.put(tokens[i],1); 
       }  
       //for sorting create TreeMap from above hash map 
       TreeMap<String, Integer> map = new TreeMap<String,Integer>(uniqueString); 
       Configuration conf=context.getConfiguration(); 
       int strIndex = 0; 
       for (Entry<String, Integer> entry : map.entrySet()) { 
        //int index=0; 
        strIndex=conf.getInt("index", 0); 
       //find the index of the word 
        int index = str.indexOf((String)entry.getKey()); 
        while (index >= 0) { 
          index+=strIndex; 
          context.write(new Text((String)entry.getKey()),new IntWritable(index)); 
          index = str.indexOf((String)entry.getKey(), index + 1); 
        } 
       } 
       conf.setInt("index", strIndex+str.length()); 
      } 
     } 

    public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> { 

    public void reduce(Text key, Iterable<IntWritable> values, Context context) 
     throws IOException, InterruptedException { 

     for (IntWritable val : values) { 
      context.write(key, new IntWritable(val.get())); 
     } 
    } 
    } 

    public static void main(String[] args) throws Exception { 
    Configuration conf = new Configuration(); 

     conf.setInt("index", 0); 
     Job job = new Job(conf, "index"); 
    job.setOutputKeyClass(Text.class); 
    job.setOutputValueClass(IntWritable.class); 

    job.setMapperClass(Map.class); 
    job.setReducerClass(Reduce.class); 

    job.setInputFormatClass(TextInputFormat.class); 
    job.setOutputFormatClass(TextOutputFormat.class); 

    FileInputFormat.addInputPath(job, new Path("input")); 
    FileOutputFormat.setOutputPath(job, new Path("output")); 

    job.waitForCompletion(true); 
    } 

} 
+0

在map-reduce編程中我們可以編寫一些用戶定義的類......如果我們想使用鏈表數據structer..can我們可以這樣做嗎? – 2015-04-06 10:55:06

+0

yes ,我們可以做到這一點,並請將我的答案作爲正確的答案,這將有助於我很好地建立自己的個人檔案 – 2015-04-06 12:49:29

+0

您是否願意發佈任何數據結構示例..其中我們創建了用戶定義的函數和類 – 2015-04-06 14:41:31