本人一贯的风格是先了解系统的基础部分,然后在深入到高级部分;如果违背这种循序渐进的次序,也超出了本人的接受能力。古人说,学有本末,事有终始,知所先后,则尽道矣。我们还是从基础开始吧(本人上文提到的开发图片服务器还是放到后面吧)

本人在第一篇文章中描述的WordCount单词统计程序是在单机环境运行的,现在我们改造一下,改造成在单机伪分布环境中运行

新建WordCount类,继承Configured,实现Tool接口

public class WordCount extends Configured implements Tool{public static class Map 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());String str=null;while (itr.hasMoreTokens()) {str=itr.nextToken();word.set(str);context.write(word, one);}}}public static class Reduce 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);  }}@Overridepublic int run(String[] args) throws Exception {// TODO Auto-generated method stub
        File jarFile = EJob.createTempJar("bin");EJob.addClasspath("/usr/hadoop/conf");ClassLoader classLoader = EJob.getClassLoader();Thread.currentThread().setContextClassLoader(classLoader);/** 创建一个job,起个名字以便跟踪查看任务执行情况 **/Job job = new Job(getConf());((JobConf) job.getConfiguration()).setJar(jarFile.toString()); /*** 当在hadoop集群上运行作业时,需要把代码打包成一个jar文件(hadoop会在集群分发这个文件),* 通过job的setJarByClass设置一个类,hadoop根据这个类找到所在的jar文件**/job.setJarByClass(WordCount.class);job.setJobName("wordcount");/*** 设置map和reduce函数的输入类型,这里没有代码是因为我们使用默认的TextInputFormat,针对文本文件,按行将文本文件切割成* InputSplits, 并用 LineRecordReader 将 InputSplit 解析成 <key,value&gt:* 对,key 是行在文件中的位置,value 是文件中的一行**//** 设置map和reduce函数的输出键和输出值类型 **/job.setOutputKeyClass(Text.class);job.setOutputValueClass(IntWritable.class);/** 设置要使用的map、combiner、reduce类型 **/job.setMapperClass(Map.class);job.setCombinerClass(Reduce.class);job.setReducerClass(Reduce.class);/** 设置输入和输出路径 **/FileInputFormat.addInputPath(job, new Path(args[0]));FileOutputFormat.setOutputPath(job, new Path(args[1]));/** 提交作业并等待它完成 **///System.exit(job.waitForCompletion(true) ? 0 : 1);return job.waitForCompletion(true) ? 0 : 1;}/*** @param args* @throws Exception */public static void main(String[] args) throws Exception {// TODO Auto-generated method stub//hdfs/localhost:9000String[] arg={"/test/input","/test/output"};int ret=ToolRunner.run(new WordCount(), arg);//int ret2=ToolRunner.run(conf, tool, args);
        System.exit(ret);}}

