MR程序部署

一、打包

mapreduce程序打包有多种方式,使用fatjar的方式最简单方便。

在pom文件插件中加入生成fatjar的插件:

<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <archive>
        </archive>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- 指定在打包节点执行jar包合并操作 -->
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

使用mvn package命令打包,即可生成xxx-1.0.0-jar-with-dependencies.jar文件。

二、测试jar运行

上传jar文件到hadoop服务器上,使用hadoop jar命令启动mapreduce。

hadoop jar /home/hadoop/application/bds/xxx-1.0.0-jar-with-dependencies.jar com.whty.bds.hadoop.Main hdfs://192.168.5.174:9000/flume
  • 1

hadoop jar命令的第一个参数为jar包路径,第二个参数为main函数的路径,后面的参数为自定义参数,可以在程序中自定义。 
本例中第三个参数为输入路径(input path)

三、定时执行

在公司的项目中mapreduce需要每天晚上定时执行,采用crontab命令定时启动mapreduce。 
1、shell文件 
因为crontab命令使用自己的环境变量,可以为crontab命令配置全局环境变量或在shell文件中自行引入,建议采用自行引入的方式。 
以下为启动的shell文件:

#!/bin/sh

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export FLUME_HOME=/home/hadoop/application/flume
export PATH=$PATH:$FLUME_HOME/bin
ZOOKEEPER_HOME=/home/hadoop/application/zookeeper
export ZOOKEEPER_HOME
export PATH=$PATH:$ZOOKEEPER_HOME/bin:$ZOOKEEPER_HOME/conf
export HADOOP_HOME=/home/hadoop/application/hadoop
export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin
export HBASE_HOME=/home/hadoop/application/hbase
export PATH=$PATH:$HBASE_HOME/bin
export REDIS_HOME=/application/redis
export PATH=$PATH:$REDIS_HOME/bin
JAVA_HOME=/usr/java/jdk1.7.0_79
export JAVA_HOME
JRE_HOME=/usr/java/jdk1.7.0_79/jre
export JRE_HOME
CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export CLASSPATH
PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH:.
export PATH

/home/hadoop/application/hadoop/bin/hadoop jar /home/hadoop/application/bds/xxx-1.0.0-jar-with-dependencies.jar com.whty.bds.hadoop.Main hdfs://192.168.5.174:9000/flume
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

环境变量配置和linux环境变量(/ect/profile)相同,然后在末尾加入hadoop jar命令,最好采用完整路径 
shell文件编写完成后可以执行./run.sh测试一下。 
如果出现#!/bin/sh无法解析的错误,可以尝试使用dos2unix命令格式化文编码。

2、crontab配置

输入crontab -e 
在vi编辑器中输入定时执行的命令,参考以下格式 
保存退出,输入crontab -l检查是否启动成功

 
 

0 3 * * * /bin/sh /home/hadoop/application/bds/run.sh >/dev/null 2 >&1==============================浅谈mapreduce程序部署======================================

虽然我们在虚拟机客户端上能很快通过shell命令,进行执行一些已经封装好实例程序,但是在应用中还是是自己写程序,然后部署到服务器中去,下面,我通过程序进行浅谈一个程序的部署过程。

在启动Hadoop之后,然后把程序达成可执行的jar包,并把相应的第三方jar包 包含进去。执行hadoop    jar   XXX. +驱动名称。

[java]  view plain  copy
  1. package com.mapred;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.PrintStream;  
  5. import java.util.StringTokenizer;  
  6. import org.apache.hadoop.conf.Configuration;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.IntWritable;  
  9. import org.apache.hadoop.io.Text;  
  10. import org.apache.hadoop.mapreduce.Job;  
  11. import org.apache.hadoop.mapreduce.Mapper;  
  12. import org.apache.hadoop.mapreduce.Mapper.Context;  
  13. import org.apache.hadoop.mapreduce.Reducer;  
  14. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  16. import org.apache.hadoop.util.GenericOptionsParser;  
  17.   
  18. public class WordCount  
  19. {  
  20.   public static void main(String[] args)  
  21.     throws Exception  
  22.   {  
  23.     Configuration conf = new Configuration();  
  24.   /*  String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs(); 
  25.     if (otherArgs.length != 2) { 
  26.       System.err.println("Usage: wordcount <in> <out>"); 
  27.       System.exit(2); 
  28.     }*/  
  29.     Job job = new Job(conf, "word count");  
  30.     job.setJarByClass(WordCount.class);  
  31.     FileInputFormat.addInputPath(job, new Path("hdfs://ubuntu:9000/Input"));  
  32.     job.setMapperClass(TokenizerMapper.class);  
  33.     job.setCombinerClass(IntSumReducer.class);  
  34.     job.setReducerClass(IntSumReducer.class);  
  35.     job.setOutputKeyClass(Text.class);  
  36.     job.setOutputValueClass(IntWritable.class);  
  37.     job.setMapOutputKeyClass(Text.class);  
  38.     job.setMapOutputValueClass(IntWritable.class);  
  39.      
  40.     FileOutputFormat.setOutputPath(job, new Path("hdfs://ubuntu:9000/output09"));  
  41.     job.waitForCompletion(true);  
  42.   }  
  43.   
  44.   public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable>  
  45.   {  
  46.     private IntWritable result;  
  47.   
  48.     public IntSumReducer()  
  49.     {  
  50.       this.result = new IntWritable();  
  51.     }  
  52.   
  53.     public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException  
  54.     {  
  55.       int sum = 0;  
  56.       for (IntWritable val : values) {  
  57.         sum += val.get();  
  58.       }  
  59.       this.result.set(sum);  
  60.       context.write(key, this.result);  
  61.     }  
  62.   }  
  63.   
  64.   public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable>  
  65.   {  
  66.     private static final IntWritable one = new IntWritable(1);  
  67.     private Text word;  
  68.   
  69.     public TokenizerMapper()  
  70.     {  
  71.       this.word = new Text();  
  72.     }  
  73.   
  74.     public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {  
  75.       StringTokenizer itr = new StringTokenizer(value.toString());  
  76.       while (itr.hasMoreTokens()) {  
  77.         this.word.set(itr.nextToken());  
  78.         context.write(this.word, one);  
  79.       }  
  80.     }  
  81.   }  
  82. }  
在执行的过程中要注意以下几个事项:

首先要注意的就是,文件在hdfs上的位置是否正确,记住只需要指定文件夹名称即可,里面有多少具体文件,Hadoop都一并给你处理,注意观察在执行过程中所出现的异常。

因为我在执行和调试过程中也出现很多异常,我认为这些异常是情况很多的,希望有兴趣的同学和我一起交流,共同分析和研究它。

1:注意观察虚拟机终端中报的错误,根据错误进行相应改进,因为关联jar较多,所以当提示你少相应的某一个包时,你要注意引进过来。

2:这里我是部署到虚拟机中执行的,不过在网上看过很多资料说,通过Eclipse也可以直接进行数据的处理,但是我没有调试成功,希望大家谁成功了,告知我一声。我感觉我是版本和虚拟机可能没有绑定好。

3:用Java命令(Java -jar   XXX.jar)也可以执行。而且在这种情况下不需要安装和部署Hadoop环境。但是因为我的Java虚拟机在运行时,老是提示内存不足。没有成功,我还是在Hadoop环境和总成功的。大家可以尝试并交流着去做一下。这个东西,处理数据有点意思。

猜你喜欢

转载自blog.csdn.net/wypersist/article/details/80275835
MR