| 66 | |
| 67 | = Hadoop Hands-on Labs (2) = |
| 68 | == MapReduce 程式設計練習 == |
| 69 | |
| 70 | 1. 執行 Wordcount 範例 |
| 71 | {{{ |
| 72 | ~/hadoop-0.18.2$ bin/hadoop fs -put conf conf |
| 73 | ~/hadoop-0.18.2$ bin/hadoop fs -ls |
| 74 | Found 1 items |
| 75 | drwxr-xr-x - jazz supergroup 0 2008-11-05 19:34 /user/jazz/conf |
| 76 | ~/hadoop-0.18.2$ bin/hadoop jar /home/jazz/hadoop-0.18.2/hadoop-0.18.2-examples.jar wordcount |
| 77 | ERROR: Wrong number of parameters: 0 instead of 2. |
| 78 | wordcount [-m <maps>] [-r <reduces>] <input> <output> |
| 79 | Generic options supported are |
| 80 | -conf <configuration file> specify an application configuration file |
| 81 | -D <property=value> use value for given property |
| 82 | -fs <local|namenode:port> specify a namenode |
| 83 | -jt <local|jobtracker:port> specify a job tracker |
| 84 | -files <comma separated list of files> specify comma separated files to be copied to the map reduce cluster |
| 85 | -libjars <comma separated list of jars> specify comma separated jar files to include in the classpath. |
| 86 | -archives <comma separated list of archives> specify comma separated archives to be unarchived on the compute machines. |
| 87 | |
| 88 | The general command line syntax is |
| 89 | bin/hadoop command [genericOptions] [commandOptions] |
| 90 | ~/hadoop-0.18.2$ bin/hadoop jar /home/jazz/hadoop-0.18.2/hadoop-0.18.2-examples.jar wordcount conf output |
| 91 | }}} |
| 92 | |
| 93 | * Wordcount 的原始碼 |
| 94 | {{{ |
| 95 | ~/hadoop-0.18.2/$ vi src/examples/org/apache/hadoop/examples/WordCount.java |
| 96 | }}} |
| 97 | * 示範 Wordcount.java 如何除錯: 故意加一段 IOException 讓 mapper 產生錯誤 |
| 98 | {{{ |
| 99 | #!diff |
| 100 | throw new IOException("SUICIDE"); |
| 101 | }}} |
| 102 | * 使用 ant 重新編譯 hadoop-0.18.2-examples.jar |
| 103 | {{{ |
| 104 | ~/hadoop-0.18.2/$ ant examples |
| 105 | }}} |
| 106 | * 原理解說: |
| 107 | * 因為 key 是 Text 型態,因此要設定 !OutputKeyClass 為 Text |
| 108 | {{{ |
| 109 | conf.setOutputKeyClass(Text.class); |
| 110 | }}} |
| 111 | * 詳細說明在官方文件: http://hadoop.apache.org/core/docs/r0.18.2/mapred_tutorial.html |
| 112 | * Input and Output Formats |
| 113 | * 通常輸入跟輸出都是純文字格式,因此預設是 !TextInputFormat 跟 !TextOutputFormat |
| 114 | * 但如果輸入跟輸出是二進位格式,那就必須使用 !SequenceFileInputFormat 跟 !SequenceFileOutputFormat 當作 Map/Reduce 的 !KeyClass |
| 115 | * Input -> !InputSplit -> !RecordReader |
| 116 | * Hadoop 會將輸入切成很多塊 !InputSplit, 但是可能會遇到要處理的資料在另一塊 !InputSplit 的困擾 |
| 117 | * Reducer 個數建議為 0.95 * num_nodes * mapred.tasktracker.tasks.maximum 這裡的 0.95 是為了預留 5% 的時間來處理其他 node 故障所造成的影響。 |
| 118 | * 不會寫 Java 程式的開發者怎麼辦? |
| 119 | * 方法一: 使用 hadoop-stream |
| 120 | * 目前處理 binary 的能力仍有限,因此建議使用在純文字處理上。 |
| 121 | * 如果保留原始 hadoop-site.xml 的 configure 描述(沒有加任何 <property>),預設是使用 local filesystem |
| 122 | {{{ |
| 123 | ~/hadoop-0.18.2$ cat conf/hadoop-site.xml |
| 124 | <?xml version="1.0"?> |
| 125 | <?xml-stylesheet type="text/xsl" href="configuration.xsl"?> |
| 126 | |
| 127 | <!-- Put site-specific property overrides in this file. --> |
| 128 | |
| 129 | <configuration> |
| 130 | |
| 131 | </configuration> |
| 132 | |
| 133 | ~/hadoop-0.18.2$ echo "sed -e \"s/ /\n/g\" | grep ." > streamingMapper.sh |
| 134 | ~/hadoop-0.18.2$ echo "uniq -c | awk '{print $2 \"\t\" $1}'" > streamingReducer.sh |
| 135 | ~/hadoop-0.18.2$ bin/hadoop jar ./contrib/streaming/hadoop-0.18.2-streaming.jar -input conf -output out1 -mapper `pwd`/streamingMapper.sh -reducer `pwd`/streamingReducer.sh |
| 136 | }}} |
| 137 | * 如果有結合 DFS 的話,那必須透過 -file 指令把 mapper 跟 reducer 的程式打包進 DFS |
| 138 | {{{ |
| 139 | }}} |
| 140 | * 更深入的 streaming 解說文件在 http://hadoop.apache.org/core/docs/r0.18.2/streaming.html |
| 141 | * 方法二: 使用 Pipes (C++ native support of Hadoop) |
| 142 | * 目前官方只支援 Java 跟 C++ 語言來撰寫 MapReduce 程式 |
| 143 | * 由於 JobTracker 還是 Java 寫的,因此必須在 Java 程式(Ex. run())裡面告訴 JobTracker 怎麼連結 C++ 的執行檔 |
| 144 | * 方法三: 使用 Pig |
| 145 | * Pig 是第三種不用學會寫 Java 而改用類似 SQL 語法的方式,Pig 會幫忙產生 MapReduce 程式(java class),然後幫忙執行 |
| 146 | * Taiwan Hadoop User Group 所提供的 PHP + Hadoop Streaming 範例 |
| 147 | * [http://www.hadoop.tw/2008/09/php-hadoop.html 用 "單機" 跟 "PHP" 開發 Hadoop 程式] |
| 148 | |
| 149 | == 大量部署 Hadoop 的方法 == |
| 150 | |
| 151 | * 參閱官方文件: http://hadoop.apache.org/core/docs/r0.18.2/cluster_setup.html |