因为本人是在伪分布环境测试上面的单词统计程序,需要将该类打包成jar文件,本人这里采用程序中生成临时jar文件的方式

 public class EJob {// To declare global fieldprivate static List<URL> classPath = new ArrayList<URL>();// To declare methodpublic 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) {}}}}

最后我们运行上面的WordCount类的main方法,记住先要将待统计的文件上传到HDFS文件系统的/test/input目录里面(可以采用本人上文中的编程方式上传或者在eclipse的UI界面上传)

---------------------------------------------------------------------------

本系列Hadoop1.2.0开发笔记系本人原创

转载请注明出处 博客园 刺猬的温驯

本文链接 http://www.cnblogs.com/chenying99/archive/2013/06/02/3113474.html

转载于:https://www.cnblogs.com/chenying99/archive/2013/06/02/3113474.html

Hadoop1.2.0开发笔记(八)相关推荐

  1. Kinect for Windows SDK v2.0 开发笔记 (十) 高清面部帧(1) FACS 介绍

    转载于:https://blog.csdn.net/dustpg/article/details/38892783 使用SDK: Kinect for Windows SDK v2.0 public ...

  2. JNI开发笔记(八)--Java读取txt文件进行JNI测试

    Java读取txt文件进行JNI测试 引 前言 1. 新建assets文件夹 2. 载入测试文件 3. 建立文件读取方法 4. 在MainActivity中读取文件数据 引 JNI开发笔记(一)–An ...

  3. Cocos2d-x 3.0 开发(八)骨骼动画的动态换肤

    1.   概述 游戏中人物的状态会发生改变,而这种改变通常要通过局部的变化来表现出来.比如获得一件装备后人物形象的改变,或者战斗中武器.防具的损坏等.这些变化的实现就要通过动态换肤来实现.在接下来的这 ...

  4. 实习小白::(转) Cocos2d-x 3.0 开发(八)骨骼动画的动态换肤

    1.   概述 游戏中人物的状态会发生改变,而这种改变通常要通过局部的变化来表现出来.比如获得一件装备后人物形象的改变,或者战斗中武器.防具的损坏等.这些变化的实现就要通过动态换肤来实现.在接下来的这 ...

  5. Extjs4.0 开发笔记-desktop开始菜单动态生成方法

    desktop开始菜单动态生成方法: Desktop.html中,在<scripts>中的Ext.onReady之前添加如下: var mArr = [];//这里是保存显示模块的数组va ...

  6. STM32学习及开发笔记八:采用主从计时器实现精确脉冲输出

    脉冲信号用于设备控制是非常常见的,但在一些情况下,我们希望精确的控制脉冲的数量以实现对运动的精确控制.实现的方式也许有多种多样,但使用计时器来实现此类操作是人们比较容易想到的. 1.原理概述 我们知道 ...

  7. Kinect for Windows SDK v2.0 开发笔记 (十三) 高清面部帧(4) 面部模型构建器

     (转载请注明出处) 使用SDK: Kinect for Windows SDK v2.0 public preview1409 同前面,因为SDK未完成,不附上函数/方法/接口的超链接. 这次让 ...

  8. Kinect for Windows SDK v2.0 开发笔记 (十二) 高清面部帧(3) 面部模型(2D)

     (转载请注明出处) 使用SDK: Kinect for Windows SDK v2.0 public preview1409 同前面,因为SDK未完成,不附上函数/方法/接口的超链接. 是的, ...

  9. Kinect for Windows SDK v2.0 开发笔记 (五)骨骼帧与笑面男

    (转载请注明出处) 使用SDK: Kinect for Windows SDK v2.0 public preview 这次说说这骨骼帧的获取.嗯,Kinect买来就为这个啊.不然其他数据,买其他产品 ...

最新文章

  1. 让智能手机和居家电脑互联互通(WM6 GPRS)
  2. 图解丨卷积神经网络数学原理解析
  3. C#编程(五十三)----------字典DictionaryTKey,TValue
  4. Android 画虚线 DashPathEffect 使用详解
  5. linux内核内存管理的三个阶段分析
  6. VC星号密码查看器源码
  7. Android Bitmap和Canvas学习笔记
  8. JUnit 5 –动态测试
  9. JSP+Servlet+C3P0+Mysql实现的azhuo商城
  10. 面试容易问的 JavaScript 知识点,你知道几个?
  11. mysql实战20 | 幻读是什么,幻读有什么问题?
  12. hdu - 1532 Drainage Ditches (最大流)
  13. 手机IMSI号码编码规则表
  14. Qt学习之资源文件(qrc)的添加以及使用
  15. caffe-openpose结构
  16. error LNK1120: 1 个无法解析的外部命令(详细小问题解释)
  17. DP(最长上升子序列)——腾讯校招题:逛街
  18. 网络架构、云平台和微信公众平台开发接入
  19. [C++11 多线程同步] --- 线程同步概述
  20. 9 本优秀的 R 语言免费电子书

热门文章

  1. VS Code配置Java环境(Java17)
  2. memset()和bzero()的使用和区别
  3. C语言 结构体数组复制
  4. Java将Excel文件、Word文件转为PDF
  5. 用excel计算分布表
  6. 《深入浅出通信原理》参考资料
  7. Linux指令——crontab
  8. sirius java_sirius
  9. jsp有哪些动作作用分别是什么?
  10. 《九日集训》day3打卡