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