| 1 | [[PageOutline]] |
| 2 | |
| 3 | ◢ <[wiki:NTUT160625/Lab7 實作七]> | <[wiki:NTUT160625 回課程大綱]> ▲ | <[wiki:NTUT160625/Lab9 實作九]> ◣ |
| 4 | |
| 5 | {{{ |
| 6 | #!text |
| 7 | 以下練習,請連線至 hdp01.3du.me 操作。底下的 userXX 等於您的用戶名稱。 |
| 8 | 以下練習,請連線至 hdp02.3du.me 操作。底下的 userXX 等於您的用戶名稱。 |
| 9 | 以下練習,請連線至 hdp03.3du.me 操作。底下的 userXX 等於您的用戶名稱。 |
| 10 | 以下練習,請連線至 hdp04.3du.me 操作。底下的 userXX 等於您的用戶名稱。 |
| 11 | }}} |
| 12 | |
| 13 | = 實作八 Lab 8 = |
| 14 | |
| 15 | {{{ |
| 16 | #!html |
| 17 | <div style="text-align: center;"><big style="font-weight: bold;"><big>HDFS、MapReduce 與 Hadoop Streaming 觀念驗證<br/>Running Hadoop Streaming with HDFS and MapReduce</big></big></div> |
| 18 | }}} |
| 19 | |
| 20 | == 準備輸入資料集 Input Dataset == |
| 21 | |
| 22 | * 首先準備輸入,包含兩部份:(1) 供 Velvet 計算的 *.fa 檔案,這裡為了方便示範起見,採用 test_long.fa 當範本,並複製 99 份不同檔名,當輸入 (2) 供 Mapper 運算的輸入檔案(內含 HDFS 的檔名路徑) |
| 23 | {{{ |
| 24 | ~$ echo "test" > test_long.fa |
| 25 | ~$ for ((i=1;i<100;i++)); do hadoop fs -put test_long.fa input-$i.fa; done |
| 26 | ~$ for ((i=1;i<20;i++)); do echo /user/$(whoami)/input-$i.fa; done > split-01.txt |
| 27 | ~$ for ((i=20;i<40;i++)); do echo /user/$(whoami)/input-$i.fa; done > split-02.txt |
| 28 | ~$ for ((i=40;i<60;i++)); do echo /user/$(whoami)/input-$i.fa; done > split-03.txt |
| 29 | ~$ for ((i=60;i<80;i++)); do echo /user/$(whoami)/input-$i.fa; done > split-04.txt |
| 30 | ~$ for ((i=80;i<100;i++)); do echo /user/$(whoami)/input-$i.fa; done > split-05.txt |
| 31 | ~$ hadoop fs -mkdir lab10_input |
| 32 | ~$ hadoop fs -put split-0* lab10_input |
| 33 | }}} |
| 34 | * 檢查輸入檔案 |
| 35 | {{{ |
| 36 | ~$ hadoop fs -ls |
| 37 | ~$ hadoop fs -ls lab10_input |
| 38 | }}} |
| 39 | |
| 40 | == 觀察 Hadoop Streaming 執行身份與工作目錄 == |
| 41 | |
| 42 | * 撰寫 testmapper.sh |
| 43 | {{{ |
| 44 | #!sh |
| 45 | #!/bin/bash |
| 46 | |
| 47 | id="h998" |
| 48 | mkdir -p /tmp/$id |
| 49 | host=`hostname` |
| 50 | pwd=`pwd` |
| 51 | uid=`whoami` |
| 52 | |
| 53 | while read line; do |
| 54 | input=$line |
| 55 | filename=`basename $input` |
| 56 | echo "$uid@$host:$pwd> hadoop fs -get $input /tmp/$id/$filename" |
| 57 | echo "$uid@$host:$pwd> velveth output-$filename 17 -fasta -short /tmp/$id/$filename" |
| 58 | echo "$uid@$host:$pwd> hadoop fs -put output-$filename ." |
| 59 | done |
| 60 | rm -rf /tmp/$id |
| 61 | }}} |
| 62 | * 接著,讓我們在本地端先驗證一下 testmapper.sh 的運作 |
| 63 | {{{ |
| 64 | ~$ head -n 2 split-01.txt > split-00.txt |
| 65 | ~$ cat > testmapper.sh << EOF |
| 66 | #!/bin/bash |
| 67 | |
| 68 | id="`whoami`" |
| 69 | mkdir -p /tmp/\$id |
| 70 | host=\`hostname\` |
| 71 | pwd=\`pwd\` |
| 72 | uid=\`whoami\` |
| 73 | |
| 74 | while read line; do |
| 75 | input=\$line |
| 76 | filename=\`basename \$input\` |
| 77 | echo "\$uid@\$host:\$pwd> hadoop fs -get \$input /tmp/\$id/\$filename" |
| 78 | echo "\$uid@\$host:\$pwd> velveth output-\$filename 17 -fasta -short /tmp/\$id/\$filename" |
| 79 | echo "\$uid@\$host:\$pwd> hadoop fs -put output-\$filename ." |
| 80 | done |
| 81 | rm -rf /tmp/\$id |
| 82 | EOF |
| 83 | ~$ chmod a+x testmapper.sh |
| 84 | ~$ cat split-00.txt | ./testmapper.sh |
| 85 | user00@hdp01.3du.me:/home/user00> hadoop fs -get /user/user00/input-1.fa /tmp/user00/input-1.fa |
| 86 | user00@hdp01.3du.me:/home/user00> velveth output-input-1.fa 17 -fasta -short /tmp/user00/input-1.fa |
| 87 | user00@hdp01.3du.me:/home/user00> hadoop fs -put output-input-1.fa . |
| 88 | user00@hdp01.3du.me:/home/user00> hadoop fs -get /user/user00/input-2.fa /tmp/user00/input-2.fa |
| 89 | user00@hdp01.3du.me:/home/user00> velveth output-input-2.fa 17 -fasta -short /tmp/user00/input-2.fa |
| 90 | user00@hdp01.3du.me:/home/user00> hadoop fs -put output-input-2.fa . |
| 91 | }}} |
| 92 | * 讓我們用 Hadoop Streaming 的方式來執行 testmapper.sh |
| 93 | {{{ |
| 94 | ~$ hadoop jar hadoop-streaming.jar -input lab10_input -output lab10_out1 -mapper testmapper.sh -file testmapper.sh -numReduceTasks 0 |
| 95 | }}} |
| 96 | * 觀察 lab10_out1 的結果,看與本機執行有何不同呢? |
| 97 | {{{ |
| 98 | ~$ hadoop fs -cat /user/$(whoami)/lab10_out1/part-00000 | head |
| 99 | root@hdp05.3du.me:/opt/hadoop/var/hadoop-root/mapred/local/taskTracker/user00/jobcache/job_201411281523_0038/attempt_201411281523_0038_m_000000_0/work> hadoop fs -get /user/user00/input-20.fa /tmp/user00/input-20.fa |
| 100 | root@hdp05.3du.me:/opt/hadoop/var/hadoop-root/mapred/local/taskTracker/user00/jobcache/job_201411281523_0038/attempt_201411281523_0038_m_000000_0/work> velveth output-input-20.fa 17 -fasta -short /tmp/user00/input-20.fa |
| 101 | root@hdp05.3du.me:/opt/hadoop/var/hadoop-root/mapred/local/taskTracker/user00/jobcache/job_201411281523_0038/attempt_201411281523_0038_m_000000_0/work> hadoop fs -put output-input-20.fa . |
| 102 | root@hdp05.3du.me:/opt/hadoop/var/hadoop-root/mapred/local/taskTracker/user00/jobcache/job_201411281523_0038/attempt_201411281523_0038_m_000000_0/work> hadoop fs -get /user/user00/input-21.fa /tmp/user00/input-21.fa |
| 103 | root@hdp05.3du.me:/opt/hadoop/var/hadoop-root/mapred/local/taskTracker/user00/jobcache/job_201411281523_0038/attempt_201411281523_0038_m_000000_0/work> velveth output-input-21.fa 17 -fasta -short /tmp/user00/input-21.fa |
| 104 | root@hdp05.3du.me:/opt/hadoop/var/hadoop-root/mapred/local/taskTracker/user00/jobcache/job_201411281523_0038/attempt_201411281523_0038_m_000000_0/work> hadoop fs -put output-input-21.fa . |
| 105 | root@hdp05.3du.me:/opt/hadoop/var/hadoop-root/mapred/local/taskTracker/user00/jobcache/job_201411281523_0038/attempt_201411281523_0038_m_000000_0/work> hadoop fs -get /user/user00/input-22.fa /tmp/user00/input-22.fa |
| 106 | root@hdp05.3du.me:/opt/hadoop/var/hadoop-root/mapred/local/taskTracker/user00/jobcache/job_201411281523_0038/attempt_201411281523_0038_m_000000_0/work> velveth output-input-22.fa 17 -fasta -short /tmp/user00/input-22.fa |
| 107 | root@hdp05.3du.me:/opt/hadoop/var/hadoop-root/mapred/local/taskTracker/user00/jobcache/job_201411281523_0038/attempt_201411281523_0038_m_000000_0/work> hadoop fs -put output-input-22.fa . |
| 108 | }}} |
| 109 | |
| 110 | == 實作透過 Hadoop Streaming 執行 99 組 velvet 運算 == |
| 111 | |
| 112 | * 撰寫 mapper.sh |
| 113 | {{{ |
| 114 | #!sh |
| 115 | #!/bin/bash |
| 116 | |
| 117 | id="h998" |
| 118 | mkdir -p /tmp/$id |
| 119 | host=`hostname` |
| 120 | pwd=`pwd` |
| 121 | uid=`whoami` |
| 122 | |
| 123 | while read line; do |
| 124 | input=$line |
| 125 | filename=`basename $input` |
| 126 | echo "$uid@$host> hadoop fs -get $input /tmp/$id/$filename" |
| 127 | hadoop fs -get $input /tmp/$id/$filename |
| 128 | echo "$uid@$host> velveth output-$filename 17 -fasta -short /tmp/$id/$filename" |
| 129 | velveth output-$filename 17 -fasta -short /tmp/$id/$filename |
| 130 | echo "$uid@$host> hadoop fs -put output-$filename /user/$id/." |
| 131 | hadoop fs -put output-$filename /user/$id/. |
| 132 | hadoop fs -chown $id /user/$id/output-$filename |
| 133 | done |
| 134 | rm -rf /tmp/$id |
| 135 | }}} |
| 136 | * 於本機測試 mapper.sh |
| 137 | {{{ |
| 138 | ~$ cat > mapper.sh << EOF |
| 139 | #!/bin/bash |
| 140 | |
| 141 | id="`whoami`" |
| 142 | mkdir -p /tmp/\$id |
| 143 | host=\`hostname\` |
| 144 | pwd=\`pwd\` |
| 145 | uid=\`whoami\` |
| 146 | |
| 147 | while read line; do |
| 148 | input=\$line |
| 149 | filename=\`basename \$input\` |
| 150 | echo "\$uid@\$host> hadoop fs -get \$input /tmp/\$id/\$filename" |
| 151 | hadoop fs -get \$input /tmp/\$id/\$filename |
| 152 | echo "\$uid@\$host> velveth output-\$filename 17 -fasta -short /tmp/\$id/\$filename" |
| 153 | velveth output-\$filename 17 -fasta -short /tmp/\$id/\$filename |
| 154 | echo "\$uid@\$host> hadoop fs -put output-\$filename /user/\$id/." |
| 155 | hadoop fs -put output-\$filename /user/\$id/. |
| 156 | hadoop fs -chown \$id /user/\$id/output-\$filename |
| 157 | done |
| 158 | rm -rf /tmp/\$id |
| 159 | EOF |
| 160 | ~$ chmod a+x mapper.sh |
| 161 | ~$ cat split-00.txt | ./mapper.sh |
| 162 | ~$ hadoop fs -rmr output-* |
| 163 | ~$ rm -rf output-sample-* |
| 164 | }}} |
| 165 | * 接著用 hadoop streaming 來執行 |
| 166 | {{{ |
| 167 | ~$ hadoop jar hadoop-streaming.jar -input lab10_input -output lab10_out2 -mapper mapper.sh -file mapper.sh -numReduceTasks 0 |
| 168 | }}} |