2017-02-23 5 views
0

: 入力データ:出力一緒に一つのキーの値を減らす

text files 

出力:

term: fileName occurrences 

地図出力:

Term:filename 1 1 1 1 1 

出力を減らす:

Term: filename occurences 

コード最終的な出力「減速出力」の例:私はこのケースを取得することができ、私は私を入れて、パーティショニング機能を使用して考えた方法

Iphone: file1 4 
Iphone: file2 3 
Galaxy: file1 2 
Htc: file1 3 
Htc file2 5 

私が欲しいの出力

Iphone: file1=4 file2=3 
Galaxy: file1=2 
Htc: file1=3 file2=5 

それをする方法を知らない?なにか提案を? ありがとうございます

+0

コード出力の例 - マッパーの出力は? –

+0

@siddharthajain no還元剤からの出力「最終出力」 – user5532529

+0

マップ出力のキーとそのフォーマットは何ですか? –

答えて

0

出力を実現するにはさまざまな方法がありますが、パーティショナーとのやり取りについて言及しているので、これでやりましょう。

あなたの質問によると、「Term」(iphone、Galaxyなど)の出力を分割したいキーベースのパーティションを作成する必要があります。マップ出力のキー形式と値の形式それに応じて変更されなければテキストです。これは、あなたのパーティが

public class Partitioners extends org.apache.hadoop.mapreduce.Partitioner<Text,Text>{ 
// I have the written the code if there are 3 reducer(since you have 3 type of key). 
//Tip: your number of reducers should be equal to the no of batches you want to divide your map output into. 
    @Override 
    public int getPartition(Text key, Text value, int numReduceTasks) { 
       String Skey = key.toString(); 
     //Again make changes according to your requirement here but I think it will work according to the composite key you have mentioned 
     String term = Skey.substring(0, Skey.indexOf(':')); 
     if(term.equals("Iphone")) 
     { // this will send all the key having iphone in reducer 1 
      return 0; 
     }else if(term.equals("Galaxy")) 
     { // this will send all the key having Galaxy in reducer 2 
      return 1; 
     } 
     else{ 
      // this will send all the key having other then Iphone and galaxy which is Htc in your case in reducer 3 
      return 2; 
     } 
    } 
} 

どのように見えるかでパーティが行われた後、今、私たちはこれについて、当社のドライバクラスを通知する必要がありますので、あなたのドライバのクラスに

job.setPartitionerClass(Partitioners.class); 
job.setNumReduceTasks(3); //since we want 3 reducers 

を以下を追加しますこれはであなたのマップの出力を分割します3パーティショナーを使用すると、減速機クラスで出力を減らすことができます。

これがあなたの問題を解決することを願っています。もし私に知らせてくれないなら。

+0

あなたのお返事いただきありがとうございます、非常に非常にありがとうございます。私のケースでは、私は多くのファイルを持っていて、これらのファイルの中には多くの用語がありますので、パーティション化のアイデアは私のケースに合わないと思います。上記のメソッドを試してみますが、私が追加するパーティションクラスでは "java.lang.RuntimeException:java.lang.NoSuchMethodException"という例外があります。この出力を得る別の方法を教えてください。私はStringBuilderを書こうとし、その値を追加しますが、キーがコンポジットなので、私が望む出力が得られません。 – user5532529

+0

エラーログの詳細を教えてください。できますか、構造を変更することができます。の。コンポジットキー –

+0

私はpartitioinerにあるすべての用語を扱うことができないので、私はパーティション化機能なしでそれを行うことができると言うことです。これはエラーです:java.lang.RuntimeException:java.lang.NoSuchMethodException:org.apache.hadoop.h.Driver $ Partitioners。() \t at org.apache.hadoop.util.ReflectionUtils.newInstance(ReflectionUtils.java:115) \t at org.apache.hadoop.mapred.MapTask $ NewOutputCollector。 org.apache.hadoop.mapred.MapTask.run(MapTask.java:305)で org.apache.hadoop.mapred.MapTask.runNewMapperで(MapTask.java:527) \t(MapTask.java:613) \t – user5532529

関連する問題