Linux下搭建Hadoop环境步骤分享
简介
Hadoop是当下最为流行的分布式计算框架之一,能够处理海量数据,并提供并行处理能力。本文将详细介绍如何在Linux系统下搭建Hadoop环境。
步骤
1. 安装JDK
首先需要安装JDK,步骤如下:
sudo apt update
sudo apt install default-jdk
2. 下载Hadoop
从官网下载Hadoop安装包,地址为 https://hadoop.apache.org。
wget https://downloads.apache.org/hadoop/common/hadoop-3.3.1/hadoop-3.3.1.tar.gz
3. 解压Hadoop
将下载的Hadoop压缩包解压,并移动到指定目录。
tar -zxvf hadoop-3.3.1.tar.gz
sudo mv hadoop-3.3.1 /usr/local/hadoop
4. 配置环境变量
编辑 ~/.bashrc,添加以下内容:
export HADOOP_HOME=/usr/local/hadoop
export PATH=$HADOOP_HOME/bin:$PATH
使环境变量生效:
source ~/.bashrc
5. 配置Hadoop
进入Hadoop配置目录,并编辑hadoop-env.sh,添加Java环境变量。
cd $HADOOP_HOME/etc/hadoop
sudo vim hadoop-env.sh
将以下内容添加到文件末尾:
export JAVA_HOME=/usr/lib/jvm/default-java
编辑core-site.xml,添加以下内容:
<configuration>
<property>
<name>fs.default.name</name>
<value>hdfs://localhost:9000</value>
</property>
</configuration>
编辑hdfs-site.xml,添加以下内容:
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
<property>
<name>dfs.namenode.name.dir</name>
<value>file:/usr/local/hadoop/hadoop_data/hdfs/namenode</value>
</property>
<property>
<name>dfs.datanode.data.dir</name>
<value>file:/usr/local/hadoop/hadoop_data/hdfs/datanode</value>
</property>
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>localhost:9001</value>
</property>
</configuration>
编辑mapred-site.xml.template,并复制一份,并命名为mapred-site.xml:
cp mapred-site.xml.template mapred-site.xml
编辑mapred-site.xml,添加以下内容:
<configuration>
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
编辑yarn-site.xml,添加以下内容:
<configuration>
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<property>
<name>yarn.nodemanager.aux-services.mapreduce_shuffle.class</name>
<value>org.apache.hadoop.mapred.ShuffleHandler</value>
</property>
<property>
<name>yarn.resourcemanager.hostname</name>
<value>localhost</value>
</property>
</configuration>
6. 启动Hadoop
启动Hadoop:
start-all.sh
查看进程:
jps
7. 示例说明
示例1:WordCount
在Hadoop上运行最简单的MapReduce示例WordCount。首先需要在本地创建一个输入文件。
echo "Hello World Again Again Wow!" > input.txt
将该文件上传到Hadoop文件系统上:
hdfs dfs -copyFromLocal input.txt input/input.txt
编写Java代码,实现WordCount:
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 java.io.IOException;
import java.util.StringTokenizer;
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("input"));
FileOutputFormat.setOutputPath(job, new Path("output"));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
将上述代码编译打包:
hadoop com.sun.tools.javac.Main WordCount.java
jar cf wc.jar WordCount*.class
运行WordCount:
hadoop jar wc.jar WordCount input output
在Hadoop文件系统上查看输出:
hdfs dfs -ls output
hdfs dfs -cat output/part*
示例2:HDFS读写
首先在Hadoop文件系统上创建一个目录并上传一个文件。
hdfs dfs -mkdir input
echo "Hello World Again Again Wow!" > input/input.txt
编写Java代码,实现HDFS读写:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
public class HdfsReadWrite {
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
FileSystem fileSystem = FileSystem.get(conf);
// 读取HDFS上的文件
BufferedReader reader = new BufferedReader(
new InputStreamReader(
fileSystem.open(new Path("input/input.txt")))
);
String content = reader.lines().collect(Collectors.joining("\n"));
System.out.println(content);
// 向HDFS上写入文件
fileSystem.create(new Path("output/output.txt"))
.write(content.getBytes());
fileSystem.close();
}
}
将上述代码编译打包:
hadoop com.sun.tools.javac.Main HdfsReadWrite.java
jar cf hdfs.jar HdfsReadWrite*.class
运行HdfsReadWrite:
hadoop jar hdfs.jar HdfsReadWrite
在Hadoop文件系统上查看输出:
hdfs dfs -ls output
hdfs dfs -cat output/output.txt
结论
本文介绍了在Linux系统上搭建Hadoop环境的详细步骤,并提供了两个示例,其中一个用于演示最简单的MapReduce示例WordCount,另一个用于演示HDFS读写文件。通过本文的学习,读者可以掌握搭建Hadoop环境和编写基本的Hadoop程序的技能。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:linux下搭建hadoop环境步骤分享 - Python技术站