目錄
- 1、本地運行模式(了解)
- 1.1 在hadoop-3.1.3檔案下面創建一個wcinput檔案夾
- 1.2 在wcinput檔案下創建一個word.txt檔案
- 1.3 回到Hadoop目錄/opt/module/hadoop-3.1.3
- 1.4 查看結果
- 2、完全分布式運行模式(重點)
- 2.1 撰寫集群分發腳本xsync
- 2.1.1 scp(secure copy)安全拷貝
- 2.1.2 rsync遠程同步工具
- 2.3 xsync集群分發腳本
- 2.2 SSH無密碼登錄配置
- 2.2.1 免密登錄原理
- 2.2.2 配置102無密登錄103和104
- 2.3 集群配置
- 2.3.1 集群部署規劃
- 2.3.2 組態檔說明
- 2.3.3 配置自定義檔案
- 2.4 群起集群
- 2.4.1 配置works
- 2.4.2 啟動集群
- 2.4.3 集群基本測驗
- Hadoop官方網站:http://hadoop.apache.org/
- Hadoop運行模式包括:本地模式、偽分布式模式以及完全分布式模式,
1、本地模式:單機運行,只是用來演示一下官方案例,生產環境不用,
2、偽分布式模式:也是單機運行,但是具備Hadoop集群的所有功能,一臺服務器模擬一個分布式的環境,個別缺錢的公司用來測驗,生產環境不用,
3、完全分布式模式:多臺服務器組成分布式環境,生產環境使用,

1、本地運行模式(了解)
1.1 在hadoop-3.1.3檔案下面創建一個wcinput檔案夾
[atguigu@hadoop102 hadoop-3.1.3]$ mkdir wcinput
1.2 在wcinput檔案下創建一個word.txt檔案
[atguigu@hadoop102 hadoop-3.1.3]$ cd wcinput
[atguigu@hadoop102 wcinput]$ vim word.txt
在檔案中輸入如下內容
hadoop yarn
hadoop mapreduce
atguigu atguigu
1.3 回到Hadoop目錄/opt/module/hadoop-3.1.3
# 選擇統計個數的jar包,執行wordcount,輸入的在wcinput,輸出在wcoutput(存在的話會報錯)
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar wordcount wcinput wcoutput
1.4 查看結果
[atguigu@hadoop102 hadoop-3.1.3]$ cat wcoutput/part-r-00000

2、完全分布式運行模式(重點)
2.1 撰寫集群分發腳本xsync
- 服務器與服務器之間的傳輸命令
- 利用分發腳本可以將102上的JDK和Hadoop直接拷貝到103和104上,
2.1.1 scp(secure copy)安全拷貝
- scp可以實作服務器與服務器之間的資料拷貝,

