`

解决Eclipse中运行WordCount出现 java.lang.ClassNotFoundException: org.apache.hadoop.exam

 
阅读更多

.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

public class EJob {

	// To declare global field
	private static List<URL> classPath = new ArrayList<URL>();

	// To declare method
	public static File createTempJar(String root) throws IOException {
		if (!new File(root).exists()) {
			return null;
		}
		Manifest manifest = new Manifest();
		manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
		final File jarFile = File.createTempFile("EJob-", ".jar", new File(System.getProperty("java.io.tmpdir")));

		Runtime.getRuntime().addShutdownHook(new Thread() {
			public void run() {
				jarFile.delete();
			}
		});

		JarOutputStream out = new JarOutputStream(new FileOutputStream(jarFile), manifest);
		createTempJarInner(out, new File(root), "");
		out.flush();
		out.close();
		return jarFile;
	}

	private static void createTempJarInner(JarOutputStream out, File f,
			String base) throws IOException {
		if (f.isDirectory()) {
			File[] fl = f.listFiles();
			if (base.length() > 0) {
				base = base + "/";
			}
			for (int i = 0; i < fl.length; i++) {
				createTempJarInner(out, fl[i], base + fl[i].getName());
			}
		} else {
			out.putNextEntry(new JarEntry(base));
			FileInputStream in = new FileInputStream(f);
			byte[] buffer = new byte[1024];
			int n = in.read(buffer);
			while (n != -1) {
				out.write(buffer, 0, n);
				n = in.read(buffer);
			}
			in.close();
		}
	}

	public static ClassLoader getClassLoader() {
		ClassLoader parent = Thread.currentThread().getContextClassLoader();
		if (parent == null) {
			parent = EJob.class.getClassLoader();
		}
		if (parent == null) {
			parent = ClassLoader.getSystemClassLoader();
		}
		return new URLClassLoader(classPath.toArray(new URL[0]), parent);
	}

	public static void addClasspath(String component) {
		if ((component != null) && (component.length() > 0)) {
			try {
				File f = new File(component);
				if (f.exists()) {
					URL key = f.getCanonicalFile().toURI().toURL();
					if (!classPath.contains(key)) {
						classPath.add(key);
					}
				}
			} catch (IOException e) {
			}
		}
	}

}

// mian方法中添加:

File jarFile = EJob.createTempJar("bin");
EJob.addClasspath("/usr/hadoop/conf");
ClassLoader classLoader = EJob.getClassLoader();
Thread.currentThread().setContextClassLoader(classLoader);

job.waitForCompletion(true) 提交之前加上下面一条:

((JobConf) job.getConfiguration()).setJar(jarFile.toString());

demo如下:

package mr2demo;

import java.io.File;
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.mapred.JobConf;
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 void main(String[] args) throws Exception {

		File jarFile = EJob.createTempJar("bin");
		EJob.addClasspath("E:\\workspace\\hadoop\\mapreduce\\mr2demo\\conf");
		ClassLoader classLoader = EJob.getClassLoader();
		Thread.currentThread().setContextClassLoader(classLoader);

		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf, args)
				.getRemainingArgs();
		if (otherArgs.length < 2) {
			System.err.println("Usage: wordcount <in> [<in>...] <out>");
			System.exit(2);
		}
		Job job = new Job(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)]));

		((JobConf) job.getConfiguration()).setJar(jarFile.toString());

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

	public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
		private IntWritable result = new IntWritable();

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

	public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
		private static final IntWritable one = new IntWritable(1);
		private Text word = new Text();

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

以上解决了eclipse和插件因为兼容性,导致无法生成jar,windows上无法运行job。

 

分享到:
评论

相关推荐

    使用hadoop实现WordCount实验报告.docx

    3. **运行WordCount程序**:调用Hadoop自带的Java程序`hadoop-mapreduce-examples-2.7.7.jar`,指定输入和输出参数。 ### 四、实验结果 成功运行WordCount后,可以在指定的输出文件夹(例如/output)中看到统计...

    大数据实验报告Hadoop编程实现wordcount单词统计程序附源码.doc

    Wordcount.java 代码实现: ```java import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org....

    Java实现Hadoop下词配对Wordcount计数代码实现

    在"Java实现Hadoop下词配对Wordcount计数代码实现"这个项目中,我们的目标是读取文档,对每一行进行处理,去除标点符号,将所有单词转换为小写,然后统计每个单词出现的次数。以下是一般的步骤: 1. **Mapper阶段**...

    eclipse连接hadoop所需要的hadoop.ddl和eclipse插件和hadoop运行案例

    在IT行业中,Eclipse是一款广泛使用的Java集成开发环境(IDE),而Hadoop是Apache软件基金会开源的大数据处理框架。为了在Eclipse中进行Hadoop相关的开发工作,我们需要配置特定的环境和工具。以下是对"eclipse连接...

    eclipse下运行wordcount

    2. **创建Hadoop项目**:在Eclipse中新建一个Java项目,将"WordCount.java"和"EJob.java"这两个源文件导入到项目中。"WordCount.java"是实现MapReduce逻辑的主类,而"EJob.java"可能是自定义的Job类,用于封装作业...

    hadoop运行wordcount实例

    ### Hadoop运行WordCount实例详解 #### 一、Hadoop简介与WordCount程序的重要性 Hadoop 是一个由Apache基金会所开发的分布式系统基础架构。它能够处理非常庞大的数据集,并且能够在集群上运行,通过将大数据分割...

    Hadoop下Eclipse用Java编程实现WordCount

    ### Hadoop下Eclipse用Java编程实现WordCount #### 一、环境配置与说明 **操作系统**: CentOS 6.5 x64 (安装类型选择软件开发平台) **安装软件**: - hadoop-2.7.1.tar.gz - jdk-7u79-linux-x64.tar.gz / jdk-8u...

    使用命令行编译打包运行自己的MapReduce程序 Hadoop2.6.0

    $ /usr/local/hadoop/bin/hadoop jar WordCount.jar org/apache/hadoop/examples/WordCount input output ``` 注意,这里的命令指定了程序所在的包名,这是因为在代码中设置了包名。如果一切正常,程序将成功运行并...

    eclipse+hadoop+wordCount+sort.docx

    eclipse+hadoop+wordCount+sort 本文档主要介绍了如何在 ...本文档提供了在 Eclipse 中搭建 Hadoop 开发环境、创建 Hadoop 连接、编写 WordCount 和 Sort 程序的详细步骤,可以帮助开发者快速上手 Hadoop 开发。

    eclipse hadoop插件安装 运行WordCount

    在本文中,我们将深入探讨如何在Eclipse IDE中安装Hadoop插件,并通过该插件运行WordCount示例,特别关注Windows环境下的配置。首先,我们需要了解Eclipse Hadoop插件的作用,它允许开发者在Eclipse环境中直接创建、...

    hadoop-2.9.2 win环境运行资源文件:hadoop.dll, winutils.exe

    Hadoop-2.9.2是Hadoop的一个稳定版本,提供了许多增强的功能和优化,使其在Windows环境中也能运行。本篇将深入探讨在Windows 10环境下运行Hadoop-2.9.2所需的两个关键资源文件:`hadoop.dll`和`winutils.exe`。 1. ...

    hadoop demo wordcount

    【Hadoop Demo WordCount】是Hadoop初学者入门的经典示例,它展示了如何利用Hadoop分布式计算框架处理大规模文本数据。这个程序的核心在于统计文本中各个单词出现的次数,是理解MapReduce编程模型的一个基础应用。 ...

    hadoop2.7.1版本 hadoop.dll,winutils.exe

    hadoop2.7.1运行Wordcount错误 at java.lang.ProcessBuilder.start(ProcessBuilder.java:1012) Exception in thread "main" java.lang.UnsatisfiedLinkError

    NativeIO和YARNRunner修改后的源码

    用eclipse本地提交Hadoop任务(如WordCount)到服务器上跑的时候,会报错: Stack trace: ExitCodeException exitCode=1: at org.apache.hadoop.util.Shell.runCommand(Shell.java:538) at org.apache.hadoop.util....

    linux下maven在eclipse安装测试Hadoop.pdf

    在Eclipse中,你可以右键点击项目,选择“Run As” -&gt; “Java Application”,并指定主类为`WordCount`,运行你的程序。 通过以上步骤,你已经在Linux环境下成功地在Eclipse中安装并测试了一个基本的Hadoop项目。...

    基于Windows eclipse maven Hadoop 的WordCount源码

    WordCount是Hadoop的经典示例,用于统计文本中各个单词出现的次数,它是理解MapReduce编程模型的一个良好起点。 首先,我们要搭建开发环境。Windows操作系统虽然不是Hadoop的首选开发平台,但通过一些工具和设置,...

    Hadoop集群-WordCount运行详解.pdf

    1.2运行WordCount程序是MapReduce编程中一个经典的入门示例,用来统计文本中单词出现的频率。1.2.1准备工作包括在本地创建示例文件并上传至Hadoop分布式文件系统(HDFS)。1.2.2运行例子步骤是在集群上运行WordCount...

    windows eclipse 运行wordcount连接linux hadoop2.8 NativeIO YARNRunner 完项目 源码

    在本项目中,我们主要关注的是如何在Windows环境下使用Eclipse IDE运行一个WordCount程序,该程序连接到Linux上的Hadoop 2.8集群,并利用NativeIO和YARNRunner进行分布式处理。以下是对这些关键概念的详细解释: 1....

    hadoop-1.2.1运行WordCount

    - 需要在Hadoop-1.2.1源码包中的`src/examples/org/apache/hadoop/examples/WordCount.java`进行必要的修改。 - 编译Java文件,这一步可能会比较耗时。 2. **替换编译后的类文件**: - 将编译好的`WordCount*....

    hadoop 运行成功代码(wordcount)

    2. `WordCount.java` - 实现WordCount的Java代码,包含Map和Reduce函数。 3. `main` 方法 - 用于运行MapReduce任务的入口点。 4. 测试数据文件 - 用于测试WordCount程序的文本文件。 运行流程如下: 1. 使用Maven...

Global site tag (gtag.js) - Google Analytics