| 1 | [[PageOutline]] |
| 2 | |
| 3 | ◢ <[wiki:NCTU110329/Lab5 實作五]> | <[wiki:NCTU110329 回課程大綱]> ▲ | <[wiki:NCTU110329/Lab7 實作七]> ◣ |
| 4 | |
| 5 | = 實作六 = |
| 6 | |
| 7 | {{{ |
| 8 | #!html |
| 9 | <div style="text-align: center;"><big style="font-weight: bold;"><big>MapReduce 程式編譯<br/>Compiling Hadoop MapReduce Java Program</big></big></div> |
| 10 | }}} |
| 11 | |
| 12 | = Practice 1 : Word Count (Basic) = |
| 13 | |
| 14 | * 上傳內容到 HDFS 內[[BR]]upload data to HDFS |
| 15 | |
| 16 | {{{ |
| 17 | $ cd /opt/hadoop |
| 18 | $ mkdir lab4_input |
| 19 | $ echo "I like NCHC Cloud Course." > lab4_input/input1 |
| 20 | $ echo "I like nchc Cloud Course, and we enjoy this course." > lab4_input/input2 |
| 21 | $ bin/hadoop fs -put lab4_input lab4_input |
| 22 | $ bin/hadoop fs -ls lab4_input |
| 23 | }}} |
| 24 | |
| 25 | * 下載 [http://secuse.nchc.org.tw/class/WordCount.java WordCount.java] 並存到/opt/hadoop;[[BR]]Download [http://trac.nchc.org.tw/cloud/raw-attachment/wiki/WordCount/WordCount.java WordCount.java] and save to /opt/hadoop |
| 26 | {{{ |
| 27 | ~$ cd /opt/hadoop |
| 28 | /opt/hadoop$ wget http://trac.nchc.org.tw/cloud/raw-attachment/wiki/WordCount/WordCount.java |
| 29 | }}} |
| 30 | |
| 31 | * 運作程式[[BR]]Compile WordCount.java and run it by '''hadoop jar''' command |
| 32 | |
| 33 | {{{ |
| 34 | $ mkdir MyJava |
| 35 | $ javac -classpath hadoop-*-core.jar -d MyJava WordCount.java |
| 36 | $ jar -cvf wordcount.jar -C MyJava . |
| 37 | $ bin/hadoop jar wordcount.jar WordCount lab4_input/ lab4_out1/ |
| 38 | $ bin/hadoop fs -cat lab4_out1/part-00000 |
| 39 | }}} |
| 40 | |
| 41 | * lab4_out1 執行結果 [[BR]]You should see results like this : |
| 42 | {{{ |
| 43 | #!text |
| 44 | Cloud 2 |
| 45 | Course, 1 |
| 46 | Course. 1 |
| 47 | I 2 |
| 48 | NCHC 1 |
| 49 | and 1 |
| 50 | course. 1 |
| 51 | enjoy 1 |
| 52 | like 2 |
| 53 | nchc 1 |
| 54 | this 1 |
| 55 | we 1 |
| 56 | }}} |
| 57 | ----- |
| 58 | |
| 59 | = Practice 2 : Word Count (Advanced) = |
| 60 | |
| 61 | {{{ |
| 62 | $ echo "\." >pattern.txt && echo "\," >>pattern.txt |
| 63 | $ bin/hadoop fs -put pattern.txt ./ |
| 64 | $ mkdir MyJava2 |
| 65 | }}} |
| 66 | |
| 67 | |
| 68 | * 下載 [raw-attachment:wiki:Hadoop_Lab4:WordCount2.java WordCount2.java] 並存到/opt/hadoop;[[BR]]Download [raw-attachment:wiki:Hadoop_Lab4:WordCount2.java WordCount2.java] to /opt/hadoop |
| 69 | {{{ |
| 70 | ~$ cd /opt/hadoop |
| 71 | /opt/hadoop$ wget http://trac.nchc.org.tw/cloud/raw-attachment/wiki/Hadoop_Lab4/WordCount2.java |
| 72 | }}} |
| 73 | |
| 74 | {{{ |
| 75 | $ javac -classpath hadoop-*-core.jar -d MyJava2 WordCount2.java |
| 76 | $ jar -cvf wordcount2.jar -C MyJava2 . |
| 77 | $ bin/hadoop jar wordcount2.jar WordCount2 lab4_input lab4_out2 -skip pattern.txt |
| 78 | $ bin/hadoop fs -cat lab4_out2/part-00000 |
| 79 | }}} |
| 80 | |
| 81 | * lab4_out2 執行結果[[BR]]You should see results like this: |
| 82 | {{{ |
| 83 | #!text |
| 84 | Cloud 2 |
| 85 | Course 2 |
| 86 | I 2 |
| 87 | NCHC 1 |
| 88 | and 1 |
| 89 | course 1 |
| 90 | enjoy 1 |
| 91 | like 2 |
| 92 | nchc 1 |
| 93 | this 1 |
| 94 | we 1 |
| 95 | }}} |
| 96 | |
| 97 | * Let's given case insensitive and ignore pattern for this example |
| 98 | {{{ |
| 99 | /opt/hadoop$ bin/hadoop jar wordcount2.jar WordCount2 -Dwordcount.case.sensitive=false lab4_input lab4_out3 -skip pattern.txt |
| 100 | /opt/hadoop$ bin/hadoop fs -cat lab4_out3/part-00000 |
| 101 | }}} |
| 102 | |
| 103 | * lab4_out3 執行結果[[BR]]You should see results like this: |
| 104 | {{{ |
| 105 | #!text |
| 106 | and 1 |
| 107 | cloud 2 |
| 108 | course 3 |
| 109 | enjoy 1 |
| 110 | i 2 |
| 111 | like 2 |
| 112 | nchc 2 |
| 113 | this 1 |
| 114 | we 1 |
| 115 | }}} |