环境:Windows8.1,Eclipse

用Hadoop自带的wordcount示例

hadoop2.7.0

hadoop-eclipse-plugin-2.7.0.jar //Eclipse的插件,需要对应Hadoop当前版本

基本步骤有很多博客已经提及,就不再赘述

1. 将hadoop-eclipse-plugin-2.7.0.jar放入Eclipse的plugins目录,启动Eclipse

2. 配置Eclipse的Hadoop location信息

3. 新建MapReduce Project

4. 将wordcount的代码拷贝进去

/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.examples; 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;
import org.apache.hadoop.util.GenericOptionsParser; public class WordCount { public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{ 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<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable(); 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 void main(String[] args) throws Exception {
Configuration conf = new Configuration();
conf.set("mapred.job.tracker", "192.168.1.150:9001");
conf.set("yarn.resourcemanager.address", "192.168.1.150:8032"); String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length < 2) {
System.err.println("Usage: wordcount <in> [<in>...] <out>");
System.exit(2);
}
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);
for (int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}
FileOutputFormat.setOutputPath(job,
new Path(otherArgs[otherArgs.length - 1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

  

Main方法的头三行代码,需要自己来配置

5. 将部署好的Hadoop集群中的配置文件拷贝至项目中

log4j.properties必须要配置,不然提交任务至集群时,Console无法显示信息,以下是我的配置

log4j.rootLogger=DEBUG, CA

log4j.appender.CA=org.apache.log4j.ConsoleAppender

log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

  

6. 右键点击WordCount.java -> Run as -> Run on Hadoop

错误1:

org.apache.hadoop.util.Shell$ExitCodeException: /bin/bash: line 0: fg: no job control

Hadoop读取Windows和Linux系统变量时的引发的问题,有几种解决方案,嫌麻烦不想重新编译整个Hadoop就在本项目中直接重写来解决

在Hadoop的源代码中找到YARNRunner.java,拷贝至项目中,项目中的Package要和Hadoop源代码中的一样,运行时才会覆盖

修改YARNRunner.java

(1)修改读取Windows系统变量的方式

注释掉的代码是原来的代码

(2)新增一个处理Windows系统变量的方法

  private void replaceEnvironment(Map<String, String> environment) {
String tmpClassPath = environment.get("CLASSPATH");
tmpClassPath=tmpClassPath.replaceAll(";", ":");
tmpClassPath=tmpClassPath.replaceAll("%PWD%", "\\$PWD");
tmpClassPath=tmpClassPath.replaceAll("%HADOOP_MAPRED_HOME%", "\\$HADOOP_MAPRED_HOME");
tmpClassPath= tmpClassPath.replaceAll("\\\\", "/" );
environment.put("CLASSPATH",tmpClassPath);
}

 在此处使用

错误2:

exited with exitCode: 1 due to: Exception from container-launch

Diagnostics: Exception from container-launch.

修改项目中的mapred-site.xml,增加以下内容

<property>
<name>mapreduce.application.classpath</name>
<value>
$HADOOP_CONF_DIR,
$HADOOP_COMMON_HOME/share/hadoop/common/*,
$HADOOP_COMMON_HOME/share/hadoop/common/lib/*,
$HADOOP_HDFS_HOME/share/hadoop/hdfs/*,
$HADOOP_HDFS_HOME/share/hadoop/hdfs/lib/*,
$HADOOP_MAPRED_HOME/share/hadoop/mapreduce/*,
$HADOOP_MAPRED_HOME/share/hadoop/mapreduce/lib/*,
$HADOOP_YARN_HOME/share/hadoop/yarn/*,
$HADOOP_YARN_HOME/share/hadoop/yarn/lib/*
</value>
</property>

ViewCode

有时还会遇到Mapper class not found <init>() 这个错误

有2个问题

1. Mapper和Reduce的实现类如果是在其他类里面,例如包含Main方法的类,则必须为Static

2. Hadoop找不到本项目的Jar包,因为是从Windows上提交远程任务

(1)可以Export后传到Hadoop服务器上

(2)本地Export后

conf.set("mapred.jar", "C:/Users/14699_000/Desktop/0725.jar");

后面JAR的文件路径是我Export的路径

Eclipse提交任务至Hadoop集群遇到的问题的更多相关文章

  1. eclipse 远程链接访问hadoop 集群日志信息没有输出的问题l

    Eclipse插件Run on Hadoop没有用到hadoop集群节点的问题参考来源 http://f.dataguru.cn/thread-250980-1-1.html http://f.dat ...

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

    https://www.codetd.com/article/664330 https://blog.csdn.net/dream_an/article/details/84342770 通过idea ...

  3. 在windows远程提交任务给Hadoop集群(Hadoop 2.6)

    我使用3台Centos虚拟机搭建了一个Hadoop2.6的集群.希望在windows7上面使用IDEA开发mapreduce程序,然后提交的远程的Hadoop集群上执行.经过不懈的google终于搞定 ...

  4. windows下在eclipse上远程连接hadoop集群调试mapreduce错误记录

    第一次跑mapreduce,记录遇到的几个问题,hadoop集群是CDH版本的,但我windows本地的jar包是直接用hadoop2.6.0的版本,并没有特意找CDH版本的 1.Exception ...

  5. Eclipse提交代码到Spark集群上运行

    Spark集群master节点:      192.168.168.200 Eclipse运行windows主机: 192.168.168.100 场景: 为了测试在Eclipse上开发的代码在Spa ...

  6. windows环境:idea或者eclipse指定用户名操作hadoop集群

    方法 在系统的环境变量或java JVM变量添加HADOOP_USER_NAME(具体值视情况而定). 比如:idea里面可以如下添加HADOOP_USER_NAME=hdfs 原理:直接看源码 /h ...

  7. Hadoop集群 -Eclipse开发环境设置

    1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...

  8. 【hadoop】——window下elicpse连接hadoop集群基础超详细版

    1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...

  9. Hadoop集群(第7期)_Eclipse开发环境设置

    1.Hadoop开发环境简介 1.1 Hadoop集群简介 Java版本:jdk-6u31-linux-i586.bin Linux系统:CentOS6.0 Hadoop版本:hadoop-1.0.0 ...

随机推荐

  1. Ansj配置指南!

    =.= 折腾死 ①你想要http://maven.ansj.org/org/ansj/ansj_seg/找一个尽可能高的版本号,比方2.0.7,点进去之后找到相应的jar,比方ansj_seg-2.0 ...

  2. hdu2818行列匹配+排序

    题意:给定一个矩阵,矩阵上有的数字是1,有的是0,给定两种操作,交换某两行或者某两列,问是否能置换出对角线为1的矩阵 题解:能够置换出对角线是1的矩形要求有n个1既不在同一行也不再同一列,即行列匹配, ...

  3. 最简单的历史Hibernate获得短暂的

    其实Hibernate本身就是一个单独的帧,不管它不需要web server或application server支持. 然而,最Hibernate简介已经加入了非常多的非Hibernate事,例: ...

  4. 潜水 java类加载器ClassLoader

    类加载器(class loader)用于装载 Java 类到 Java 虚拟机中.一般来说.Java 虚拟机使用 Java 类的方式例如以下:Java 源程序(.java 文件)在经过 Java 编译 ...

  5. Spring系列

    Spring系列之访问数据库   阅读目录 一.概述 二.JDBC API的最佳实践 三.Spring对ORM的集成 回到顶部 一.概述 Spring的数据访问层是以统一的数据访问异常层体系为核心,结 ...

  6. Ubuntu下hadoop2.4搭建集群(单机模式)

    一  .新建用户和用户组 注明:(这个步骤事实上能够不用的.只是单独使用一个不同的用户好一些) 1.新建用户组 sudo addgroup hadoop 2.新建用户 sudo adduser -in ...

  7. c# Use Properties Instead of Accessible Data Members

    advantage of properties: 1 properties can be used in data binding, public data member can not. 2 dat ...

  8. leetcode先刷_Climbing Stairs

    水的问题. 以为很常见.青蛙跳楼梯.能跳一步可以跳两步,它实际上是一个斐波那契数. 注意.空间O(1). class Solution { public: int climbStairs(int n) ...

  9. 慎重使用MySQL auto_increment

    在使用MySQL中,常常会在表中建立一个自增的ID字段,利用自增ID可以高速建立索引,也是MySQL官方比較推荐的一种方式,可是,这样的方式在大量数据且配置主从时,可能会出现因为自增ID导致同步失败的 ...

  10. c# 通过配置自动附加数据库

    using System; using System.Collections.Generic; using System.Windows.Forms; using System.Data.SqlCli ...