hadoop学习记录-1

前面写一篇hadoop学习记录-安装的文章发现其实没有必要这么详细,我其实就是想记录那些本身的看法和学习心得也是为了往后的复习,因此没有必要写的那么详尽。node

slaves.sh uptime | sort:检查hadoop datanode节点启动时间等信息。apache

slaves.sh jps | sort:检查各个datanode节点进程启动状况windows


在windows上开发hadoop程序的过程当中,不少人喜欢在eclipse上安装插件实现远程调试,其实否则,服务器

能够利用ant来实现一样的功能,经过ant将程序打包之后使用scp将jar包传送到hadoop平台运行便可,app

这个过程ant须要引用jsch这个第三方jar包,而后才能在build.xml脚本中使用scp和sshexec:eclipse


<target name="scp" depends="jar" description="将项目jar文件经过ssh拷贝到远程服务器指定目录下">ssh

 <scp file="${product}/${jar}" todir="root:liuweicai@master:~" trust="true" />ide

</target>oop

<target name="sshexec" depends="scp" description="经过ssh远程执行hadoop任务" >学习

<sshexec host="master" username="root" password="liuweicai" command="hadoop jar ${jar} -jt master:9001 /cache /output " trust="true"/>

</target>


这样就能经过ssh协议远程运行hadoop的job了!



在编写hadoop程序的运行驱动程序Driver时,推荐使用继承Configured和实现Tool接口,如如下面参考代码:

package com.ims.hadoop.commentwordcount;


import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.conf.Configured;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IntWritable;

import org.apache.hadoop.io.Text;

import org.apache.hadoop.io.compress.CompressionCodec;

import org.apache.hadoop.io.compress.GzipCodec;

import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.util.Tool;

import org.apache.hadoop.util.ToolRunner;


public class CommentWordCountDriver extends Configured implements Tool {


   public static void main(String[] args) throws Exception{

       int res = ToolRunner.run(new CommentWordCountDriver(), args);

       System.exit(res);

   }

   @Override

   public int run(String[] args) throws Exception {

       Configuration conf = getConf();

       if(args.length != 2){

           System.err.printf("Usage: %s [generic options] <input> <output>\n",getClass().getSimpleName());

           ToolRunner.printGenericCommandUsage(System.err);

           return -1;

       }

       Path in = new Path(args[0]);

       Path out = new Path(args[1]);

       FileSystem fs = FileSystem.get(conf);

       if(fs.exists(out))

           fs.delete(out, true);

       conf.setBoolean("mapred.output.compress", true);

       conf.setClass("mapred.output.compression.codec", GzipCodec.class,CompressionCodec.class);

       Job job = new Job(conf,"StackOverFlow Comment Word Count");

       job.setJarByClass(CommentWordCount.class);

       job.setMapperClass(WordCountMapper.class);

       job.setCombinerClass(IntSumReducer.class);

       job.setReducerClass(IntSumReducer.class);

       job.setOutputKeyClass(Text.class);

       job.setOutputValueClass(IntWritable.class);

       FileInputFormat.addInputPath(job, in);

       FileOutputFormat.setOutputPath(job, out);

       return (job.waitForCompletion(true) ? 0 : 1);

   }

}

相关文章
相关标签/搜索