forked from HiveRunner/HiveRunner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HiveShell.java
323 lines (280 loc) · 9.72 KB
/
HiveShell.java
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Copyright (C) 2013-2018 Klarna AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.klarna.hiverunner;
import com.klarna.hiverunner.data.InsertIntoTable;
import org.apache.hadoop.hive.conf.HiveConf;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.util.List;
/**
* Test handle to the hive server.
*
* Please refer to test class {@code com.klarna.hiverunner.examples.HelloHiveRunner} for usage examples.
*/
public interface HiveShell {
/**
* Executes a single query.
* <p>
* May only be called post #start()
* </p>
*/
List<String> executeQuery(String hiveSql);
/**
* Executes a single query.
* <p>
* May only be called post #start()
* </p>
*/
List<String> executeQuery(String hiveSql, String rowValuesDelimitedBy, String replaceNullWith);
/**
* Executes a single query from a script file, returning any results.
* <p>
* May only be called post #start()
* </p>
*/
List<String> executeQuery(File script);
/**
* Executes a single query from a script file, returning any results.
* <p>
* May only be called post #start()
* </p>
*/
List<String> executeQuery(Path script);
/**
* Executes a single query from a script file, returning any results.
* <p>
* May only be called post #start()
* </p>
*/
List<String> executeQuery(Charset charset, File script);
/**
* Executes a single query from a script file, returning any results.
* <p>
* May only be called post #start()
* </p>
*/
List<String> executeQuery(Charset charset, Path script);
/**
* Executes a single query from a script file, returning any results.
* <p>
* May only be called post #start()
* </p>
*/
List<String> executeQuery(File script, String rowValuesDelimitedBy, String replaceNullWith);
/**
* Executes a single query from a script file, returning any results.
* <p>
* May only be called post #start()
* </p>
*/
List<String> executeQuery(Path script, String rowValuesDelimitedBy, String replaceNullWith);
/**
* Executes a single query from a script file, returning any results.
* <p>
* May only be called post #start()
* </p>
*/
List<String> executeQuery(Charset charset, File script, String rowValuesDelimitedBy, String replaceNullWith);
/**
* Executes a single query from a script file, returning any results.
* <p>
* May only be called post #start()
* </p>
*/
List<String> executeQuery(Charset charset, Path script, String rowValuesDelimitedBy, String replaceNullWith);
/**
* Execute a single hive query
* <p>
* May only be called post #start()
* </p>
*/
List<Object[]> executeStatement(String hiveSql);
/**
* Executes a hive script. The script may contain multiple statements delimited by ';'
* <p>
* May only be called post #start()
* </p>
*/
void execute(String script);
/**
* Executes a hive script. The script may contain multiple statements delimited by ';'.
* Default charset will be used to read the given files.
* <p>
* May only be called post #start()
* </p>
*/
void execute(File file);
/**
* Executes a hive script. The script may contain multiple statements delimited by ';'.
* Default charset will be used to read the given files.
* <p>
* May only be called post #start()
* </p>
*/
void execute(Path path);
/**
* Executes a hive script. The script may contain multiple statements delimited by ';'
* <p>
* May only be called post #start()
* </p>
*/
void execute(Charset charset, File file);
/**
* Executes a hive script. The script may contain multiple statements delimited by ';'
* <p>
* May only be called post #start()
* </p>
*/
void execute(Charset charset, Path path);
/**
* Start the shell. May only be called once. The test engine will by default call this method,
* Set {@link com.klarna.hiverunner.annotations.HiveSQL#autoStart()} to false to explicitly control
* when to start from the test case.
* <p>
* This might be useful for test methods that needs additional setup not catered for with the provided annotations.
* </p>
*/
void start();
/**
* Set a HiveConf property.
* <p>
* May only be called pre #start()
* </p>
* @deprecated Use {@link HiveShell#setHiveConfValue(String, String)} instead
*/
@Deprecated
void setProperty(String key, String value);
/**
* Set HiveConf property.
* <p>
* May only be called pre #start()
* </p>
*/
void setHiveConfValue(String key, String value);
/**
* Set Hive variable.
* <p>
* May only be called pre #start()
* </p>
*/
void setHiveVarValue(String var, String value);
/**
* Get the current HiveConf from hive
*/
HiveConf getHiveConf();
void setCwd(Path cwd);
Path getCwd();
/**
* Copy test data into hdfs
* May only be called pre #start()
* <p>
* {@link com.klarna.hiverunner.MethodLevelResourceTest#resourceLoadingAsFileTest()}
* and {@link com.klarna.hiverunner.MethodLevelResourceTest#resourceLoadingAsStringTest()}
* </p>
*/
void addResource(String targetFile, File sourceFile);
/**
* Copy test data into hdfs
* May only be called pre #start()
* <p>
* {@link com.klarna.hiverunner.MethodLevelResourceTest#resourceLoadingAsFileTest()}
* and {@link com.klarna.hiverunner.MethodLevelResourceTest#resourceLoadingAsStringTest()}
* </p>
*/
void addResource(String targetFile, Path sourceFile);
/**
* Copy test data into hdfs
* May only be called pre #start()
* <p>
* {@link com.klarna.hiverunner.MethodLevelResourceTest#resourceLoadingAsFileTest()}
* and {@link com.klarna.hiverunner.MethodLevelResourceTest#resourceLoadingAsStringTest()}
* </p>
*/
void addResource(String targetFile, String data);
/**
* Add a hive script that will be executed when the hive shell is started
* Scripts will be executed in the order they are added.
*
* Note that execution order is not guaranteed with
* fields annotated with {@link com.klarna.hiverunner.annotations.HiveSetupScript}
*/
void addSetupScript(String script);
/**
* Add hive scripts that will be executed when the hive shell is started. Scripts will be executed in given order.
*
* Note that execution order is not guaranteed with
* fields annotated with {@link com.klarna.hiverunner.annotations.HiveSetupScript}
*/
void addSetupScripts(Charset charset, File... scripts);
/**
* Add hive scripts that will be executed when the hive shell is started. Scripts will be executed in given order.
*
* Note that execution order is not guaranteed with
* fields annotated with {@link com.klarna.hiverunner.annotations.HiveSetupScript}
*/
void addSetupScripts(Charset charset, Path... scripts);
/**
* Add hive scripts that will be executed when the hive shell is started. Scripts will be executed in given order.
*
* Default charset will be used to read the given files
*
* Note that execution order is not guaranteed with
* fields annotated with {@link com.klarna.hiverunner.annotations.HiveSetupScript}
*/
void addSetupScripts(File... scripts);
/**
* Add hive scripts that will be executed when the hive shell is started. Scripts will be executed in given order.
*
* Default charset will be used to read the given files
*
* Note that execution order is not guaranteed with
* fields annotated with {@link com.klarna.hiverunner.annotations.HiveSetupScript}
*/
void addSetupScripts(Path... scripts);
/**
* Get the test case sand box base dir
*/
TemporaryFolder getBaseDir();
/**
* Resolve all substituted variables with the hive conf.
* @throws IllegalArgumentException if not all substitutes could be resolved
* @throws IllegalStateException if the HiveShell was not started yet.
*/
String expandVariableSubstitutes(String expression);
/**
* Open up a stream to write test data into HDFS.
*
* May only be called pre #start().
* No writes to the stream will be allowed post #start().
* @param targetFile The path to the target file relative to the hive work space.
*
* See test class {@code com.klarna.hiverunner.ResourceOutputStreamTest#sequenceFile()} for an example of how this works.
* with sequence files.
*/
OutputStream getResourceOutputStream(String targetFile);
/**
* Returns an {@link InsertIntoTable} that allows programmatically inserting data into a table in a fluent manner.
* <p>
* May only be called post #start()
* </p>
* @param databaseName The database name
* @param tableName The table name
*/
InsertIntoTable insertInto(String databaseName, String tableName);
}