windows IDEA直接提交MapReduce到集群上执行

云主机

  1. 开放云主机所有端口(可以限制源IP,避免被挖矿)
    在这里插入图片描述
  2. 编写WordCount程序,并配置相应的参数
package www.immoc.hadoop.mapreduce.yarn;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
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;

import java.io.IOException;

public class WordCountOnYarnApp {
    public static void main(String[] args) throws Exception{
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Configuration conf = new Configuration();
        conf.set("yarn.resourcemanager.address", "bigdata:8032");
        conf.set("dfs.client.use.datanode.hostname", "true");
        conf.set("mapreduce.framework.name", "yarn");
        conf.set("fs.defaultFS", "hdfs://bigdata:9000/");
        conf.set("mapreduce.app-submission.cross-platform", "true");
        Job job = Job.getInstance(conf, "wc");

        String inputPath = "hdfs://bigdata:9000/ruozedata/wc/input/wc.data";
//        String inputPath = args[0];
        String outputPath = "hdfs://bigdata:9000/ruozedata/wc/outputyarn1";
//        String outputPath = args[1];

        job.setJar("target/hdfs-train-1.0.jar");
        job.setJarByClass(WordCountOnYarnApp.class);

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

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

        FileInputFormat.setInputPaths(job, new Path(inputPath));
        FileOutputFormat.setOutputPath(job, new Path(outputPath));


        boolean result = job.waitForCompletion(true);
        System.out.println(result ? 0 : 1);
    }

    public static class myMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] datas = value.toString().split(",");

            for (String data : datas) {
                context.write(new Text(data), new IntWritable(1));
            }
        }
    }
    
    public static class myReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int count = 0;

            for (IntWritable value : values) {
                count += value.get();
            }

            context.write(key, new IntWritable(count));
        }
    }
}

在这里插入图片描述
在这里插入图片描述
如果实现不了,先配置本地远行的环境(由于本人是先本地之后才要提交到远程集群中,所以不知道本地运行的环境是有影响)
本地运行环境配置

虚拟机思路同上

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值