本地idea开发mapreduce程序提交到远程hadoop集群执行

摘要:
您不需要设置idea运行参数wc。运行时的javapackagecom;importorg.apach.hadoop.conf.Configuration;importorg.apach.hadoop.fs。路径importorg.apache.hadoop.io。国际可写;importorg.apache.hadoop.io。文本importorg.apach.hadoop.mapreduce。柜台importorg.apach.hadoop.mapreduce。工作importorg.apach.hadoop.mapreduce。映射器;importorg.apach.hadoop.mapreduce。减速器;importorg.apach.hadoop.mapreduce.lib.input。文件输入格式;importorg.apach.hadoop.mapreduce.lib.output。文件输出格式;importorg.apache.hadoop.util。GenericOptionsParser;importorg.apache.hadoop.util。StringUtils;importjava.io。缓冲读取器;importjava.io。文件读取器;importjava.io。IOException;导入java.net。URI;importjava.util.*;/***@Authorwangxiaolei(王晓蕾)*@自2018年11月22日起*/publicclasswc{publicstatic classTokenizerMapperextendsMapper<Object,Text,Text,IntWritable>{staticumCountersEnum{INPUT_WORDS}privatefinancialstaticIntWritableone=newIntWritatable;privateTextword=newText();privatebooleancase敏感;privateSetpatternsToSkip=newHashSet();privateConfigurationconf;privateBufferedReaderfis;@覆盖publicvoidsetupthrowsIOException,InterruptedException{conf=context.getConfiguration();caseSensitive=conf.getBoolean;如果{URI[]patternsURIs=Job.getInstance.getCacheFiles();对于{PathpatternsPath=newPath;StringpatternsFileName=patternsPath.getName().toString();parseSkipFile;}}privatevoidparseSkipFile{try{fis=newBufferedReader;Stringpattern=null;while((pattern=fis.readLine())!

https://www.codetd.com/article/664330

本地idea开发mapreduce程序提交到远程hadoop集群执行第1张

https://blog.csdn.net/dream_an/article/details/84342770

本地idea开发mapreduce程序提交到远程hadoop集群执行第2张

通过idea开发mapreduce程序并直接run,提交到远程hadoop集群执行mapreduce。

简要流程:本地开发mapreduce程序–>设置yarn 模式 --> 直接本地run–>远程集群执行mapreduce程序;

完整的流程:本地开发mapreduce程序——> 设置yarn模式——>初次编译产生jar文件——>增加 job.setJar("mapreduce/build/libs/mapreduce-0.1.jar");——>直接在Idea中run——>远程集群执行mapreduce程序;

一图说明问题:
本地idea开发mapreduce程序提交到远程hadoop集群执行第3张

源码
build.gradle

plugins {
    id 'java'}


group 'com.ruizhiedu'version '0.1'
sourceCompatibility = 1.8
repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.hadoop', name: 'hadoop-common', version: '3.1.0'compile group: 'org.apache.hadoop', name: 'hadoop-mapreduce-client-core', version: '3.1.0'compile group: 'org.apache.hadoop', name: 'hadoop-mapreduce-client-jobclient', version: '3.1.0'
    testCompile group: 'junit', name: 'junit', version: '4.12'}

java文件

输入、输出已经让我写死了,可以直接run。不需要再运行时候设置idea运行参数

wc.java

package com;

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.Counter;
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 org.apache.hadoop.util.GenericOptionsParser;
import org.apache.hadoop.util.StringUtils;

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.util.*;



/**
 * @author wangxiaolei(王小雷)
 * @since 2018/11/22
 */

public classwc {
    public static classTokenizerMapper
            extends Mapper<Object, Text, Text, IntWritable>{

        static enumCountersEnum { INPUT_WORDS }

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

        privateboolean caseSensitive;
        private Set<String> patternsToSkip = new HashSet<String>();

        privateConfiguration conf;
        privateBufferedReader fis;

        @Override
        public voidsetup(Context context) throws IOException,
                InterruptedException {
            conf =context.getConfiguration();
            caseSensitive = conf.getBoolean("wordcount.case.sensitive", true);
            if (conf.getBoolean("wordcount.skip.patterns", false)) {
                URI[] patternsURIs =Job.getInstance(conf).getCacheFiles();
                for(URI patternsURI : patternsURIs) {
                    Path patternsPath = newPath(patternsURI.getPath());
                    String patternsFileName =patternsPath.getName().toString();
                    parseSkipFile(patternsFileName);
                }
            }
        }

        private voidparseSkipFile(String fileName) {
            try{
                fis = new BufferedReader(newFileReader(fileName));
                String pattern = null;
                while ((pattern = fis.readLine()) != null) {
                    patternsToSkip.add(pattern);
                }
            } catch(IOException ioe) {
                System.err.println("Caught exception while parsing the cached file '"
                        +StringUtils.stringifyException(ioe));
            }
        }

        @Override
        public voidmap(Object key, Text value, Context context
        ) throws IOException, InterruptedException {
            String line = (caseSensitive) ?value.toString() : value.toString().toLowerCase();
            for(String pattern : patternsToSkip) {
                line = line.replaceAll(pattern, "");
            }
            StringTokenizer itr = newStringTokenizer(line);
            while(itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
                Counter counter = context.getCounter(CountersEnum.class.getName(),
                        CountersEnum.INPUT_WORDS.toString());
                counter.increment(1);
            }
        }
    }

    public static classIntSumReducer
            extends Reducer<Text,IntWritable,Text,IntWritable>{
        private IntWritable result = newIntWritable();

        public void reduce(Text key, Iterable<IntWritable>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 voidmain(String[] args) throws Exception {

        Configuration conf = newConfiguration();
        conf.set("yarn.resourcemanager.address", "192.168.56.101:8050");
        conf.set("mapreduce.framework.name", "yarn");
        conf.set("fs.defaultFS", "hdfs://vbusuanzi:9000/"); 
//conf.set("mapred.jar", "mapreduce/build/libs/mapreduce-0.1.jar"); //也可以在这里设置刚刚编译好的jar
        conf.set("mapred.job.tracker", "vbusuanzi:9001");
//conf.set("mapreduce.app-submission.cross-platform", "true");//Windows开发者需要设置跨平台
       args = new String[]{"/tmp/test/LICENSE.txt","/tmp/test/out30"};
        GenericOptionsParser optionParser = newGenericOptionsParser(conf, args);
        String[] remainingArgs =optionParser.getRemainingArgs();


        if ((remainingArgs.length != 2) && (remainingArgs.length != 4)) {
            System.err.println("Usage: wordcount <in> <out> [-skip skipPatternFile]");
            System.exit(2);
        }

        Job job = Job.getInstance(conf,"test");
        job.setJar("mapreduce/build/libs/mapreduce-0.1.jar");
        job.setJarByClass(com.wc.class);


        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        List<String> otherArgs = new ArrayList<String>();
        for (int i=0; i < remainingArgs.length; ++i) {
            if ("-skip".equals(remainingArgs[i])) {
                job.addCacheFile(new Path(remainingArgs[++i]).toUri());
                job.getConfiguration().setBoolean("wordcount.skip.patterns", true);
            } else{
                otherArgs.add(remainingArgs[i]);
            }
        }
        FileInputFormat.addInputPath(job, new Path(otherArgs.get(0)));
        FileOutputFormat.setOutputPath(job, new Path(otherArgs.get(1)));



        job.waitForCompletion(true);

        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

免责声明:文章转载自《本地idea开发mapreduce程序提交到远程hadoop集群执行》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇HTML标签语义化含义及实用技巧并发下的死锁问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

FileUpload控件使用初步

FileUpload控件使用初步: 1.实现文件上传 protected void btnSubmit_click(object sender, EventArgs e) { if (FileUpload1.HasFile == true) { string strErr = ""; //获得上传文件的大小 int filesize = FileUploa...

.NET中RabbitMq的使用

【原文链接:https://www.cnblogs.com/xibei666/p/5931267.html】 .NET中RabbitMQ的使用  概述   MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法。RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统。他遵循Mozilla Public L...

JAVA 实现CLOB转String

CLOB 定义   数据库中的一种保存文件所使用的类型。   Character Large Object   SQL 类型 CLOB 在 JavaTM 编程语言中的映射关系。SQL CLOB 是内置类型,它将字符大对象 (Character Large Object) 存储为数据库表某一行中的一个列值。默认情况下,驱动程序使用 SQL locator(C...

Mongodb学习笔记五(C#操作mongodb)

mongodb c# driver(驱动)介绍 目前基于C#的mongodb驱动有两种,分别是官方驱动(下载地址)和samus驱动(下载地址)。本次我们只演示官方驱动的使用方法。官方驱动文档查看 第一步:引用驱动dll 引用驱动有两种方式:1. 根据上面的下载地址下载对应的版本,然后引用到项目中。2. 在项目的引用上右击->管理NuGet程序包(首先...

UITextView 和 UITextField限制字符数和表情符号

UITextField限制字符数 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{ int pMaxLength = 12; NSInteg...

java 关于JDBC和DAO模式使用

JDBC(全称:Java Data Base Connectivity)是java数据库连接简称 ,提供连接各种数据库的能力 JDBC API主要的功能: 与数据库建立连接 执行SQL语句 处理结果 JDBC关键字的使用: DriverManager:依据数据库的不同,管理JDBC驱动 Connection:负责连接数据库并且担任传送数据库的任务 S...