-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata-analyisis-apache-hive-practical-introduction.html
More file actions
169 lines (136 loc) · 6.47 KB
/
Copy pathdata-analyisis-apache-hive-practical-introduction.html
File metadata and controls
169 lines (136 loc) · 6.47 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
<!DOCTYPE html>
<html lang="en" itemscope itemtype="http://schema.org/Article">
<head>
<title>Data analysis with Apache Hive. A practical introduction</title>
<meta charset="utf-8">
<meta property="og:title" content="Data analysis with Apache Hive. A practical introduction">
<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/data-analyisis-apache-hive-practical-introduction">
<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/data-analyisis-apache-hive-practical-introduction">
<meta name="twitter:card" content="summary">
<meta name="twitter:domain" content="mmas.github.io">
<meta name="twitter:title" content="Data analysis with Apache Hive. A practical introduction">
<meta name="description" content="Apache Hive is a framework for data warehousing for manage large datasets. Hive can be used for data analysis in a SQL-like language called HiveQL. Th...">
<meta name="twitter:description" content="Apache Hive is a framework for data warehousing for manage large datasets. Hive can be used for data analysis in a SQL-like language called HiveQL. Th...">
<meta property="og:description" content="Apache Hive is a framework for data warehousing for manage large datasets. Hive can be used for data analysis in a SQL-like language called HiveQL. Th...">
<meta name="keywords" content="data-analysis,data-warehousing,hadoop,hive">
<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-analysis">
<meta property="article:tag" content="data-analysis,data-warehousing,hadoop,hive">
<meta property="article:published_time" content="2015-10-02">
<meta property="article:modified_time" content="2015-10-02">
<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="/data-analyisis-apache-hive-practical-introduction">Data analysis with Apache Hive. A practical introduction</a></h1>
<time datetime="2015-10-02">Oct 02, 2015</time>
<a class="tag" href="/tags?tag=data-analysis">data-analysis</a>
<a class="tag" href="/tags?tag=data-warehousing">data-warehousing</a>
<a class="tag" href="/tags?tag=hadoop">hadoop</a>
<a class="tag" href="/tags?tag=hive">hive</a>
</header>
<aside id="article-nav"></aside>
<section class="body">
<p><a target="_blank" href="http://hive.apache.org/">Apache Hive</a> is a framework for data warehousing for manage large datasets. Hive can be used for data analysis in a SQL-like language called HiveQL. </p>
<p>The following examples are done using the hourly precipitations dataset from NCDC for May of 1998 and the weather stations description file, both can be downloaded from <a target="_blank" href="http://www.ncdc.noaa.gov/orders/qclcd/">http://www.ncdc.noaa.gov/orders/qclcd/</a>.</p>
<h2>Loading data</h2>
<p>Load CSV data (hourly precipitations records):</p>
<pre><code class="hiveql">CREATE TABLE records (wban STRING, day STRING, time STRING, hp FLOAT)
ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
TBLPROPERTIES('skip.header.line.count'='1');
LOAD DATA INPATH 'input/199805hpd.txt'
OVERWRITE INTO TABLE records;
</code></pre>
<p>To load more complex data, in this case, the weather stations metadata with many columns splitted by a pipe and not all of them required, first we load the data as a simple table with a single string column:</p>
<pre><code class="hiveql">CREATE TABLE stations_raw (value STRING)
TBLPROPERTIES('skip.header.line.count'='1');
LOAD DATA INPATH 'input/station.txt'
INTO TABLE stations_raw;
</code></pre>
<p>Create a custom script to obtain the desired columns:</p>
<pre><code class="python">#!/usr/bin/env python
import sys
for line in sys.stdin:
cols = line.split('|')
wban = cols[0]
city, state = cols[6].split(',')
print '\t'.join([wban, city.capitalize(), state.lstrip()])
</code></pre>
<p>Create the final table with the desired columns using the previous script to map them:</p>
<pre><code class="hiveql">CREATE TABLE stations (wban STRING, city STRING, state STRING);
FROM stations_raw
INSERT OVERWRITE TABLE stations
MAP value
USING '/opt/data/ncdc/qclcd/parse_stations.py'
AS wban, city, state;
</code></pre>
<h2>Querying data</h2>
<p>Precipitations per station and date:</p>
<pre><code class="hiveql">SELECT day, SUM(hp)
FROM records
WHERE wban='03024'
GROUP BY day;
</code></pre>
<pre><code class="stdout">19980501 0.029999999329447746
19980502 0.0
19980503 0.0800000000745058
19980504 0.0
19980505 0.0
[...]
</code></pre>
<p>Month precipitations by station with station data:</p>
<pre><code class="hiveql">SELECT stations.*, records.sum_hp
FROM stations
JOIN (SELECT wban, SUM(hp) AS sum_hp FROM records GROUP BY wban) records
ON (records.wban = stations.wban);
</code></pre>
<pre><code class="stdout">03013 LAMAR CO 1.2399999964982271
03016 RIFLE CO 0.47999999299645424
03017 DENVER CO 1.7299999985843897
03024 BORGER TX 1.3900000154972076
03026 BURLINGTON CO 2.8499999884516
[...]
</code></pre>
</section>
</article>
</section>
<footer></footer>
<script type="text/javascript" src="/js/article.js"></script>
</body>
</html>