Hadoop-WordCount

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/shufangreal/article/details/101201993

Hadoop客户端依赖

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>2.7.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.7.2</version>
        </dependency>
    </dependencies>

MapReduce简单程序事例

Mapper

package com.shufang.mapreduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class HdfsMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    Text newKey = new Text();
    IntWritable v = new IntWritable(1);

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        String[] words = value.toString().split(" ");

        for (int i = 0; i < words.length; i++) {

            newKey.set(words[i]);
            context.write(newKey, v);
        }

    }
}

Reducer

package com.shufang.mapreduce;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class HdfsReducer extends Reducer<Text,IntWritable,Text,IntWritable> {
    IntWritable v = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

        int sum = 0;
        for (IntWritable value : values) {
            sum+=value.get();
        }

        v.set(sum);
        context.write(key,v);
    }
}

Driver

package com.shufang.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Hdfs;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.mapred.lib.FilterOutputFormat;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class MyDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        String path = "hdfs:///out1";


        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);

        boolean delete = fs.delete(new Path("/checkpointdir"), true);
        System.out.println(delete);

        JobConf jobconf = new JobConf();

        Job job = Job.getInstance(conf);
        job.setJarByClass(MyDriver.class);
        job.setMapperClass(HdfsMapper.class);
        job.setReducerClass(HdfsReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

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

        /**********************************************************************|
        *在Mr中,可以自定义Partitioner、Inputformat和RecordReader 、outputformat和 |				*RecoredWriter、自定义WritableAndComparable实现序列化                     |
        ***********************************************************************/
        FileInputFormat.setInputPaths(job, new Path("hdfs:///hello.txt"));
        FileOutputFormat.setOutputPath(job, new 	Path("file:///D:/Idea_workspace/hadoop_project/out/out2"));

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

    }
}

猜你喜欢

转载自blog.csdn.net/shufangreal/article/details/101201993