前提:在hadoop102、hadoop103、hadoop104都已經創建好的/opt/module和/opt/software兩個目錄,并且已經把這兩個目錄修改為atguigu:atguigu
(1)在hadoop102上,將hadoop102中/opt/module/jdk1.8.0_212目錄拷貝到hadoop103上,
[atguigu@hadoop102 ~]$ scp -r /opt/module/jdk1.8.0_212 atguigu@hadoop103:/opt/module
(2)在hadoop103上,將hadoop102中/opt/module/hadoop-3.1.3目錄拷貝到hadoop103上,
[atguigu@hadoop103 ~]$ scp -r atguigu@hadoop102:/opt/module/hadoop-3.1.3 /opt/module/
(3)在hadoop103上操作,將hadoop102中/opt/module目錄下所有目錄拷貝到hadoop104上,
[atguigu@hadoop103 opt]$ scp -r atguigu@hadoop102:/opt/module/* atguigu@hadoop104:/opt/module
2.1.2 rsync遠程同步工具
- rsync主要用于備份和鏡像,具有速度快、避免復制相同內容和支持符號鏈接的優點,
- rsync和scp區別:用rsync做檔案的復制要比scp的速度快,rsync只對差異檔案做更新,scp是把所有檔案都復制過去,

(1)洗掉hadoop103中/opt/module/hadoop-3.1.3/wcinput和wcoutput
[atguigu@hadoop103 hadoop-3.1.3]$ rm -rf wcinput/ wcoutput/
(2)同步hadoop102中的/opt/module/hadoop-3.1.3到hadoop103
[atguigu@hadoop102 module]$ rsync -av hadoop-3.1.3/ atguigu@hadoop103:/opt/module/hadoop-3.1.3/
2.3 xsync集群分發腳本
目的:每次訪問都要密碼,還要切換不同的集群,該腳本是對rsync進行封裝實作集群分發功能,回圈復制檔案到所有節點的相同目錄下
期望腳本案例實作:
- 在102上輸入:xsync a.txt 就把 txt檔案分發到103、104上相同的目錄里面,
- 在任何路徑上都能實作(全域路徑)
(1)新建txt檔案

(2)在/home/atguigu/bin目錄下創建xsync檔案
[atguigu@hadoop102 opt]$ cd /home/atguigu
[atguigu@hadoop102 ~]$ mkdir bin
[atguigu@hadoop102 ~]$ cd bin
[atguigu@hadoop102 bin]$ vim xsync
添加下面內容:
#!/bin/bash
#1. 判斷引數個數
if [ $# -lt 1 ]
then
echo Not Enough Arguement!
exit;
fi
#2. 遍歷集群所有機器
for host in hadoop102 hadoop103 hadoop104
do
echo ==================== $host ====================
#3. 遍歷所有目錄,挨個發送
for file in $@
do
#4. 判斷檔案是否存在
if [ -e $file ]
then
#5. 獲取父目錄
pdir=$(cd -P $(dirname $file); pwd)
#6. 獲取當前檔案的名稱
fname=$(basename $file)
ssh $host "mkdir -p $pdir"
rsync -av $pdir/$fname $host:$pdir
else
echo $file does not exists!
fi
done
done
(3)修改可執行權限

(4)在102上同步bin目錄,發現103和104上都有bin/xsync
[atguigu@hadoop102 ~]$ xsync bin/
(5)分發環境變數my_env.sh
# 發現根本沒有分發
[atguigu@hadoop102 profile.d]$ xsync /etc/profile.d/my_env.sh
修改:(注意:如果用了sudo,那么xsync一定要給它的路徑補全,)
[atguigu@hadoop102 ~]$ sudo ./bin/xsync/etc/profile.d/my_env.sh
(6)讓環境變數生效
[atguigu@hadoop103 bin]$ source /etc/profile
[atguigu@hadoop104 bin$ source /etc/profile
2.2 SSH無密碼登錄配置
hadoop102上直接訪問103,(exit退出)

2.2.1 免密登錄原理

(1)查看所有檔案(包括隱藏的)

(2)查看訪問記錄(只有用過ssh的才有.ssh)

(3).ssh檔案夾下(~/.ssh)的檔案功能解釋

2.2.2 配置102無密登錄103和104
(1)生成公鑰和私鑰
#然后敲(三個回車),就會生成兩個檔案id_rsa(私鑰)、id_rsa.pub(公鑰)
[atguigu@hadoop102 .ssh]$ ssh-keygen -t rsa

(2)將公鑰拷貝到要免密登錄的目標機器上
[atguigu@hadoop102 .ssh]$ ssh-copy-id hadoop102 # 訪問自己
[atguigu@hadoop102 .ssh]$ ssh-copy-id hadoop103
[atguigu@hadoop102 .ssh]$ ssh-copy-id hadoop104
- 在103和104上也做上述操作,就可以三個自己免密登錄,
- 目前只在atguigu上配置了,想再root權限下也免密,也需要相同配置,
2.3 集群配置
2.3.1 集群部署規劃
- NameNode和SecondaryNameNode不要安裝在同一臺服務器
- ResourceManager也很消耗記憶體,不要和NameNode、SecondaryNameNode配置在同一臺機器上,

2.3.2 組態檔說明
Hadoop組態檔分兩類:默認組態檔和自定義組態檔,只有用戶想修改某一默認配置值時,才需要修改自定義組態檔,更改相應屬性值,
(1)默認組態檔:

(2)自定義組態檔:
core-site.xml、hdfs-site.xml、yarn-site.xml、mapred-site.xml四個組態檔存放在[atguigu@hadoop102 ~]$ cd /opt/module/hadoop-3.1.3/etc/hadoop/這個路徑上,用戶可以根據專案需求重新進行修改配置,
2.3.3 配置自定義檔案
(1)配置core-site.xml
[atguigu@hadoop102 ~]$ cd /opt/module/hadoop-3.1.3/etc/hadoop/
[atguigu@hadoop102 hadoop]$ vim core-site.xml
添加內容:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 指定NameNode的地址 -->
<property>
<name>fs.defaultFS</name>
<value>hdfs://hadoop102:8020</value>
</property>
<!-- 指定hadoop資料的存盤目錄 -->
<property>
<name>hadoop.tmp.dir</name>
<value>/opt/module/hadoop-3.1.3/data</value>
</property>
<!-- 配置HDFS網頁登錄使用的靜態用戶為atguigu -->
<property>
<name>hadoop.http.staticuser.user</name>
<value>atguigu</value>
</property>
</configuration>

(2)HDFS組態檔,配置hdfs-site.xml
[atguigu@hadoop102 hadoop]$ vim hdfs-site.xml
檔案內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- nn web端訪問地址-->
<property>
<name>dfs.namenode.http-address</name>
<value>hadoop102:9870</value>
</property>
<!-- 2nn web端訪問地址-->
<property>
<name>dfs.namenode.secondary.http-address</name>
<value>hadoop104:9868</value>
</property>
</configuration>
(3)YARN組態檔,配置yarn-site.xml
[atguigu@hadoop102 hadoop]$ vim yarn-site.xml
檔案內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 指定MR走shuffle -->
<property>
<name>yarn.nodemanager.aux-services</name>
<value>mapreduce_shuffle</value>
</property>
<!-- 指定ResourceManager的地址-->
<property>
<name>yarn.resourcemanager.hostname</name>
<value>hadoop103</value>
</property>
<!-- 環境變數的繼承 -->
<property>
<name>yarn.nodemanager.env-whitelist</name>
<value>JAVA_HOME,HADOOP_COMMON_HOME,HADOOP_HDFS_HOME,HADOOP_CONF_DIR,CLASSPATH_PREPEND_DISTCACHE,HADOOP_YARN_HOME,HADOOP_MAPRED_HOME</value>
</property>
</configuration>
(4)MapReduce組態檔,配置mapred-site.xml
[atguigu@hadoop102 hadoop]$ vim mapred-site.xml
檔案內容如下:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 指定MapReduce程式運行在Yarn上 -->
<property>
<name>mapreduce.framework.name</name>
<value>yarn</value>
</property>
</configuration>
(5)在集群上分發配置好的Hadoop組態檔
[atguigu@hadoop102 hadoop]$ xsync /opt/module/hadoop-3.1.3/etc/hadoop/
(6)去103和104上查看檔案分發情況
[atguigu@hadoop103 ~]$ cat /opt/module/hadoop-3.1.3/etc/hadoop/core-site.xml
[atguigu@hadoop104 ~]$ cat /opt/module/hadoop-3.1.3/etc/hadoop/core-site.xml
2.4 群起集群
2.4.1 配置works
[atguigu@hadoop102 hadoop]$ vim /opt/module/hadoop-3.1.3/etc/hadoop/workers
在該檔案中增加如下內容:
hadoop102
hadoop103
hadoop104
注意:該檔案中添加的內容結尾不允許有空格,檔案中不允許有空行,
同步所有節點組態檔
[atguigu@hadoop102 hadoop]$ xsync /opt/module/hadoop-3.1.3/etc
2.4.2 啟動集群
(1)如果集群是第一次啟動,需要在hadoop102節點格式化NameNode(注意:格式化NameNode,會產生新的集群id,導致NameNode和DataNode的集群id不一致,集群找不到已往資料,如果集群在運行程序中報錯,需要重新格式化NameNode的話,一定要先停止namenode和datanode行程,并且要洗掉所有機器的data和logs目錄,然后再進行格式化,)
[atguigu@hadoop102 hadoop-3.1.3]$ hdfs namenode -format
初始化會生成一個data檔案:

(2)啟動HDFS
#102、103、104都啟動了
[atguigu@hadoop102 hadoop-3.1.3]$ sbin/start-dfs.sh

(3)在配置了ResourceManager的節點(hadoop103)啟動YARN
[atguigu@hadoop103 hadoop-3.1.3]$ sbin/start-yarn.sh

[atguigu@hadoop102 hadoop-3.1.3]$ jps
95059 NameNode
95282 DataNode
107602 Jps
103260 NodeManager
[atguigu@hadoop103 hadoop-3.1.3]$ jps
61331 ResourceManager
53572 DataNode
61545 NodeManager
63113 Jps
[atguigu@hadoop104 hadoop-3.1.3]$ jps
53574 DataNode
61465 NodeManager
66347 Jps
53771 SecondaryNameNode

(4)Web端查看HDFS的NameNode
(a)瀏覽器中輸入:http://hadoop102:9870

(b)查看HDFS上存盤的資料資訊

(5)Web端查看YARN的ResourceManager
(a)瀏覽器中輸入:http://hadoop103:8088
(b)查看YARN上運行的Job資訊

2.4.3 集群基本測驗
1. 上傳小檔案
# 創建
[atguigu@hadoop102 ~]$ hadoop fs -mkdir /wcinput
# 上傳本地word.txt到wcinput
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop fs -put wcinput/word.txt /wcinput


2. 上傳大檔案
# 上傳到根目錄
[atguigu@hadoop102 ~]$ hadoop fs -put /opt/software/jdk-8u212-linux-x64.tar.gz /

注意:
- 這里web頁面不具備存盤功能,只是方便展示;
- 真正存盤的檔案的是datenode,路徑為之前設定的data檔案夾中,
3. 查看HDFS檔案存盤路徑
[atguigu@hadoop102 subdir0]$ pwd
/opt/module/hadoop-3.1.3/data/dfs/data/current/BP-1436128598-192.168.10.102-1610603650062/current/finalized/subdir0/subdir0

4. 查看HDFS在磁盤存盤檔案內容
[atguigu@hadoop102 subdir0]$ cat blk_1073741825

# 查看檔案追加到gz
[atguigu@hadoop102 subdir0]$ cat blk_1073741826>>tmp.tar.gz
[atguigu@hadoop102 subdir0]$ cat blk_1073741827>>tmp.tar.gz
[atguigu@hadoop102 subdir0]$ tar -zxvf tmp.tar.gz

102,103,104上都有相同的內容,所以有三份,

5. 執行wordcount程式
[atguigu@hadoop102 hadoop-3.1.3]$ hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.3.jar wordcount /wcinput /wcoutput
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/417171.html
標籤:其他
下一篇:開課吧 不按時返現
