《快速掌握Hadoop:从入门到实战》- 搭建大数据平台首选

84 阅读2分钟

Hadoop 分布式计算和存储框架教程

Hadoop 是一个开源的分布式计算和存储框架,由 Apache 基金会开发和维护。它为庞大的计算机集群提供了可靠的、可伸缩的应用层计算和存储支持,允许使用简单的编程模型跨计算机群集分布式处理大型数据集,并且支持在单台计算机到几千台计算机上扩展。

Hadoop 快速入门

安装 Hadoop 环境是学习 Hadoop 的第一步。以下是在 Ubuntu 18.04 LTS 上快速安装 Hadoop 的步骤:

  1. 下载 Hadoop 并解压缩:
wget https://mirror.bit.edu.cn/apache/hadoop/common/hadoop-3.3.0/hadoop-3.3.0.tar.gz
tar -xzvf hadoop-3.3.0.tar.gz

2.配置环境变量: 在 /etc/profile 中添加如下环境变量:

export HADOOP_HOME=/path/to/hadoop-3.3.0
export PATH=$HADOOP_HOME/bin:$PATH

执行 source /etc/profile,使修改生效。

3.启动 Hadoop 集群:

进入 Hadoop 目录,执行以下命令启动 Hadoop 集群:

cd /path/to/hadoop-3.3.0
sbin/start-all.sh

1584484171.jpg

Hadoop 实战

下面我们通过一个实例来演示在 Hadoop 中如何分析数据。

我们有一个文本文件 sample.txt,内容如下:

Hello World Welcome to Hadoop world Hadoop is a powerful framework

我们将使用 Hadoop 统计单词出现次数。

首先,我们需要把文件上传到 Hadoop 的分布式文件系统上:

hadoop fs -put sample.txt /

接着,我们编写一个 Java 程序 WordCount.java,代码如下:

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;
 
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();
    
    public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
      StringTokenizer itr = new StringTokenizer(value.toString());
      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();
    
    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);
    }
  }
  
  public static void main(String[] args) throws Exception {
    Configuration conf = new Configuration();
    Job job = Job.getInstance(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("/sample.txt"));
    FileOutputFormat.setOutputPath(job, new Path("/output"));
    System.exit(job.waitForCompletion(true) ? 0 : 1);
  }
}

编译程序并将 WordCount.class 上传到 Hadoop 分布式文件系统上:

javac -classpath ${HADOOP_HOME}/share/hadoop/mapreduce/lib/hadoop-mapreduce-client-core-3.3.0.jar -d . WordCount.java
hadoop fs -mkdir /input
hadoop fs -put WordCount.class /input

最后,在 Hadoop 集群上运行程序:

hadoop jar WordCount.jar /input/sample.txt /output

输出每个单词出现的次数。

QQ截图20230410185828.jpg

Hadoop 视频教程

如果您想深入学习 Hadoop,在这里提供一个​ 100 集 Hadoop 视频教程 ​。希望对您有所帮助!

视频教程获取方式:vk666.lanzoum.com/iwsCQ0slrh3…