mac下hadoop環境的搭建以及碰到的坑點

後開啟撒打發了發表於2017-10-15

提示:這裡有Exit code: 127 Stack trace: ExitCodeException exitCode=127: 錯誤的解決的方法,在文章最後面

一、首先要配置好java環境
下載地址http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
mac電腦直接jdk-8u144-macosx-x64.dmg一鍵安裝jdk
然後配置jdk環境,

JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home  
CLASSPAHT=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar  
PATH=$JAVA_HOME/bin:$PATHexport JAVA_HOME  
export CLASSPATH  
export PATH  

二、ssh配置
首先確認能夠遠端登入
系統偏好設定-共享 -遠端登入

  1. ssh-keygen -t rsa
    Press enter for each line 提示輸入直接按回車就好

  2. cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

  3. chmod og-wx ~/.ssh/authorized_keys
    ssh localhost
    如果ssh localhost還需要密碼 檢視一下你.ssh目錄的許可權
    .ssh的父目錄的許可權問題(我的問題就出現在這裡):.ssh的父目錄檔案許可權應該是755,即所屬使用者的 使用者檔案 (/home下屬的一個使用者檔案)

執行chmod 755 ~/.ssh

三、安裝配置hadoop檔案
下載地址:http://hadoop.apache.org/releases.html
http://mirrors.tuna.tsinghua.edu.cn/apache/hadoop/common/hadoop-2.8.1/hadoop-2.8.1.tar.gz

tar -zxvf hadoop-2.8.1.tar.gz 
cd hadoop-2.8.1  

修改配置檔案 配置檔案在hadoop目錄下的/etc/hadoop
1.修改core-site.xml 檔案

<configuration>  
    <!-- 指定HDFS老大(namenode)的通訊地址 -->  
    <property>  
        <name>fs.defaultFS</name>  
        <value>hdfs://localhost:9000</value>  
    </property>  
    <!-- 指定hadoop執行時產生檔案的儲存路徑 -->  
    <property>  
        <name>hadoop.tmp.dir</name>  
        <value>/Users/chenxun/software/hadoop-2.8.1/temp</value>  
    </property>  
</configuration>

2.修改hadfs-site.xml
預設副本數3,修改為1,dfs.namenode.name.dir指明fsimage存放目錄,多個目錄用逗號隔開。dfs.datanode.data.dir指定塊檔案存放目錄,多個目錄逗號隔開

<configuration>
<property>  
        <name>dfs.replication</name>  
        <value>1</value>  
    </property>  
    <property>  
        <name>dfs.namenode.name.dir</name>  
       <value>file:/Users/chenxun/software/hadoop-2.8.1/tmp/hdfs/name</value>  
    </property>  
    <property>  
        <name>dfs.datanode.data.dir</name>  
          <value>file:/Users/chenxun/software/hadoop-2.8.1/tmp/hdfs/data</value>  
    </property>  
    <property>  
        <name>dfs.namenode.secondary.http-address</name>  
        <value>localhost:9001</value>  
    </property>  
    <property>  
      <name>dfs.webhdfs.enabled</name>  
      <value>true</value>  
    </property> 
</configuration>

3.yarn配置
mapred-site.xml

