第3章的代码-林子雨编著《大数据基础编程、实验和案例教程(第3版)》教材

大数据学习路线图

林子雨编著《大数据基础编程、实验和案例教程(第3版)》(教材官网)教材中的命令行和代码,在纸质教材中的印刷效果不是很好,可能会影响读者对命令行和代码的理解,为了方便读者正确理解命令行和代码或者直接拷贝命令行和代码用于上机实验,这里提供全书配套的所有命令行和代码。
查看教材所有章节的代码

第3章 Hadoop的安装和使用

教材第29页

(温馨提示:代码框上方的复制代码按钮,也就是“两张A4纸图标”,用鼠标点击复制代码按钮,就可以把代码框中的代码复制到粘贴板,粘贴到其他地方。但是,有的浏览器可能不支持该功能)

sudo apt-get update
sudo apt-get install openssh-server
ssh localhost

教材第30页

cd ~/.ssh/        # 若没有该目录,请先执行一次ssh localhost
ssh-keygen -t rsa    # 会有提示,都按回车即可
cat ./id_rsa.pub >> ./authorized_keys  # 加入授权

教材第31页

cd /usr/lib
sudo mkdir jvm #创建/usr/lib/jvm目录用来存放JDK文件
cd ~ #进入hadoop用户的主目录
cd Downloads
sudo tar -zxvf ./jdk-8u171-linux-x64.tar.gz -C /usr/lib/jvm
vim ~/.bashrc
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_171
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
source ~/.bashrc
java -version

教材第32页

sudo tar -zxf ~/下载/hadoop-3.3.5.tar.gz -C /usr/local    # 解压到/usr/local中
cd /usr/local/
sudo mv ./hadoop-3.3.5/ ./hadoop      # 将文件夹名改为hadoop
sudo chown -R hadoop ./hadoop       # 修改文件权限
cd /usr/local/hadoop
./bin/hadoop version
cd /usr/local/hadoop
./bin/hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-3.3.5.jar

教材第33页

