Ubantu19安裝配置Hadoop3
主要參考了這兩篇博客:
https://blog.csdn.net/weixin_38883338/article/details/82928809
https://blog.csdn.net/kongxx/article/details/79350554
需要注意的是,Hadoop3配置xml文件與Hadoop2配置xml文件不太一樣。
(對於我的系統):
Hadoop bin和sbin所在的文件夾:/usr/local/hadoop
將Hadoop添加到PATH中:
https://blog.csdn.net/qjk19940101/article/details/70666349
在運行hadoop相關命令時,'/'代表的是'hdfs://localhost:9000'
有一些.sh配置文件在/usr/local/hadoop/etc/hadoop裏,比如hadoop-env.sh。
Hadoop編譯、運行java文件
前提是已經配置好java.
1. 啓動Hadoop
start-all.sh
2. 在hadoop目錄下創建myclass和input文件
mkdir -p myclass input
3. 進入input目錄,建立例子文件上傳到hdfs中
touch quangle.txt
vim quangle.txt
hadoop fs -mkdir /class4
hadoop fs -ls /
cd /usr/local/hadoop/input
hadoop fs -copyFromLocal quangle.txt /class4/quangle.txt
hadoop fs -ls /class4
4. 配置本地環境
cd /usr/local/hadoop/etc/hadoop
sudo vim hadoop-env.sh
添加:
export HADOOP_CLASSPATH=/usr/local/hadoop/myclass
5. 編寫代碼
cd /usr/local/hadoop/myclass/
vim FileSystemCat.java
6. 編譯代碼
javac -classpath ../share/hadoop/common/hadoop-common-3.2.0.jar FileSystemCat.java
7. 打包class
jar -cvf FileSystemCat.jar -C . FileSystemCat.class
此時該文件目錄裏面會有:
FileSystemCat.class FileSystemCat.jar FileSystemCat.java三個文件.
8. 運行java程序
hadoop jar FileSystemCat.jar FileSystemCat /class4/quangle.txt
對於hadoop1,可以這樣寫:
./hadoop jar ../hadoop-examples-1.2.1.jar wordcount /home/hadoop/input /home/hadoop/output
記錄一下配置過程中遇到的問題
1. Hadoop 沒辦法啓動resourcemanager和nodemanager,查看log發現java.lang.ClassNotFoundException: javax.activation.DataSource錯誤。
是jdk版本的問題,我之前安裝的是jdk12,對javax api的調用有限制,應該改成jdk8。
重新下載一個就好,之前那個不用刪,但是要改 .bashrc,etc/profile,以及hadoop/etc/裏面那些ev.sh的JAVA_HOME,然後source重啓,如果出現了靈異事件,那就把電腦重啓一下,確保環境更新了。
2.
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/yarn/exceptions/YarnRuntimeException
將yarn - site.xml中的yarn.application.classpath配置項註釋掉,就可以正常跑wordcount.
3. 每次啓動都要重新格式化namenode
在網上找到了相應的解決方法http://blog.csdn.net/bychjzh/article/details/7830508
內容如下:
最近遇到了一個問題,執行start-all.sh的時候發現JPS一下namenode沒有啓動
每次開機都得重新格式化一下namenode纔可以
其實問題就出在tmp文件,默認的tmp文件每次重新開機會被清空,與此同時namenode的格式化信息就會丟失
於是我們得重新配置一個tmp文件目錄
首先在home目錄下建立一個hadoop_tmp目錄
sudo mkdir ~/hadoop_tmp
然後修改hadoop/conf目錄裏面的core-site.xml文件,加入以下節點:
<property>
<name>hadoop.tmp.dir</name>
<value>/home/chjzh/hadoop_tmp</value>
<description>A base for other temporary directories.</description>
</property>
注意:我的用戶是chjzh所以目錄是/home/chjzh/hadoop_tmp
OK了,重新格式化Namenode
hadoop namenode -format
然後啓動hadoop
start-all.sh
執行下JPS命令就可以看到NameNode了
我依照上面的方法操作後,發現還是無法啓動。
最後才明白,是新建的目錄沒有修改權限,圖點簡單,就直接給了777權限,然後就都好了。
室友推薦了一篇更加詳細的配置hadoop 的文章,圖文並茂。
http://blog.csdn.net/hitwengqi/article/details/8008203
(core-site.xml 和 hdfs-site.xml的配置參考了這篇博客)
Hadoop小項目——倒排索引
步驟:
1. 啓動hadoop
start-all.sh
jps
2. 在hdfs中創建input和output目錄
hadoop fs -mkdir /cn
cd /usr/local/hadoop/input
touch file0.txt
# vim
touch file1.txt
# vim
touch file2.txt
# vim
hadoop fs -copyFromLocal file0.txt /cn/file0.txt
hadoop fs -copyFromLocal file1.txt /cn/file1.txt
hadoop fs -copyFromLocal file2.txt /cn/file2.txt
hadoop fs -mkdir /cn_output
3. 編輯java源文件
cd /usr/local/hadoop/myclass
vim InvertedIndexMapper.java
vim InvertedIndexCombiner.java
vim IntertedIndexReducer.java
vim InvertedIndexRunner.java
4. 整合、下載必須的編譯java文件所需要的jar,並(爲了方便)寫成shell文件
編譯java程序時會報錯:找不到 org.apache.common.lang符號,所以我在官網上下載了lang的binary版的tar.gz,然後解壓縮,發現裏面有一個commons-lang-2.6.jar的文件,然後我把它拷貝在了/usr/local/hadoop/share/hadoop/common裏。
# now we are in /usr/local/hadoop/myclass
touch cn_run.sh
vim cn_run.sh
chmod +x cn_run.sh
cn_run.sh裏面的內容:
javac -classpath ../share/hadoop/common/hadoop-common-3.2.0.jar:../share/hadoop/common/commons-lang-2.6.jar:../share/hadoop/mapreduce/hadoop-mapreduce-client-common-3.2.0.jar:../share/hadoop/mapreduce/hadoop-mapreduce-client-core-3.2.0.jar InvertedIndexRunner.java
5. 先編譯、打包除了InvertedIndexRunner之外的三個java程序(因爲Runner的編譯需要依賴於幾個class)
jar -cvf InvertedIndexMapper.jar -C . InvertedIndexMapper.class
jar -cvf InvertedIndexCombiner.jar -C . InvertedIndexCombiner.class
jar -cvf InvertedIndexReducer.jar -C . InvertedIndexReducer.class
6. 編譯、打包InvertedIndexRunner.java
vim cn_run.sh
cn_run.sh裏面的內容:
javac -classpath ../share/hadoop/common/hadoop-common-3.2.0.jar:../share/hadoop/common/commons-lang-2.6.jar:../share/hadoop/mapreduce/hadoop-mapreduce-client-common-3.2.0.jar:../share/hadoop/mapreduce/hadoop-mapreduce-client-core-3.2.0.jar:/usr/local/hadoop/myclass/InvertedIndexMapper.jar:/usr/local/hadoop/myclass/InvertedIndexCombiner.jar:/usr/local/hadoop/myclass/InvertedIndexReducer.jar InvertedIndexRunner.java
jar -cvf InvertedIndexRunner.jar -C . InvertedIndexRunner.class
7. 運行
hadoop jar InvertedIndexRunner.jar InvertedIndexRunner /cn /cn_output
大功告成啦!
可以通過 localhost:50070查看hdfs狀態,通過localhost:8088查看yarn管理狀態。
用wireshark抓取本地數據包:
在wireshark圖形化面板中選擇 捕獲-選項-loopback,然後點開始。
1. 心跳數據包
start-all.sh
2. hdfs
hadoop fs -ls /