<configuration>
<property>  
        <name>mapreduce.framework.name</name>  
        <value>yarn</value>  
    </property>  
    <property>  
        <name>mapreduce.admin.user.env</name>  
        <value>HADOOP_MAPRED_HOME=$HADOOP_COMMON_HOME</value>  
    </property>  
    <property>  
        <name>yarn.app.mapreduce.am.env</name>  
        <value>HADOOP_MAPRED_HOME=$HADOOP_COMMON_HOME</value>  
    </property> 

    <property>
  <name>mapreduce.application.classpath</name>
  <value>
      /Users/chenxun/software/hadoop-2.8.1/etc/hadoop,
      /Users/chenxun/software/hadoop-2.8.1/share/hadoop/common/*,
      /Users/chenxun/software/hadoop-2.8.1/share/hadoop/common/lib/*,
      /Users/chenxun/software/hadoop-2.8.1/share/hadoop/hdfs/*,
      /Users/chenxun/software/hadoop-2.8.1/share/hadoop/hdfs/lib/*,
      /Users/chenxun/software/hadoop-2.8.1/share/hadoop/mapreduce/*,
      /Users/chenxun/software/hadoop-2.8.1/share/hadoop/mapreduce/lib/*,
      /Users/chenxun/software/hadoop-2.8.1/share/hadoop/yarn/*,
      /Users/chenxun/software/hadoop-2.8.1/share/hadoop/yarn/lib/*
  </value>
</property>
</configuration>

yarn-site.xml

<configuration>

<!-- Site specific YARN configuration properties -->
    <property>  
        <name>yarn.nodemanager.aux-services</name>  
        <value>mapreduce_shuffle</value>  
    </property>

    <property>
    <name>yarn.application.classpath</name>
    <value>
        /Users/chenxun/software/hadoop-2.8.1/etc/hadoop,
        /Users/chenxun/software/hadoop-2.8.1/share/hadoop/common/*,
        /Users/chenxun/software/hadoop-2.8.1/share/hadoop/common/lib/*,
        /Users/chenxun/software/hadoop-2.8.1/share/hadoop/hdfs/*,
        /Users/chenxun/software/hadoop-2.8.1/share/hadoop/hdfs/lib/*,
        /Users/chenxun/software/hadoop-2.8.1/share/hadoop/mapreduce/*,
        /Users/chenxun/software/hadoop-2.8.1/share/hadoop/mapreduce/lib/*,
        /Users/chenxun/software/hadoop-2.8.1/share/hadoop/yarn/*,
        /Users/chenxun/software/hadoop-2.8.1/share/hadoop/yarn/lib/*
    </value>
  </property>

</configuration>

四、配置hadoop環境變數,可能需要重新編譯native library

vim ~/.bash_profile
export HADOOP_HOME=/Users/chenxun/software/hadoop-2.8.1
export PATH=$PATH:$HADOOP_HOME/sbin:$HADOOP_HOME/bin

export  LD_LIBRARY_PATH=$HADOOP_HOME/lib/native/
export  HADOOP_COMMON_LIB_NATIVE_DIR=$HADOOP_HOME/lib/native
export  HADOOP_OPTS="-Djava.library.path=$HADOOP_HOME/lib/native:$HADOOP_COMMON_LIB_NATIVE_DIR"

**上面配置基本完成,一般沒什麼問題了。可以開始執行你的hadoop了,直接看第五步好了,有問題繼續往下看。

**

下面步驟在你執行hadoop的過程可能碰到,如果你碰到了再回頭看這,:
在mac可能還要重新編譯hadoop的執行用到動態庫;這個時候肯能要配置maven環境,根據自己的maven安裝路徑配置maven環境,再重新編譯/lib/native下動態要用到
如果你在執行出現下面的情況就需要重新編譯動態庫:
WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform… using builtin-java classes where applicable
參考連結:http://blog.csdn.net/tterminator/article/details/51779689
參考二:Mac下 hadoop-2.7.0編譯過程記錄http://blog.csdn.net/beijihukk/article/details/53782508?utm_medium=referral&utm_source=itdadao

先下載好hadoop原始碼:hadoop-2.8.1-src
重新編譯之前需要安裝
protobuf
和maven以及cmake,自己百度搜尋安裝即可
protobuf安裝方法如下:

1. protobuf 2.5版本的brew安裝方法
$ brew search protobuf
protobuf                   protobuf-c                 protobuf-swift
homebrew/php/php53-protobuf              homebrew/php/php56-protobuf
homebrew/php/php54-protobuf              homebrew/versions/protobuf250
homebrew/php/php55-protobuf              homebrew/versions/protobuf260
$ brew install homebrew/versions/protobuf250

配置maven環境

export M2_HOME=/Users/chenxun/software/apache-maven-3.5.0

編譯動態庫過程出現下面的錯誤可能是zlib沒安裝
也可能是openssl沒設定好,設定openssl的方法如下(我的mac,你根據你自己的openssl安裝路徑設定)

export OPENSSL_ROOT_DIR=/usr/local/Cellar/openssl/1.0.2k
export OPENSSL_INCLUDE_DIR=/usr/local/Cellar/openssl/1.0.2k/include

**開始編譯native庫,命令如下:
mvn package -Pdist,native -DskipTests -Dtar**

將編譯出的native library複製到下載的二進位制版本的hadoop-2.8.1相應目錄中
編譯出的native library庫的位置為
hadoop-2.8.1-src/hadoop-dist/target/hadoop-2.8.1/lib/native
拷貝到二進位制版本的hadoop-2.8.1的目錄
hadoop-2.8.1/lib/native

修改/etc/hadoop/hadoop-env.sh配置

 export HADOOP_OPTS=”$HADOOP_OPTS -Djava.net.preferIPv4Stack=true -Djava.library.path=/hadoop-2.6.0/lib/native”

重新啟動Hadoop

此時就不會出現本文開頭處的那個警告了。


五、執行hadoop 以及hadoop命令簡單的介紹
格式化HDFS

hdfs namenode -format

一次啟動hadoop所有程式:

start-all.sh 

開啟 http://localhost:50070 進入hdfs管理頁面
開啟 http://localhost:8088 進入hadoop程式管理頁面

hadoop命令簡單介紹:
建立目錄

hdfs dfs -mkdir -p /user/chenxun/input

檢視:
hadoop fs -ls /user/chenxun/

先自己隨便建一個file.txt檔案,在檔案隨便寫點東西,如下:

vim file.txt

hadoop
chenxun
hadoop
chen

把這個檔案用命令上傳你剛才建檔案input下面:命令如下:

hdfs dfs -put ./file.txt  input 

用命令檢視一下是否成功:如果成功你在input下面看到file.txt檔案

hadoop fs -ls /user/chenxun/input

這個時候你測試hadoop提供的wordcount例子:執行下面的語句(如果出現錯誤不要著急往文章後面看解決方法)

hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.8.1.jar wordcount /user/chenxun/input/file.txt  output

執行過程如下

17/10/14 01:55:26 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032
17/10/14 01:55:28 INFO input.FileInputFormat: Total input files to process : 1
17/10/14 01:55:28 INFO mapreduce.JobSubmitter: number of splits:1
17/10/14 01:55:28 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1507912953622_0017
17/10/14 01:55:28 INFO impl.YarnClientImpl: Submitted application application_1507912953622_0017
17/10/14 01:55:28 INFO mapreduce.Job: The url to track the job: http://chen.local:8088/proxy/application_1507912953622_0017/
17/10/14 01:55:28 INFO mapreduce.Job: Running job: job_1507912953622_0017
17/10/14 01:55:36 INFO mapreduce.Job: Job job_1507912953622_0017 running in uber mode : false
17/10/14 01:55:36 INFO mapreduce.Job:  map 0% reduce 0%
17/10/14 01:55:41 INFO mapreduce.Job:  map 100% reduce 0%
17/10/14 01:55:47 INFO mapreduce.Job:  map 100% reduce 100%
17/10/14 01:55:47 INFO mapreduce.Job: Job job_1507912953622_0017 completed successfully
17/10/14 01:55:48 INFO mapreduce.Job: Counters: 49
    File System Counters
        FILE: Number of bytes read=44
        FILE: Number of bytes written=276523
        FILE: Number of read operations=0
        FILE: Number of large read operations=0
        FILE: Number of write operations=0
        HDFS: Number of bytes read=141
        HDFS: Number of bytes written=26
        HDFS: Number of read operations=6
        HDFS: Number of large read operations=0
        HDFS: Number of write operations=2
    Job Counters 
        Launched map tasks=1
        Launched reduce tasks=1
        Data-local map tasks=1
        Total time spent by all maps in occupied slots (ms)=3032
        Total time spent by all reduces in occupied slots (ms)=3133
        Total time spent by all map tasks (ms)=3032
        Total time spent by all reduce tasks (ms)=3133
        Total vcore-milliseconds taken by all map tasks=3032
        Total vcore-milliseconds taken by all reduce tasks=3133
        Total megabyte-milliseconds taken by all map tasks=3104768
        Total megabyte-milliseconds taken by all reduce tasks=3208192
    Map-Reduce Framework
        Map input records=4
        Map output records=4
        Map output bytes=43
        Map output materialized bytes=44
        Input split bytes=114
        Combine input records=4
        Combine output records=3
        Reduce input groups=3
        Reduce shuffle bytes=44
        Reduce input records=3
        Reduce output records=3
        Spilled Records=6
        Shuffled Maps =1
        Failed Shuffles=0
        Merged Map outputs=1
        GC time elapsed (ms)=134
        CPU time spent (ms)=0
        Physical memory (bytes) snapshot=0
        Virtual memory (bytes) snapshot=0
        Total committed heap usage (bytes)=306708480
    Shuffle Errors
        BAD_ID=0
        CONNECTION=0
        IO_ERROR=0
        WRONG_LENGTH=0
        WRONG_MAP=0
        WRONG_REDUCE=0
    File Input Format Counters 
        Bytes Read=27
    File Output Format Counters 
        Bytes Written=26

再執行看看執行結果:

hadoop fs -ls /user/chenxun/output

這時候你看到:

Found 2 items
-rw-r--r--   1 chenxun supergroup          0 2017-10-14 01:55 /user/chenxun/output/_SUCCESS
-rw-r--r--   1 chenxun supergroup         26 2017-10-14 01:55 /user/chenxun/output/part-r-00000

接下來真正看一下你的單詞統計結果:

hadoop fs -cat  /user/chenxun/output/part-r-00000

因為我的file.txt檔案內容是

[chenxun@chen.local 11:13 ~/software/hadoop-2.8.1]$cat file.txt 
hadoop
chenxun
hadoop
chen

所有統計結果如下:

chen    1
chenxun 1
hadoop  2

Exit code: 127 Stack trace: ExitCodeException exitCode=127: 的錯誤解決方法如下:

六、如果你在第五步執行過程出現錯誤:Exit code: 127 Stack trace: ExitCodeException exitCode=127: 不要著急這裡瞭解決方法:

今天在Mac配置偽分散式環境。部署完畢後,執行MapReduce程式,程式執行剛開始沒有問題, 但是到啟動Map Task時,就報錯誤,報exitCode: 127錯誤。
錯誤日誌如下:
15/04/06 00:08:01 INFO mapreduce.Job: Job job_1428250045856_0002 failed with state FAILED due to: Application application_1428250045856_0002 failed 2 times due to AM Container for appattempt_1428250045856_0002_000002 exited with  exitCode: 127 due to: Exception from container-launch: org.apache.hadoop.util.Shell$ExitCodeException: 
org.apache.hadoop.util.Shell$ExitCodeException: 
at org.apache.hadoop.util.Shell.runCommand(Shell.java:505)
at org.apache.hadoop.util.Shell.run(Shell.java:418)
at org.apache.hadoop.util.Shell$ShellCommandExecutor.execute(Shell.java:650)

解決辦法:
1 從錯誤日誌上看應該是某個Shell命令在Mac上沒有找到。
2 檢視每個程式的日誌:hadoop-2.3.0/logs/userlogs/application_1428247759749_0002/container_1428247759749_0002_02_000001中的錯誤日誌:
                     /bin/bash: /bin/java: No such file or directory
可以看到:/bin/java 的命令沒有找到。
          建立一個軟連結,連結到java程式即可。

上文中的日誌檔案和異常中的檔名對應不上,這個是我編寫時的問題, 實際可以根據日誌中的檔名找到具體的錯誤檔案。但是整個流程,是這個。

是因為Hadoop預設檢查/bin/java路徑下的java,可是Mac的Java不是裝這裡的,它的路徑是/usr/bin/java。你不信啊,那你輸入命令$ /usr/bin/java -version, 看看是不是會出現類似以下的資訊:

Java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java Hotspot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
如果對了的話,那麼下一步就是在/bin/java建立一個快捷方式,讓Hadoop讀到/usr/bin/java裡的資訊:
$ sudo ln -s /usr/bin/java /bin/java

再輸入命令$ /bin/java -version驗證一遍,顯示:
引用
Java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java Hotspot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

這個時候你在執行sudo ln -s /usr/bin/java /bin/java出現問題:ln: /bin/java: Operation not permitted


這是mac系統的原因不是你的錯。
這是因為蘋果在OS X 10.11中引入的SIP特性使得即使加了sudo(也就是具有root許可權)也無法修改系統級的目錄,其中就包括了/usr/bin。要解決這個問題有兩種做法:一種是比較不安全的就是關閉SIP,也就是rootless特性;另一種是將本要連結到/usr/bin下的改連結到/usr/local/bin下就好了。
廢話不多說上解決方法:

重啟按住 Command+R,進入恢復模式,開啟Terminal

csrutil disable

重啟即可。如果要恢復預設,最好這麼做,那麼

csrutil enable

參考連結:http://www.jianshu.com/p/22b89f19afd6

相關文章