Windows 下运行 Hadoop 并部署到 AWS(qbit)

本文环境

Windows 10
JDK 8
IntelliJ IDEA 2019.3.4(Community Edition)
Hadoop 2.8.5
AWS EMR 5.3.0

单机程序

  • 新建 Maven 工程

New Project

  • 修改 pom.xml 配置


    4.0.0

    mapreducedemo
    mapreducedemo
    1.0-SNAPSHOT

    
        2.8.5
    

    
        
            org.apache.hadoop
            hadoop-client
            ${hadoop.version}
        
    
  • 新建 PackageJava Class

WordCount.java

  • 官方拷贝代码到 WordCount.java
package mapreducedemo;

import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {

    public static class TokenizerMapper
            extends Mapper{

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context
        ) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }

    public static class IntSumReducer
            extends Reducer {
        private IntWritable result = new IntWritable();

        public void reduce(Text key, Iterable values,
                           Context context
        ) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}
  • 将文本文件 demo.txt 放到 pom.xml 同级目录
  • 添加运行配置

Run Configuration

  • 点击运行,报如下错误
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/String;I)Z
  • 官网下载并解压 hadoop-2.8.5.tar.gz

hadoop

  • 下载 winutils/hadoop-2.8.5/,将 bin 目录里面的文件拷贝到 hadoop-2.8.5/bin (复制或替换)
  • 设置环境变量(qbit 为了不跟系统环境变量冲突,直接在 IDEA 里面设置)

environment

  • 重新运行,正常输入结果。

output

部署到 AWS

  • pom.xml 里面添加 build 标签及内容,完整 pom.xml 如下


    4.0.0

    mapreducedemo
    mapreducedemo
    1.0-SNAPSHOT

    
        1.8
        2.8.5
    

    
        
            org.apache.hadoop
            hadoop-client
            ${hadoop.version}
        
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.1
                
                    ${java.version}
                    ${java.version}
                
            
            
                org.apache.maven.plugins
                maven-shade-plugin
                3.2.4
                
                    false
                
                
                    
                        package
                        
                            shade
                        
                        
                            
                                
                                    *:*
                                    
                                        META-INF/*.SF
                                        META-INF/*.DSA
                                        META-INF/*.RSA
                                    
                                
                            
                            
                                
                                
                                    mapreducedemo.WordCount
                                
                            
                            
                                
                                    junit:junit
                                
                            
                        
                    
                
            
        
    
  • 添加运行配置

jar maven

  • 运行并导出 jar
  • demo.txt 拷贝到 target 目录

target

  • cmd 命令行测试是否正常
...\mapreducedemo\target>set HADOOP_HOME=E:\hadoop\hadoop-2.8.5

...\mapreducedemo\target>set PATH=E:\hadoop\hadoop-2.8.5\bin;%PATH%

...\mapreducedemo\target>java -jar mapreducedemo-1.0-SNAPSHOT.jar demo.txt output
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
  • 将文件上传到 S3
s3://zt-hadoop-cn-northwest-1/jar/mapreducedemo-1.0-SNAPSHOT.jar
s3://zt-hadoop-cn-northwest-1/usr/qbit/input/idea.txt
  • 创建 EMR 集群(默认配置)

create cluster

  • 添加步骤(输出目录应不存在)

step detail

  • 等待程序运行

finished

  • S3 查看运行结果 s3://zt-hadoop-cn-northwest-1/usr/qbit/output

result

本文出自 qbit snap

你可能感兴趣的:(mapreduce,wordcount)