用Eclipse提交hadoop 程序提交时总是发现有些类不存在,只好用EJob这个类辅助成功。 这个类主要用于把hadoop程序打包,并从本机发到集群 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().toURL(); if (!classPath.contains(key)) { classPath.add(key); } } } catch (IOException e) { } } } }
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; 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 { File jarFile = EJob.createTempJar("bin"); ClassLoader classLoader = EJob.getClassLoader(); Thread.currentThread().setContextClassLoader(classLoader); Configuration conf = new Configuration(); conf.set("mapred.job.tracker", "bfdbjc1:12001"); 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); FileInputFormat.addInputPath(job, new Path("hdfs://bfdbjc1:12000/user/work/a.txt")); //本地提交至集群的时候得有写权限 FileOutputFormat.setOutputPath(job, new Path("hdfs://bfdbjc1:12000/user/work/output/20130528_12")); //Eclipse 本地提交 ((JobConf) job.getConfiguration()).setJar(jarFile.toString()); System.exit(job.waitForCompletion(true) ? 0 : 1); } }
相关推荐
Hadoop_Hadoop集群(第7期)_Eclipse开发环境设置 Hadoop_Hadoop集群(第8期)_HDFS初探之旅 Hadoop_Hadoop集群(第9期)_MapReduce初级案例 Hadoop_Hadoop集群(第10期)_MySQL关系数据库 Web(Json-Lib类库使用...
hadoop伪集群搭建及eclipse插件配置,主要介绍伪集群下配置文件的配置及eclipse插件配置。
Map-Reduce原理体系架构和工作机制,eclipse与Hadoop集群连接
windows下 eclipse操作hadoop集群 插件
Win10的eclipse连接CentOS的Hadoop集群 首先你得现在Windows上安装好eclipse 1.下载eclipse连接hadoop的插件 ,去下载符合你hadoop版本的插件,然后放入eclipse安装目录下的plugins目录下
第6讲:eclipse与Hadoop集群连接
Eclipse链接Hadoop集群配置,图文说明
本人亲手操作搭建Hadoop集群成功,并通过Eclipse进行MapReduce程序的开发,步骤详细完整,在相关过程中配有完整代码和解释,全程无误,只需复制粘贴即可,小白新手按步骤一步一步来也能搭建Hadoop集群成功并进行...
本文档是将eclipse与服务器端Hadoop连接说明文档,几经验证能够使用,希望能够帮到大家。
pc机连接hadoop集群必须的文件,把它放到本地,然后配置到环境变量中,才能在本地操作集群。
Hadoop集群·Eclipse开发环境设置(第7期) Hadoop集群·HDFS初探之旅(第8期) Hadoop集群·MapReduce初级案例(第9期) Hadoop集群·MySQL关系数据库(第10期) Hadoop集群·常用MySQL数据库命令(第10期副刊) ...
NULL 博文链接:https://qindongliang.iteye.com/blog/2078452
在Windows7 x64 + Eclipse + Hadoop 2.5.2搭建MapReduce开发环境,下载的文件中包括下载的文件包括:hadoop 2.5.2.tar.gz,hadoop-common-2.2.0-bin-master.zip,hadoop-eclipse-plugin-2.5.2.jar。应用这些软件的...
8.细细品味Hadoop_Hadoop集群(第7期)_Eclipse开发环境设置 9.细细品味Hadoop_Hadoop集群(第8期)_HDFS初探之旅 10.细细品味Hadoop_Hadoop集群(第9期)_MapReduce初级案例 ........................
此插件是本人亲自编译好的可以让Eclipse上实现hadoop的mapreduce编程,目标集群运行了hadoop2.4.0,集群系统CENTOS6.5,jdk1.8.20,Eclipse运行在win7系统中
Windows环境下采用eclipse连接虚拟机中的Hadoop伪分布式集群-附件资源
eclipse连接远程hadoop集群开发时权限不足问题解决方案 (2).pdfeclipse连接远程hadoop集群开发时权限不足问题解决方案 (2).pdf
hadoop集群eclipse安装配置共30页.pdf.zip