| 1 | ◢ <[wiki:III120526/Lab7 實作七]> | <[wiki:III120526 回課程大綱]> ▲ | <[wiki:III120526/Lab9 實作九]> ◣ |
| 2 | |
| 3 | = 實作八 Lab 8 = |
| 4 | [[PageOutline]] |
| 5 | {{{ |
| 6 | #!html |
| 7 | <div style="text-align: center;"><big style="font-weight: bold;"><big>在完全分散模式下編譯 MapReduce 程式<br/>Compiling Hadoop MapReduce Java Program in Hadoop Cluster</big></big></div> |
| 8 | }}} |
| 9 | |
| 10 | {{{ |
| 11 | #!text |
| 12 | 以下練習,請連線至 hadoop.nchc.org.tw 操作。底下的 hXXXX 等於您的用戶名稱。 |
| 13 | }}} |
| 14 | |
| 15 | = Practice 1 : Word Count (Basic) = |
| 16 | |
| 17 | * 上傳內容到 HDFS 內[[BR]]upload data to HDFS |
| 18 | {{{ |
| 19 | $ mkdir lab8_input |
| 20 | $ echo "I like NCTU Cloud Course." > lab8_input/input1 |
| 21 | $ echo "I like nctu Cloud Course, and we enjoy this course." > lab8_input/input2 |
| 22 | $ hadoop fs -put lab8_input lab8_input |
| 23 | $ hadoop fs -ls lab8_input |
| 24 | Found 2 items |
| 25 | -rw-r--r-- 2 hXXXX supergroup 26 2011-04-19 10:07 /user/hXXXX/lab8_input/input1 |
| 26 | -rw-r--r-- 2 hXXXX supergroup 52 2011-04-19 10:07 /user/hXXXX/lab8_input/input2 |
| 27 | }}} |
| 28 | |
| 29 | * 下載 [http://hadoop.nchc.org.tw/WordCount.java WordCount.java] 並存到家目錄;[[BR]]Download [http://hadoop.nchc.org.tw/WordCount.java WordCount.java] and save to your home directory |
| 30 | {{{ |
| 31 | ~$ wget http://hadoop.nchc.org.tw/WordCount.java |
| 32 | }}} |
| 33 | |
| 34 | * 運作程式[[BR]]Compile WordCount.java and run it by '''hadoop jar''' command |
| 35 | |
| 36 | {{{ |
| 37 | $ mkdir MyJava |
| 38 | $ ln -s /usr/lib/hadoop/hadoop-*-core.jar hadoop-core.jar |
| 39 | $ javac -classpath hadoop-core.jar -d MyJava WordCount.java |
| 40 | $ jar -cvf wordcount.jar -C MyJava . |
| 41 | $ hadoop jar wordcount.jar WordCount lab8_input/ lab8_out1/ |
| 42 | $ hadoop fs -cat lab8_out1/part-00000 |
| 43 | }}} |
| 44 | |
| 45 | * lab8_out1 執行結果 [[BR]]You should see results like this : |
| 46 | {{{ |
| 47 | #!text |
| 48 | Cloud 2 |
| 49 | Course, 1 |
| 50 | Course. 1 |
| 51 | I 2 |
| 52 | NCTU 1 |
| 53 | and 1 |
| 54 | course. 1 |
| 55 | enjoy 1 |
| 56 | like 2 |
| 57 | nctu 1 |
| 58 | this 1 |
| 59 | we 1 |
| 60 | }}} |
| 61 | ----- |
| 62 | |
| 63 | = Practice 2 : Word Count (Advanced) = |
| 64 | |
| 65 | {{{ |
| 66 | $ echo "\." >pattern.txt && echo "\," >>pattern.txt |
| 67 | $ hadoop fs -put pattern.txt . |
| 68 | $ mkdir -p MyJava2 |
| 69 | }}} |
| 70 | |
| 71 | |
| 72 | * 下載 [http://hadoop.nchc.org.tw/WordCount2.java WordCount2.java] 並存到家目錄;[[BR]]Download [http://hadoop.nchc.org.tw/WordCount2.java WordCount2.java] to home directory |
| 73 | {{{ |
| 74 | ~$ wget http://hadoop.nchc.org.tw/WordCount2.java |
| 75 | }}} |
| 76 | |
| 77 | {{{ |
| 78 | $ javac -classpath hadoop-core.jar -d MyJava2 WordCount2.java |
| 79 | $ jar -cvf wordcount2.jar -C MyJava2 . |
| 80 | $ hadoop jar wordcount2.jar WordCount2 lab8_input lab8_out2 -skip pattern.txt |
| 81 | $ hadoop fs -cat lab8_out2/part-00000 |
| 82 | }}} |
| 83 | |
| 84 | * lab8_out2 執行結果[[BR]]You should see results like this: |
| 85 | {{{ |
| 86 | #!text |
| 87 | Cloud 2 |
| 88 | Course 2 |
| 89 | I 2 |
| 90 | NCTU 1 |
| 91 | and 1 |
| 92 | course 1 |
| 93 | enjoy 1 |
| 94 | like 2 |
| 95 | nctu 1 |
| 96 | this 1 |
| 97 | we 1 |
| 98 | }}} |
| 99 | |
| 100 | * Let's given case insensitive and ignore pattern for this example |
| 101 | {{{ |
| 102 | $ hadoop jar wordcount2.jar WordCount2 -Dwordcount.case.sensitive=false lab8_input lab8_out3 -skip pattern.txt |
| 103 | $ hadoop fs -cat lab8_out3/part-00000 |
| 104 | }}} |
| 105 | |
| 106 | * lab8_out3 執行結果[[BR]]You should see results like this: |
| 107 | {{{ |
| 108 | #!text |
| 109 | and 1 |
| 110 | cloud 2 |
| 111 | course 3 |
| 112 | enjoy 1 |
| 113 | i 2 |
| 114 | like 2 |
| 115 | nctu 2 |
| 116 | this 1 |
| 117 | we 1 |
| 118 | }}} |