Linux下部署Hadoop开发环境

来源:互联网 发布:免费版网络管理软件 编辑:程序博客网 时间:2024/06/05 17:13

需要提前准备的系统环境及软件有:Eclipse,Hadoop集成包,Java。(推荐从官网下载压缩包进行解压,Linux系统自动安装的功能要考虑国内网络情况)

本文使用的版本: Linux--Ubuntu 17.04

Hadoop--2.8.0

Java--Java8_u131

Eclipse--版本代号Oxygen


步骤:一.安装Java环境

1.1.将java压缩包解压到usr下
tar -xzvf jdk-8u131-linux-x64.tar.gz /usr/

1.2.修改/etc/profile文件,加入(注意目录名称,不同的Java版本的目录名称有一定差别)
export JAVA_HOME=/usr/jdk1.8.0_131
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin

1.3.在/etc/rc.local中加入以下语句,以便每次开机默认配置Java路径

source /etc/profile

注:该语句只能让登录的用户获取Java路径,如果需要使用root用户来进行开发,需要使用sudo语句。


二.Eclipse安装

将Eclipse的安装包解压到用户目录下(如果是root用户,推荐放入opt以供所有用户使用)

tar -xzvf eclipse-oxygen.tar.gz /home/xxx/


三.Hadoop安装及配置

3.1. 将hadoop解压到对应文件夹(本文默认解压到桌面,即/home/xxx/Desktop/)

3.2 下载适用于Eclipse的Hadoop MapReduce插件(原贴自wanghc处fork,感谢lartin对新版本的编译与分享https://github.com/larntin/hadoop2x-eclipse-plugin/tree/v2.8.0/release)

3.3 将该插件放入Eclipse下的plugin目录中

3.4 如果插件运行成功,就可以在Window-->Preferences中找到Hadoop Map/Reduce选项,配置好其路径,指向Haddop的主目录即可(如当前版本中只需要指向至hadoop-2.8.0目录下)

3.5配置Hadoop集群

在状态栏中新增一个窗口Map/Reduce Locations,点击该窗口下的New Hadoop Location(大象图标),主要配置这几项:Location name:名称,自行定义

Master:设置主机的IP及端口号,根据自身实际情况配置。如果是单机运行,则使用localhost即可。

Dfs:设置主机HDFS的IP及端口号,勾选Use M/R Master Host则跟随主机的IP地址。

3.6 成功创建后,在New Project中新建一个Map/Reduce Project,命名为WordCount

3.7 编写WordCount程序(该程序参考自《MapReduce设计模式》一书)

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.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 class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {private final static IntWritable one = new IntWritable(1);private Text word = new Text();@Overrideprotected void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context)throws IOException, InterruptedException {String txt = value.toString().toLowerCase();if (txt==null) {return;}txt = txt.replaceAll("'","").replaceAll("[^a-zA-Z0-9]", " ");StringTokenizer itr = new StringTokenizer(txt);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();@Overrideprotected 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();}result.set(sum);context.write(key, result);}}@SuppressWarnings("deprecation")public static void main(String[] args) throws Exception {Configuration conf = new Configuration();String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();if(otherArgs.length!=2) {System.err.println("Usage: wordcount <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);FileInputFormat.addInputPath(job, new Path(otherArgs[0]));FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));System.exit(job.waitForCompletion(true)? 0:1);}}

3.8 配置运行参数

点击Run-->Run Configuration选项,将运行方式改为Java Application,配置Project Name和Main Class为WordCount,在第二栏Arguments中添加两项。

第一项为input路径,第二项为output的路径。

注:output路径必须不存在,由MapReduce作业创建,所以往后再次运行该作业的时候需要先删除整个output目录。

3.9 添加测试文件并运行

将需要参与WordCount测试的文本文件放入input目录下,点击Run,等待执行结束,就可以在output目录中得到结果了。

 

P.S. Hadoop中,InputFormat默认为TextInputFormat,因此在WordCount的例子中可以按照默认的输入格式来处理文本。如果是处理其它类型的数据,特别是用户自定义类型数据的情况,就可能要修改RecordReader,从而用输入的数据生成map所需的键值对。


阅读全文
0 0