2016-09-19 2 views
0

次のコードは、ストリーミングジョブをクラスタに送信するために問題なく動作します。C#でキーとなるファイル名を取得バイナリmapper.exeとreducer.exeを使用してストリーミングmapreduceジョブ

string statusFolderName = @"/tutorials/wordcountstreaming/status"; 

var jobcred = new BasicAuthCredential(); 
jobcred.UserName = "username"; 
jobcred.Password = "pass"; 
jobcred.Server = new Uri("https://something.azurehdinsight.net"); 

// Define the Hadoop streaming MapReduce job 
StreamingMapReduceJobCreateParameters myJobDefinition = new StreamingMapReduceJobCreateParameters() 
{ 
    JobName = "my word counting job", 
    StatusFolder = statusFolderName, 
    Input = "/example/data/gutenberg/davinci.txt", 
    Output = "/tutorials/wordcountstreaming/output", 
    Reducer = "wc.exe", 
    Mapper = "cat.exe" 

}; 

myJobDefinition.Files.Add("/example/apps/wc.exe"); 
myJobDefinition.Files.Add("/example/apps/cat.exe"); 

var jobClient = JobSubmissionClientFactory.Connect(jobcred); 

// Run the MapReduce job 
JobCreationResults mrJobResults = jobClient.CreateStreamingJob(myJobDefinition); 

----------------------マッパー-------------------- -------

namespace wc 
{ 
    class wc 
    { 
     static void Main(string[] args) 
     { 
      string line; 
      var count = 0; 

      if (args.Length > 0) 
      { 
       Console.SetIn(new StreamReader(args[0])); 
      } 

      while ((line = Console.ReadLine()) != null) 
      { 
       count += line.Count(cr => (cr == ' ' || cr == '\n')); 
      } 
      Console.WriteLine(count); 
     } 
    } 
} 

テキストファイルの名前をキーとして取得するにはどうすればよいですか? 出力にキー値を表示します。キーはファイルの名前で、値はファイル内の単語の数です 私は複数のファイルを持っています。

答えて

1
In order to get the name of text file processed by Mapper as key you can use the below command in your mapper function. 

    string Key = Environment.GetEnvironmentVariable("map_input_file"); 
Modify your Mapper code as: 

     namespace wc 
     { 
      class wc 
      { 
       static void Main(string[] args) 
       { 
        string line; 
        var count = 0; 

        if (args.Length > 0) 
        { 
         Console.SetIn(new StreamReader(args[0])); 
        } 

        while ((line = Console.ReadLine()) != null) 
        { 
         count += line.Count(cr => (cr == ' ' || cr == '\n')); 
        } 
        string Key = Environment.GetEnvironmentVariable("map_input_file"); 
        var output = String.Format("{0}\t{1}",Key, count); 
        Console.WriteLine(output); 
       } 
      } 
     } 

希望します。

+0

ありがとうございます。私はこれをテストし、出力を知り、それを受け入れます。 – NAS

関連する問題