Hadoop入门之Hadoop中的HelloWorld程序

在linux平台上执行wordcount,有官方示例,相应的jar包放在hadoop-2.0.0-cdh4.5.0\share\hadoop\mapreduce1下的hadoop-examples-2.0.0-mr1-cdh4.5.0.jar(注:本人用的是CDH4.5.0版本),我们首先需要准备好数据:

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


然后把数据put到HDFS里:

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

再然后进入到jar所在的目录里“

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

执行命令:

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

其中,/output是执行结果输出目录。

到此,HelloWorld就顺利执行了,你可以用hadoop fs -cat  /output/part 命令来查看结果.

接下来,我们在看看在window上的eclipse如何执行。

首先贴出代码:

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");//这句话很重要,要不然会告你没有权限执行
        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上点执行即可,在执行时可能发现jvm内存不够,添加-Xmx1024M参数执行即可。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值