溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Hadoop中MapReduce獲取命令行參數(shù)的方法

發(fā)布時間:2021-07-26 18:04:38 來源:億速云 閱讀:308 作者:chen 欄目:云計算

本篇內容介紹了“Hadoop中MapReduce獲取命令行參數(shù)的方法”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!

package cmd;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import mapreduce.MyMapper;
import mapreduce.MyReducer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
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;
import org.apache.hadoop.mapreduce.lib.partition.HashPartitioner;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * 計算單詞
 * @author Xr
 *
 */
public class WordCountApp  extends Configured implements Tool{
    public static String INPUT_PATH = "";
    public static String OUTPUT_PATH = "";
    @Override
    public int run(String[] args) throws Exception {
        INPUT_PATH = args[0];
        OUTPUT_PATH = args[1];
        Configuration conf = new Configuration();
        
        //判處是否存在輸入目錄
        existsFile(conf);
        Job job = new Job(conf,WordCountApp.class.getName());
        //打成jar包
        job.setJarByClass(WordCountApp.class);
        //1.1    從哪里讀取數(shù)據(jù)
        FileInputFormat.setInputPaths(job, INPUT_PATH);
        //把輸入文本中的每一行解析成一個個鍵值對
        job.setInputFormatClass(TextInputFormat.class);
        
        //1.2    設置自定義map函數(shù)
        job.setMapperClass(MyMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        
        //1.3    分區(qū)
        job.setPartitionerClass(HashPartitioner.class);
        job.setNumReduceTasks(1);
        
        //1.4    TODO    排序分組
        //1.5    TODO    規(guī)約
        
        //2.1    是框架做的,不需要程序員手工干預。
        //2.2    自定義reducer函數(shù)
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        
        //2.3    寫入到HDFS中
        FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));
        //格式化類
        job.setOutputFormatClass(TextOutputFormat.class);
        
        //提交給JobTracker去執(zhí)行
        job.waitForCompletion(true);
        return 0;
    }
    public static void main(String[] args)throws Exception {
        ToolRunner.run(new WordCountApp(), args);
    }
    private static void existsFile(Configuration conf) throws IOException,
            URISyntaxException {
        FileSystem fs = FileSystem.get(new URI(INPUT_PATH), conf);
        if(fs.exists(new Path(OUTPUT_PATH))){
            fs.delete(new Path(OUTPUT_PATH), true);
        }
    }
}
運行:hadoop jar WordCount.jar hdfs://hadoop:9000/hello  hdfs://hadoop:9000/h2


                                                                     Name : Xr
                                                                     Date : 2014-03-02 21:47

“Hadoop中MapReduce獲取命令行參數(shù)的方法”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關的知識可以關注億速云網(wǎng)站,小編將為大家輸出更多高質量的實用文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內容。

AI