厦门大学数据库实验室http://dblab.xmu.edu.cn
厦门大学数据库实验室博客http://dblab.xmu.edu.cn/post/5663
1)Hadoop安装教程_单机/伪分布式配置Hadoop2.6.0(2.7.1)/Ubuntu14.04(16.04)
http://dblab.xmu.edu.cn/blog/install-hadoop/
#创建Hadoop用户
sudo useradd -m hadoop -s /bin/bash
sudo passwd hadoop
sudo adduser hadoop sudo
#安装SSH、配置SSH无密码登陆
sudo apt-get install openssh-server
ssh localhost
exit # 退出刚才的 ssh localhost
cd ~/.ssh/ # 若没有该目录,请先执行一次ssh localhost
ssh-keygen -t rsa # 会有提示,都按回车就可以
cat ./id_rsa.pub >> ./authorized_keys # 加入授权
#安装Java环境
cd /usr/lib
sudo mkdir jvm #创建/usr/lib/jvm目录用来存放JDK文件
cd ~ #进入hadoop用户的主目录
cd Downloads #注意区分大小写字母,刚才已经通过FTP软件把JDK安装包jdk-8u162-linux-x64.tar.gz上传到该目录下
sudo tar -zxvf ./jdk-8u162-linux-x64.tar.gz -C /usr/lib/jvm #把JDK文件解压到/usr/lib/jvm目录下
cd /usr/lib/jvm
cd ~
vim ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_162 #添加
export JRE_HOME=${JAVA_HOME}/jre #添加
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib #添加
export PATH=${JAVA_HOME}/bin:$PATH #添加
source ~/.bashrc
java -version
#安装 Hadoop 2
sudo tar -zxf ~/下载/hadoop-2.6.0.tar.gz -C /usr/local # 解压到/usr/local中
cd /usr/local/
sudo mv ./hadoop-2.6.0/ ./hadoop # 将文件夹名改为hadoop
sudo chown -R hadoop ./hadoop # 修改文件权限
cd /usr/local/hadoop
./bin/hadoop version
#Hadoop单机配置(非分布式)
cd /usr/local/hadoop
mkdir ./input
cp ./etc/hadoop/*.xml ./input # 将配置文件作为输入文件
./bin/hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-*.jar grep ./input ./output 'dfs[a-z.]+'
cat ./output/* # 查看运行结果
#Hadoop伪分布式配置,需要对core-site.xml和hdfs-site.xml进行修改
cd /usr/local/hadoop
./bin/hdfs namenode -format
cd /usr/local/hadoop
./sbin/start-dfs.sh #start-dfs.sh是个完整的可执行文件,中间没有空格
./sbin/stop-dfs.sh # 关闭
2)Hadoop安装教程_伪分布式配置_CentOS6.4/Hadoop2.6.0
http://dblab.xmu.edu.cn/blog/install-hadoop-in-centos/
3)Hadoop3.1.3安装教程_单机/伪分布式配Hadoop3.1.3/Ubuntu18.04(16.04)
http://dblab.xmu.edu.cn/blog/2441-2/
4)Hadoop集群安装配置教程_Hadoop2.6.0_Ubuntu/CentOS
http://dblab.xmu.edu.cn/blog/install-hadoop-cluster/
5)大数据技术原理与应用 第二章 大数据处理架构Hadoop 学习指南
http://dblab.xmu.edu.cn/blog/285/
6)大数据技术原理与应用 第三章 分布式文件系统HDFS 学习指南
http://dblab.xmu.edu.cn/blog/290-2/
#含目录操作与文件操作
cd /usr/local/hadoop
./bin/hdfs dfs –mkdir –p /user/hadoop
./bin/hdfs dfs –ls .
./bin/hdfs dfs –ls /user/hadoop
./bin/hdfs dfs –rm –r /input
./bin/hdfs dfs -put /home/hadoop/myLocalFile.txt input
./bin/hdfs dfs –cat input/myLocalFile.txt
./bin/hdfs dfs -get input/myLocalFile.txt /home/hadoop/下载
./bin/hdfs dfs -cp input/myLocalFile.txt /input
7)使用Eclipse编译运行MapReduce程序_Hadoop2.6.0_Ubuntu/CentOS
http://dblab.xmu.edu.cn/blog/hadoop-build-project-using-eclipse/
/*wordCount.java*/
package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.Iterator;
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 WordCount() {
}
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> [<in>...] <out>");
System.exit(2);
}
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(WordCount.TokenizerMapper.class);
job.setCombinerClass(WordCount.IntSumReducer.class);
job.setReducerClass(WordCount.IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
for(int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
}
FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
System.exit(job.waitForCompletion(true)?0:1);
}
public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
public IntSumReducer() {
}
public void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
int sum = 0;
IntWritable val;
for(Iterator i$ = values.iterator(); i$.hasNext(); sum += val.get()) {
val = (IntWritable)i$.next();
}
this.result.set(sum);
context.write(key, this.result);
}
}
public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
private static final IntWritable one = new IntWritable(1);
private Text word = new Text();
public TokenizerMapper() {
}
public void map(Object key, Text value, Mapper<Object, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while(itr.hasMoreTokens()) {
this.word.set(itr.nextToken());
context.write(this.word, one);
}
}
}
}