• 第一个hadoop 程序


    首先检查hadoop是否安装并配置正确
    然后建立WordCount.java文件
    里面保存
    package org.myorg;

    import java.io.IOException;
    import java.util.*;

    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.conf.*;
    import org.apache.hadoop.io.*;
    import org.apache.hadoop.mapred.*;
    import org.apache.hadoop.util.*;

    public class WordCount {

    public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
    private final static IntWritable one = new IntWritable(1);
    private Text word = new Text();

    public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
    String line = value.toString();
    StringTokenizer tokenizer = new StringTokenizer(line);
    while (tokenizer.hasMoreTokens()) {
    word.set(tokenizer.nextToken());
    output.collect(word, one);
    }
    }
    }

    public static class Reduce extends MapReduceBase implements Reducer<Text, IntWritable, Text, IntWritable> {

    public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
    int sum = 0;
    while (values.hasNext()) {
    sum += values.next().get();
    }
    output.collect(key, new IntWritable(sum));
    }
    }

    public static void main(String[] args) throws Exception {
    JobConf conf = new JobConf(WordCount.class);
    conf.setJobName("wordcount");

    conf.setOutputKeyClass(Text.class);
    conf.setOutputValueClass(IntWritable.class);

    conf.setMapperClass(Map.class);
    conf.setCombinerClass(Reduce.class);
    conf.setReducerClass(Reduce.class);

    conf.setInputFormat(TextInputFormat.class);
    conf.setOutputFormat(TextOutputFormat.class);

    FileInputFormat.setInputPaths(conf, new Path(args[0]));
    FileOutputFormat.setOutputPath(conf, new Path(args[1]));

    JobClient.runJob(conf);
    }

    }

    然后编译WordCount.java文件,把它制作成可执行jar包
    javac -d . -classpath /root/hadoop-0.20.1/hadoop-0.20.1-core.jar WordCount.java
    然后在org的同级目录上建立manifest.mf
    在里面写上Main-Class: org.myorg.WordCount
    然后保存并执行jar -cvfm count.jar manifest.mf org/
    然后在hdfs上建立一个文件夹,hadoop fs -mkdir /test
    hadoop fs -put /root/wordtestnum.txt /test
    然后执行hadoop jar /root/Desktop/count.jar /test/in /test/out
    查看运行结果hadoop fs -cat /test/out/part-00000

    运城互联网论坛地址:http://www.dmyc8.com/forum-104-1.html

  • 相关阅读:
    php或JS中输出判断项
    拿大神的博客来记一下
    2017.6.8 项目进展
    2017.6.8
    2017.5.18
    2017.5.17
    2017.5.16
    如何实现从php传数据到js
    项目笔记
    tp框架之Model类与命名空间
  • 原文地址:https://www.cnblogs.com/wangyayun/p/4514402.html
Copyright © 2020-2023  润新知