-
Notifications
You must be signed in to change notification settings - Fork 35
/
5-hive.Rpres
222 lines (121 loc) · 3.59 KB
/
5-hive.Rpres
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
Analysing New York taxis with RHive
========================================================
author: Andrie de Vries & Michele Usuelli
date: 2015-07-01, UseR!2015
width: 1680
height: 1050
css: css/custom.css
Introduction to Hive
====
- Hadoop data warehouse
- SQL-like interface
- Language: HQL (Hive Query Language)
- HQL translated into MapReduce jobs
- R interface: RHive, RevoScaleR
- Hue web interface
Using Hive with R
====
- Building queries using string manipulation tools like sprintf and paste
- Running queries using RHive
Syntax difference between Hive and RHive
====
type: alert
- Hive: semi-colon in the end
```
SELECT COUNT(*) FROM table_data;
```
- RHive: no semi-colon in the end
```
SELECT COUNT(*) FROM table_data
```
Computing the number of taxi rides by hour
====
Using Hive, we can replicate the rmr2 example counting the number of taxi runs by hour. The steps are
1. Importing the data
2. Querying the data
Defining an external table
====
Starting from our data, we can create a Hive table. Since the data is already in CSV format, the easiest option is creating an external table.
We need to specify
- The data location
- The data format
- The field formats
Query to create an extenal table
====
```
CREATE EXTERNAL TABLE taxi_sample(medallion STRING, hack_license STRING, vendor_id STRING, rate_code INT, store_and_fwd_flag STRING, pickup_datetime STRING, dropoff_datetime STRING, passenger_count INT, trip_time_in_secs INT, trip_distance FLOAT, pickup_longitude FLOAT, pickup_latitude FLOAT, dropoff_longitude FLOAT, dropoff_latitude FLOAT) ROW FORMAT
DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
STORED AS TEXTFILE
LOCATION '/user/share/taxi/sample';
```
Query to create an external table - defining the field format
====
The first part of the query defines a table called *taxi_sample* and the format of its fields.
```
CREATE EXTERNAL TABLE taxi_sample(medallion STRING, hack_license STRING, vendor_id STRING, rate_code INT, store_and_fwd_flag STRING, pickup_datetime STRING, dropoff_datetime STRING, passenger_count INT, trip_time_in_secs INT, trip_distance FLOAT, pickup_longitude FLOAT, pickup_latitude FLOAT, dropoff_longitude FLOAT, dropoff_latitude FLOAT) ROW FORMAT
```
Query to create an external table: defining the data format
====
This part of the query defines the data format.
```
DELIMITED FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
STORED AS TEXTFILE
```
Query to create an external table: defining the data location
====
This part of the query defines the HDFS path to the data folder.
```
LOCATION '/user/share/taxi/sample';
```
Setting up RHive
====
```{r 2-hive-queries.R, cache=FALSE, include=FALSE}
read_chunk("hive/2-hive-queries.R")
```
```{r configure-rhive}
```
Simple query: count the number of records
====
```{r row-count}
```
```{r run-row-count, cache=TRUE}
```
Simple query: count the number of records
====
```{r build-row-count}
```
```{r run-row-count, cache=TRUE}
```
Define the hour
====
```{r define-hour}
```
Define the hour
====
```{r build-define-hour}
```
Define the hour
====
```{r run-define-hour, cache=TRUE}
```
Count by hour
====
```{r count-by-hour}
```
Count by hour
====
```{r build-count-by-hour}
```
Count by hour
====
```{r run-count-by-hour, cache=TRUE}
```
Conclusions
====
- Hive allows to define group-by queries writing SQL-like code
- The MapReduce code is not displayed
- R tools for string manipulation allow to easily build complex queries
- RHive allows to run Hive queries directly from R
- For more complex queries, we still need to use other tools like rmr2