Changes between Initial Version and Version 1 of III140503/Lab10


Ignore:
Timestamp:
May 4, 2014, 1:20:04 PM (8 years ago)
Author:
jazz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • III140503/Lab10

    v1 v1  
     1[[PageOutline]]
     2
     3◢ <[wiki:III140503/Lab9 實作九]> | <[wiki:III140503 回課程大綱]> ▲ | <[wiki:III140503/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
     45id="h998"
     46mkdir -p /tmp/$id
     47host=`hostname`
     48pwd=`pwd`
     49uid=`whoami`
     50
     51while 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 ."
     57done
     58rm -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
     66id="`whoami`"
     67mkdir -p /tmp/\$id
     68host=\`hostname\`
     69pwd=\`pwd\`
     70uid=\`whoami\`
     71
     72while 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 ."
     78done
     79rm -rf /tmp/\$id
     80EOF
     81~$ chmod a+x testmapper.sh
     82~$ cat sample-00.txt | ./testmapper.sh
     83h998@hadoop:/home/h998> hadoop fs -get /user/h998/input-1.fa /tmp/h998/input-1.fa
     84h998@hadoop:/home/h998> velveth output-input-1.fa 17 -fasta -short /tmp/h998/input-1.fa
     85h998@hadoop:/home/h998> hadoop fs -put output-input-1.fa .
     86h998@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
     95hadoop@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   
     96hadoop@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   
     97hadoop@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
     107id="h998"
     108mkdir -p /tmp/$id
     109host=`hostname`
     110pwd=`pwd`
     111uid=`whoami`
     112
     113while 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
     123done
     124rm -rf /tmp/$id
     125}}}
     126 * 於本機測試 mapper.sh
     127{{{
     128~$ cat > mapper.sh << EOF
     129#!/bin/bash
     130
     131id="`whoami`"
     132mkdir -p /tmp/\$id
     133host=\`hostname\`
     134pwd=\`pwd\`
     135uid=\`whoami\`
     136
     137while 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
     147done
     148rm -rf /tmp/\$id
     149EOF
     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}}}