Hadoop 在Ubuntu下的单机配置及运行示例

xiaoxiao2021-02-28  25

Hadoop是现在广泛使用分布式系统基础架构,由Apache基金会所开发,从2006年面试以来,发展迅猛。Hadoop以MapReduce和HDFS为其核心,之后发展出了很多的框架,如Spark,Pig,Hive,Zookeeper等,主要用来进行大量数据的存储和分析。

Hadoop的配置有三种方式:

单机模式:主要用来进行MapReduce的开发,简单易用。

伪分布式:主要用于进行分布式存储和数据访问开发,在一台机器上模拟分布式配置,方便进行开发和测试。

全分布式:实际工作的集群使用的配置方式。

本文先介绍如何进行单机配置,然后通过一个简单的例子看看如何运行Hadoop的MapReduce程序。

单机配置

Hadoop依赖于JDK,因此,在进行配置前,需要到官网下载JDK和Hadoop,这里不再赘述。本文使用了目前最新的jdk-9.0.1以及hadoop-2.8.2.

Ubuntu下JDK的配置

在进行配置前,先确认是否安装了JDK,如果已经安装,可以省略JDK的配置,直接跳到下一小节。在终端执行

$ java -version

查看是否输出版本,如果没有,请按本小节进行操作。

如果没有创建/usr/lib/jvm/目录

$ sudo mkdir /usr/lib/jvm/

解压JDK

$ sudo tar xvzf jdk-9.0.1_linux-x64_bin.tar.gz -C /usr/lib/jvm/

配置环境变量

$ sudo vim /etc/profile

在文件尾部添加:

export JAVA_HOME=/usr/lib/jvm/jdk-9.0.1 export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH export CLASSPATH=$CLASSPATH:.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

使环境变量生效

$ source /etc/profile

验证安装成功,查看版本信息

$ java -version

Hadoop安装配置

解压Hadoop

$ sudo tar xvzf hadoop-2.8.2.tar.gz -C /usr/local/

配置环境变量

$ sudo vim /etc/profile

在文件末尾添加

export HADOOP_HOME=/usr/local/hadoop-2.8.2 export PATH=$PATH:$HADOOP_HOME/bin:$HADOOP_HOME/sbin

使环境变量生效

$ source /etc/profile

验证安装成功,查看版本信息

$ hadoop version

这样,Hadoop的单机模式就配置好了。下面做一个简单的MapReduce的例子,来看看如何运行MapReduce代码。详细的MapReduce的原理,以后的文章会给大家介绍。

MapReduce示例

下面通过一个例子来看看如何利用Hadoop进行开发。MapReduce后面的文章会做详细的介绍,这里只是为了示范如何开发,先不过多介绍。我们使用Eclispe进行开发。

创建一个Java Project,名称为HadoopDemo,后续文章的代码我尽可能包含在这个项目中,代码已上传到github,供大家下载学习。

在项目上右键,Build Path->Configure Build Path,在Libraries标签下,右面点击Add External JARs...,我们需要将Hadoop安装路径下的两个jar加入到项目中,然后保存退出。两个jar的路径如下:

share/hadoop/common/hadoop-common-2.8.2.jar

share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.8.2.jar

这样,项目就创建好了,我们希望利用MapReduce统计所有单词的首字母出现的次数,创建一个类,命名为FirstCharCount。这个类中需要定义两个内部类,分别执行Map和Reduce两个步骤。大家可以先不去深究代码的意思。代码如下:

package com.yjp.mapreduce; import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; 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; public class FirstCharCount { // 执行Map private static class FirstCharMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private static IntWritable one = new IntWritable(1); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer token = new StringTokenizer(line); while (token.hasMoreTokens()) { String word = token.nextToken(); context.write(new Text(word.charAt(0) + ""), one); } } } // 执行Reduce private static class FirstCharReducer extends Reducer<Text, IntWritable, Text, IntWritable> { @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int sum = 0; for (IntWritable value : values) { sum += value.get(); } context.write(key, new IntWritable(sum)); } } public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: FirstCharCount <inout path> <output path>"); System.exit(-1); } // 设置类信息,方便hadoop从JAR文件中找到 Job job = Job.getInstance(); job.setJarByClass(FirstCharCount.class); job.setJobName("First Char Count"); // 添加输入输出路径 FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); // 设置执行Map和Reduce的类 job.setMapperClass(FirstCharMapper.class); job.setReducerClass(FirstCharReducer.class); //设置输出数据类型 job.setOutputKeyClass(Text.class); job.setOutputValueClass(IntWritable.class); System.exit(job.waitForCompletion(true) ? 0 : 1); } }

然后在项目上面点击右键,Export...,选择Java下的JAR file,点击Next,然后选择Jar文件的保存路径,点击Finish即可

然后,回到命令行,我们创建一个input目录,作为输入目录,其中放置一个text文件,写入内容,我复制了一段Hadoop官网上的介绍

The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models. It is designed to scale up from single servers to thousands of machines, each offering local computation and storage. Rather than rely on hardware to deliver high-availability, the library itself is designed to detect and handle failures at the application layer, so delivering a highly-available service on top of a cluster of computers, each of which may be prone to failures.

在命令行执行:

$ hadoop jar 你的jar包文件名 全限定包名.FirstCharCount input output

注意output是输出目录,我们不需要创建,而且每次重新运行时,要删除同名的output目录,否则会报错,Hadoop之所以这样,是为了防止分布式处理时出现文件复写的问题。如果执行成功,应该会看到一个很长的报表

File System Counters         FILE: Number of bytes read=10004         FILE: Number of bytes written=661713         FILE: Number of read operations=0         FILE: Number of large read operations=0         FILE: Number of write operations=0

......

然后执行下面的命令,就能看到结果了。

$ cat output/part-r-00000

本文内容比较多,最好大家可以亲自试一下,有问题一起学习交流。

转载请注明原文地址: https://www.6miu.com/read-1399984.html

最新回复(0)