Hadoop中的HelloWorld程序怎么實(shí)現(xiàn)

本篇內(nèi)容介紹了“Hadoop中的HelloWorld程序怎么實(shí)現(xiàn)”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿(mǎn)足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的蘭溪網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

在linux平臺(tái)上執(zhí)行wordcount,有官方示例,相應(yīng)的jar包放在hadoop-2.0.0-cdh5.5.0\share\hadoop\mapreduce1下的hadoop-examples-2.0.0-mr1-cdh5.5.0.jar(注:本人用的是CDH4.5.0版本),我們首先需要準(zhǔn)備好數(shù)據(jù):

echo "Hello World Hello Hadoop" > 1.txt
echo "Hello Hadoop Bye  " >2.txt

然后把數(shù)據(jù)put到HDFS里:

hadoop fs -mkdir /input
hadoop fs -put /root/1.txt /input
hadoop fs -put /root/2.txt /input

再然后進(jìn)入到j(luò)ar所在的目錄里“

cd hadoop-2.0.0-cdh5.5.0\share\hadoop\mapreduce1

執(zhí)行命令:

hadoop jar hadoop-mapreduce-examples-2.0.0-cdh5.5.0.jar WordCount /input /output

其中,/output是執(zhí)行結(jié)果輸出目錄。

到此,HelloWorld就順利執(zhí)行了,你可以用hadoop fs -cat  /output/part 命令來(lái)查看結(jié)果.

接下來(lái),我們?cè)诳纯丛趙indow上的eclipse如何執(zhí)行。

首先貼出代碼:

public class WordCount {
    // mapper
    public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
        private static IntWritable one = new IntWritable(1);

        private Text word = new Text();

        @Override
        public void map(LongWritable key, Text value, Context context) throws IOException,
                InterruptedException {
            String line = value.toString();
            StringTokenizer token = new StringTokenizer(line);
            while (token.hasMoreElements()) {
                word.set(token.nextToken());
                context.write(word, one);
            }

        };
    }

    // reduce
    public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
        protected void reduce(Text key, Iterable<IntWritable> values, Context context)
                throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable value : values) {
                sum += value.get();
            }
            context.write(key, new IntWritable(sum));
        };
    }

    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();
        System.setProperty("HADOOP_USER_NAME", "root");//這句話(huà)很重要,要不然會(huì)告你沒(méi)有權(quán)限執(zhí)行
        Job job = new Job(conf);
        
        String[] ioArgs = new String[] { "hdfs://192.168.1.101:7001/input",
                "hdfs://192.168.1.101:7001/output" };
        String[] otherArgs = new GenericOptionsParser(conf, ioArgs).getRemainingArgs();

        job.setJarByClass(WordCount.class);

        FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));

        job.setMapperClass(Map.class);
        job.setReducerClass(Reduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        System.exit(job.waitForCompletion(true) ? 0 : 1);

    }
}

然后在eclipse上點(diǎn)執(zhí)行即可,在執(zhí)行時(shí)可能發(fā)現(xiàn)jvm內(nèi)存不夠,添加-Xmx1024M參數(shù)執(zhí)行即可。

“Hadoop中的HelloWorld程序怎么實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

當(dāng)前標(biāo)題:Hadoop中的HelloWorld程序怎么實(shí)現(xiàn)
轉(zhuǎn)載源于:http://muchs.cn/article16/gdcidg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、網(wǎng)站策劃、網(wǎng)站導(dǎo)航、企業(yè)網(wǎng)站制作微信小程序、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)