cd /usr/local/hadoop
mkdir input
cp ./etc/hadoop/*.xml ./input   # 将配置文件复制到input目录下
./bin/hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-*.jar grep ./input ./output 'dfs[a-z.]+'
cat ./output/*          # 查看运行结果
rm -r ./output
<configuration>
    <property>
        <name>hadoop.tmp.dir</name>
        <value>file:/usr/local/hadoop/tmp</value>
        <description>Abase for other temporary directories.</description>
    </property>
    <property>
        <name>fs.defaultFS</name>
        <value>hdfs://localhost:9000</value>
    </property>
</configuration>

教材第34页

<configuration>
    <property>
        <name>dfs.replication</name>
        <value>1</value>
    </property>
    <property>
        <name>dfs.namenode.name.dir</name>
        <value>file:/usr/local/hadoop/tmp/dfs/name</value>
    </property>
    <property>
        <name>dfs.datanode.data.dir</name>
        <value>file:/usr/local/hadoop/tmp/dfs/data</value>
    </property>
</configuration>
cd /usr/local/hadoop
./bin/hdfs namenode -format

教材第35页

cd /usr/local/hadoop
./sbin/start-dfs.sh  #start-dfs.sh是个完整的可执行文件,中间没有空格

教材第36页

export HADOOP_HOME=/usr/local/hadoop
export HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
jps

教材第37页

./sbin/stop-dfs.sh   # 关闭
rm -r ./tmp     # 删除 tmp 文件,注意这会删除 HDFS中原有的所有数据
./bin/hdfs namenode -format   # 重新格式化名称节点
./sbin/start-dfs.sh  # 重启
cd /usr/local/hadoop
./bin/hdfs dfs -mkdir -p /user/hadoop
cd /usr/local/hadoop
./bin/hdfs dfs -mkdir input  #在HDFS中创建hadoop用户对应的input目录
./bin/hdfs dfs -put ./etc/hadoop/*.xml input  #把本地文件复制到HDFS中

教材第38页

./bin/hdfs dfs -ls input
./bin/hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar grep input output 'dfs[a-z.]+'
./bin/hdfs dfs -cat output/*
./bin/hdfs dfs -rm -r output    # 删除 output 文件夹
cd /usr/local/hadoop
./sbin/stop-dfs.sh

教材第39页

export PATH=$PATH:/usr/local/hadoop/sbin
export PATH=$PATH:/usr/local/hadoop/sbin:/usr/local/hadoop/bin

教材第40页

sudo vim /etc/hostname
sudo ifconfig

教材第41页

netstat -nr

教材第42页

sudo vim /etc/hosts
192.168.91.128   hadoop01
192.168.91.129   hadoop02
192.168.91.130   hadoop03

教材第43页

192.168.91.128   hadoop01
192.168.91.129   hadoop02
192.168.91.130   hadoop03
ping hadoop01 -c 3   # 只ping 3次就会停止,否则要按Ctrl+c中断ping命令
ping hadoop02 -c 3
ping hadoop03 -c 3
sudo apt-get install openssh-server

教材第44页

cd ~/.ssh              # 如果没有该目录,先执行一次ssh localhost
rm ./id_rsa*           # 删除之前生成的公匙(如果已经存在)
ssh-keygen -t rsa       # 执行该命令后,遇到提示信息,一直按回车就可以
cat ./id_rsa.pub >> ./authorized_keys
scp ~/.ssh/id_rsa.pub hadoop@hadoop02:/home/hadoop/
scp ~/.ssh/id_rsa.pub hadoop@hadoop03:/home/hadoop/
mkdir ~/.ssh       # 如果不存在该文件夹需先创建,若已存在,则忽略本命令
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
rm ~/id_rsa.pub    # 用完以后就可以删掉
ssh hadoop02
ssh hadoop03

教材第45页

sudo tar -zxvf ~/Downloads/hadoop-3.3.5.tar.gz -C /usr/local    # 解压到/usr/local中
cd /usr/local/
sudo mv ./hadoop-3.3.5/ ./hadoop      # 将文件夹名改为hadoop
sudo chown -R hadoop:hadoop ./hadoop       # 修改文件权限
export PATH=$PATH:/usr/local/hadoop/bin:/usr/local/hadoop/sbin
hadoop01
hadoop02
hadoop03

教材第46页

<configuration>
        <property>
                <name>fs.defaultFS</name>
                <value>hdfs://Master:9000</value>
        </property>
        <property>
                <name>hadoop.tmp.dir</name>
                <value>file:/usr/local/hadoop/tmp</value>
                <description>Abase for other temporary directories.</description>
        </property>
</configuration>
<configuration>
        <property>
                <name>dfs.namenode.secondary.http-address</name>
                <value>Master:50090</value>
        </property>
        <property>
                <name>dfs.replication</name>
                <value>1</value>
        </property>
        <property>
                <name>dfs.namenode.name.dir</name>
                <value>file:/usr/local/hadoop/tmp/dfs/name</value>
        </property>
        <property>
                <name>dfs.datanode.data.dir</name>
                <value>file:/usr/local/hadoop/tmp/dfs/data</value>
        </property>
</configuration>
<configuration>
        <property>
                <name>mapreduce.framework.name</name>
                <value>yarn</value>
        </property>
        <property>
                <name>mapreduce.jobhistory.address</name>
                <value>hadoop01:10020</value>
        </property>
        <property>                     <name>mapreduce.jobhistory.webapp.address</name>
                <value>hadoop01:19888</value>
        </property>
        <property>
<name>yarn.app.mapreduce.am.env</name>
<value>HADOOP_MAPRED_HOME=/usr/local/hadoop</value>
</property>
<property>
<name>mapreduce.map.env</name>
<value>HADOOP_MAPRED_HOME=/usr/local/hadoop</value>
</property>
<property>
<name>mapreduce.reduce.env</name>
<value>HADOOP_MAPRED_HOME=/usr/local/hadoop</value>
</property> 
</configuration>

教材第47页

<configuration>
        <property>
                <name>yarn.resourcemanager.hostname</name>
                <value>hadoop01</value>
        </property>
        <property>
                <name>yarn.nodemanager.aux-services</name>
                <value>mapreduce_shuffle</value>
        </property>
</configuration>
cd /usr/local/hadoop
sudo rm -r ./tmp     # 删除 Hadoop 临时文件
sudo rm -r ./logs/*   # 删除日志文件
cd /usr/local
tar -zcf ~/hadoop.master.tar.gz ./hadoop   # 先压缩再复制
cd ~
scp ./hadoop.master.tar.gz hadoop02:/home/hadoop
scp ./hadoop.master.tar.gz hadoop03:/home/hadoop

教材第48页

cd ~
sudo rm -r /usr/local/hadoop    # 删掉旧的(如果存在)
sudo tar -zxf ~/hadoop.master.tar.gz -C /usr/local
sudo chown -R hadoop /usr/local/hadoop
cd /usr/local/hadoop
./bin/hdfs namenode -format
cd /usr/local/hadoop
./sbin/start-dfs.sh
./sbin/start-yarn.sh
./sbin/mr-jobhistory-daemon.sh start historyserver
cd /usr/local/hadoop
./bin/hdfs dfsadmin -report

教材第49页

hdfs dfs -mkdir -p /user/hadoop

教材第50页

hdfs dfs -mkdir input
hdfs dfs -put /usr/local/hadoop/etc/hadoop/*.xml input
hadoop jar /usr/local/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar grep input output 'dfs[a-z.]+'
stop-yarn.sh
stop-dfs.sh
mr-jobhistory-daemon.sh stop historyserver