-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhadoop-streaming-practical-introduction-mapreduce-python.html
More file actions
275 lines (217 loc) · 8.67 KB
/
hadoop-streaming-practical-introduction-mapreduce-python.html
File metadata and controls
275 lines (217 loc) · 8.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<!DOCTYPE html>
<html lang="en" itemscope itemtype="http://schema.org/Article">
<head>
<title>Hadoop Streaming. Practical introduction to MapReduce with Python</title>
<meta charset="utf-8">
<meta property="og:title" content="Hadoop Streaming. Practical introduction to MapReduce with Python">
<meta property="og:site_name" content="Modesto Mas | Blog">
<meta property="og:image" content="https://mmas.github.io/images/profile.jpg">
<meta property="og:image:width" content="200">
<meta property="og:image:height" content="200">
<meta property="og:url" content="https://mmas.github.io/hadoop-streaming-practical-introduction-mapreduce-python">
<meta property="og:locale" content="en_GB">
<meta name="twitter:image" content="https://mmas.github.io/images/profile.jpg">
<meta name="twitter:url" content="https://mmas.github.io/hadoop-streaming-practical-introduction-mapreduce-python">
<meta name="twitter:card" content="summary">
<meta name="twitter:domain" content="mmas.github.io">
<meta name="twitter:title" content="Hadoop Streaming. Practical introduction to MapReduce with Python">
<meta name="description" content="Apache Hadoop is a framework for distributed storage and processing. I'm not going to explain how Hadoop modules work or to describe the Hadoop ecosys...">
<meta name="twitter:description" content="Apache Hadoop is a framework for distributed storage and processing. I'm not going to explain how Hadoop modules work or to describe the Hadoop ecosys...">
<meta property="og:description" content="Apache Hadoop is a framework for distributed storage and processing. I'm not going to explain how Hadoop modules work or to describe the Hadoop ecosys...">
<meta name="keywords" content="data-processing,hadoop,mapreduce,python">
<meta property="og:type" content="blog">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta property="og:type" content="article">
<meta property="article:author" content="https://github.com/mmas">
<meta property="article:section" content="data-processing">
<meta property="article:tag" content="data-processing,hadoop,mapreduce,python">
<meta property="article:published_time" content="2015-09-11">
<meta property="article:modified_time" content="2015-09-11">
<link rel="stylesheet" type="text/css" href="/css/main.css">
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
CommonHTML: {
scale: 93,
showMathMenu: false
},
tex2jax: {
"inlineMath": [["$","$"], ["\\(","\\)"]]
}
});
</script>
<script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"></script>
</head>
<body class="entry-detail">
<header>
<div>
<img src="https://mmas.github.io/images/profile.jpg">
<a class="brand" href="/">Modesto Mas</a>
<span>Data/Python/DevOps Engineer</span>
<nav>
<ul>
<li><a href="/tags">Tags</a></li>
<li><a href="https://github.com/mmas/mmas.github.io/issues" target="_blank">Issues</a></li>
</ul>
</nav>
</div>
</header>
<section id="content" role="main">
<article>
<header>
<h1><a href="/hadoop-streaming-practical-introduction-mapreduce-python">Hadoop Streaming. Practical introduction to MapReduce with Python</a></h1>
<time datetime="2015-09-11">Sep 11, 2015</time>
<a class="tag" href="/tags?tag=data-processing">data-processing</a>
<a class="tag" href="/tags?tag=hadoop">hadoop</a>
<a class="tag" href="/tags?tag=mapreduce">mapreduce</a>
<a class="tag" href="/tags?tag=python">python</a>
</header>
<aside id="article-nav"></aside>
<section class="body">
<p><a target="_blank" href="http://hadoop.apache.org/">Apache Hadoop</a> is a framework for distributed storage and processing. I'm not going to explain how Hadoop modules work or to describe the Hadoop ecosystem, since there are a lot of really good resources that you can easily find in the form of blog entries, papers, books or videos.</p>
<p>Hadoop is written in Java, however, for these two MapReduce examples I'm going to use Python for the mapper and reducer functions. You can use any language that can read and write standard input and outputs for the Hadoop Streaming.</p>
<h2>Maximum temperature</h2>
<p>Obtain the maximum temperature of each day of 1998.</p>
<p>I'm going to use some weather data from <a target="_blank" href="http://www.ncdc.noaa.gov/">NCDC</a>. Without any reason I've chosen the daily records of 1998. A month is represented per file and I'm going to focus on the date (second column) and maximum temperature (third column) to get the maximum temperature according to the different weather stations (wban - first column).</p>
<p>You can download the data at <a target="_blank" href="http://www.ncdc.noaa.gov/orders/qclcd/">http://www.ncdc.noaa.gov/orders/qclcd/</a>. </p>
<p>Create sample data to test:</p>
<pre>% head -n 100 199801daily.txt >> sample.txt
</pre>
<p>Set executable permissions:</p>
<pre>% chmod +x mapper.py reducer.py
</pre>
<p>Mapper function:</p>
<pre>#!/usr/bin/env python
import sys
for line in sys.stdin:
if not line.strip() or line.startswith('Wban'):
continue
_, k, v = line[:17].split(',', 2)
try:
v = int(v)
except ValueError:
continue
print '%s\t%s' % (k, v)
</pre>
<p>Reducer function:</p>
<pre>#!/usr/bin/env python
import sys
last_k = None
max_v = 0
for line in sys.stdin:
k, v = line.strip().split('\t')
v = int(v)
if last_k == k:
max_v = max(max_v, v)
else:
if last_k:
print '%s\t%s' % (last_k, max_v)
last_k = k
max_v = v
if last_k == k:
print '%s\t%s' % (last_k, max_v)
</pre>
<p>Test:</p>
<pre>% cat sample.txt | ./mapper.py | sort | ./reducer.py
</pre>
<p>Copy data from local filesystem to HDFS:</p>
<pre>% hadoop fs -put 1998*.txt /user/$(whoami)/input/
</pre>
<p>Map and reduce with Hadoop:</p>
<pre>% hadoop jar $HADOOP_PREFIX/share/hadoop/tools/lib/hadoop-streaming-*.jar \
-input input/1998*.txt \
-output output \
-mapper /opt/hadoop/maximum_temperature/mapper.py \
-reducer /opt/hadoop/maximum_temperature/reducer.py
</pre>
<p>See the results:</p>
<pre>% hadoop fs -cat output/*
[...]
19981222 94
19981223 92
19981224 91
19981225 91
19981226 90
19981227 91
19981228 90
19981229 90
19981230 89
19981231 89
</pre>
<h2>Word counter</h2>
<p>Count the words of a book.</p>
<p>From a book in text file I'm going to count the times a word is repeated. This is a simple example, so some issues won't be covered (like word contractions).</p>
<p>The book I've chosen is <em>The Trial </em>and you can download it from <a target="_blank" href="http://www.gutenberg.org/ebooks/7849">http://www.gutenberg.org/ebooks/7849</a></p>
<p>Create sample data:</p>
<pre>% head -n 100 the_trial__franz_kafka.txt >> sample.txt
</pre>
<p>Set executable permissions:</p>
<pre>% chmod +x mapper.py reducer.py
</pre>
<p>Mapper function:</p>
<pre>#!/usr/bin/env python
import re
import sys
started = False
for line in sys.stdin:
if started:
if line.startswith('*** END OF THIS PROJECT'):
break
# Filter out some punctuation marks and set to lowercase.
line = re.sub(r'["?!.,;:()-]', '', line).strip().lower()
for word in line.split():
print '%s\t1' % word
elif line.startswith('*** START OF THIS PROJECT'):
started = True
</pre>
<p>Reducer function:</p>
<pre>#!/usr/bin/env python
import sys
last_k = None
last_v = 0
for line in sys.stdin:
k, v = line.strip().split('\t')
v = int(v)
if last_k == k:
last_v += v
else:
if last_k:
print '%s\t%s' % (last_k, last_v)
last_k = k
last_v = v
if last_k == k:
print '%s\t%s' % (last_k, last_v)
</pre>
<p>Test:</p>
<pre>% cat sample.txt | ./mapper.py | sort | ./reducer.py
</pre>
<p>Copy data from local filesystem to HDFS:</p>
<pre>% hadoop fs -put the_trial__franz_kafka.txt /user/$(whoami)/input/
</pre>
<p>Map and reduce with Hadoop:</p>
<pre>% hadoop jar $HADOOP_PREFIX/share/hadoop/tools/lib/hadoop-streaming-*.jar \
-input input/the_trial__franz_kafka.txt \
-output output \
-mapper /opt/hadoop/word_count/mapper.py \
-reducer /opt/hadoop/word_count/reducer.py
</pre>
<p>See the results:</p>
<pre>% hadoop fs -cat output/*
[...]
you'll 12
you're 67
you've 37
young 30
younger 1
your 94
yours 10
yourself 23
yourselves 3
youth 4
</pre>
</section>
</article>
</section>
<footer></footer>
<script type="text/javascript" src="/js/article.js"></script>
</body>
</html>