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