5cd4c26ab3091e5c9d1ce5d91060a46806d0cfa8
[sqoop.git] / src / test / org / apache / sqoop / hcat / TestHCatalogBasic.java
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.apache.sqoop.hcat;
20
21 import junit.framework.TestCase;
22
23 import org.junit.Before;
24
25 import com.cloudera.sqoop.SqoopOptions;
26 import com.cloudera.sqoop.tool.ExportTool;
27 import com.cloudera.sqoop.tool.ImportTool;
28
29 /**
30 * Test basic HCatalog related features.
31 */
32 public class TestHCatalogBasic extends TestCase {
33 private static ImportTool importTool;
34 private static ExportTool exportTool;
35
36 @Before
37 @Override
38 public void setUp() {
39 importTool = new ImportTool();
40 exportTool = new ExportTool();
41 }
42 private SqoopOptions parseImportArgs(String[] argv) throws Exception {
43 SqoopOptions opts = importTool.parseArguments(argv, null, null, false);
44 return opts;
45 }
46
47 private SqoopOptions parseExportArgs(String[] argv) throws Exception {
48 SqoopOptions opts = exportTool.parseArguments(argv, null, null, false);
49 return opts;
50 }
51
52 public void testHCatalogHomeWithImport() throws Exception {
53 String[] args = {
54 "--hcatalog-home",
55 "/usr/lib/hcatalog",
56 };
57
58 SqoopOptions opts = parseImportArgs(args);
59 }
60
61 public void testHCatalogHomeWithExport() throws Exception {
62 String[] args = {
63 "--hcatalog-home",
64 "/usr/lib/hcatalog",
65 };
66
67 SqoopOptions opts = parseExportArgs(args);
68 }
69
70 public void testHCatalogImport() throws Exception {
71 String[] args = {
72 "--hcatalog-table",
73 "table",
74 };
75
76 SqoopOptions opts = parseImportArgs(args);
77 }
78
79 public void testHCatalogExport() throws Exception {
80 String[] args = {
81 "--hcatalog-table",
82 "table",
83 };
84
85 SqoopOptions opts = parseExportArgs(args);
86 }
87
88 public void testHCatImportWithTargetDir() throws Exception {
89 String[] args = {
90 "--connect",
91 "jdbc:db:url",
92 "--table",
93 "dbtable",
94 "--hcatalog-table",
95 "table",
96 "--target-dir",
97 "/target/dir",
98 };
99 try {
100 SqoopOptions opts = parseImportArgs(args);
101 importTool.validateOptions(opts);
102 fail("Expected InvalidOptionsException");
103 } catch (SqoopOptions.InvalidOptionsException ioe) {
104 // expected.
105 }
106 }
107
108 public void testHCatImportWithWarehouseDir() throws Exception {
109 String[] args = {
110 "--connect",
111 "jdbc:db:url",
112 "--table",
113 "dbtable",
114 "--hcatalog-table",
115 "table",
116 "--warehouse-dir",
117 "/target/dir",
118 };
119 try {
120 SqoopOptions opts = parseImportArgs(args);
121 importTool.validateOptions(opts);
122 fail("Expected InvalidOptionsException");
123 } catch (SqoopOptions.InvalidOptionsException ioe) {
124 // expected.
125 }
126 }
127
128 public void testHCatImportWithHiveImport() throws Exception {
129 String[] args = {
130 "--connect",
131 "jdbc:db:url",
132 "--table",
133 "dbtable",
134 "--hcatalog-table",
135 "table",
136 "--hive-import",
137 };
138 try {
139 SqoopOptions opts = parseImportArgs(args);
140 importTool.validateOptions(opts);
141 fail("Expected InvalidOptionsException");
142 } catch (SqoopOptions.InvalidOptionsException ioe) {
143 // expected.
144 }
145 }
146
147 public void testHCatExportWithExportDir() throws Exception {
148 String[] args = {
149 "--connect",
150 "jdbc:db:url",
151 "--table",
152 "dbtable",
153 "--hcatalog-table",
154 "table",
155 "--export-dir",
156 "/export/dir",
157 };
158 try {
159 SqoopOptions opts = parseExportArgs(args);
160 exportTool.validateOptions(opts);
161 fail("Expected InvalidOptionsException");
162 } catch (SqoopOptions.InvalidOptionsException ioe) {
163 // expected.
164 }
165 }
166
167
168 public void testHCatImportWithSequenceFile() throws Exception {
169 String[] args = {
170 "--connect",
171 "jdbc:db:url",
172 "--table",
173 "dbtable",
174 "--hcatalog-table",
175 "table",
176 "--as-sequencefile",
177 };
178 try {
179 SqoopOptions opts = parseImportArgs(args);
180 importTool.validateOptions(opts);
181 fail("Expected InvalidOptionsException");
182 } catch (SqoopOptions.InvalidOptionsException ioe) {
183 // expected.
184 }
185 }
186
187 public void testHCatImportWithAvroFile() throws Exception {
188 String[] args = {
189 "--connect",
190 "jdbc:db:url",
191 "--table",
192 "dbtable",
193 "--hcatalog-table",
194 "table",
195 "--as-avrodatafile",
196 };
197 try {
198 SqoopOptions opts = parseImportArgs(args);
199 importTool.validateOptions(opts);
200 fail("Expected InvalidOptionsException");
201 } catch (SqoopOptions.InvalidOptionsException ioe) {
202 // expected.
203 }
204 }
205 public void testHCatImportWithCreateTable() throws Exception {
206 String[] args = {
207 "--hcatalog-table",
208 "table",
209 "--create-hcatalog-table",
210 };
211 SqoopOptions opts = parseImportArgs(args);
212 }
213
214 public void testHCatImportWithDropAndCreateTable() throws Exception {
215 String[] args = {
216 "--connect",
217 "jdbc:db:url",
218 "--table",
219 "dbtable",
220 "--hcatalog-table",
221 "table",
222 "--drop-and-create-hcatalog-table",
223 };
224 SqoopOptions opts = parseImportArgs(args);
225 importTool.validateOptions(opts);
226 }
227
228 public void testHCatImportWithCreateTableAndDropAndCreateTable()
229 throws Exception {
230 String[] args = {
231 "--connect",
232 "jdbc:db:url",
233 "--table",
234 "dbtable",
235 "--hcatalog-table",
236 "table",
237 "--create-hcatalog-table",
238 "--drop-and-create-hcatalog-table",
239 };
240 SqoopOptions opts = parseImportArgs(args);
241 try {
242 importTool.validateOptions(opts);
243 fail("Expected InvalidOptionsException");
244 } catch (SqoopOptions.InvalidOptionsException ioe) {
245 // expected.
246 }
247 }
248
249 public void testHCatImportWithStorageStanza() throws Exception {
250 String[] args = {
251 "--hcatalog-table",
252 "table",
253 "--hcatalog-storage-stanza",
254 "stored as textfile",
255 };
256 SqoopOptions opts = parseImportArgs(args);
257 }
258
259 public void testHCatImportWithDatabase() throws Exception {
260 String[] args = {
261 "--hcatalog-table",
262 "table",
263 "--hcatalog-database",
264 "default",
265 };
266 SqoopOptions opts = parseImportArgs(args);
267 }
268
269 public void testHCatImportWithPartKeys() throws Exception {
270 String[] args = {
271 "--hcatalog-table",
272 "table",
273 "--hcatalog-partition-keys",
274 "k1,k2",
275 "--hcatalog-partition-values",
276 "v1,v2",
277 };
278 SqoopOptions opts = parseImportArgs(args);
279 }
280
281 public void testHCatImportWithOnlyHCatKeys() throws Exception {
282 String[] args = {
283 "--connect",
284 "jdbc:db:url",
285 "--table",
286 "dbtable",
287 "--hcatalog-table",
288 "table",
289 "--hcatalog-partition-keys",
290 "k1,k2",
291 };
292 try {
293 SqoopOptions opts = parseImportArgs(args);
294 importTool.validateOptions(opts);
295 fail("Expected InvalidOptionsException");
296 } catch (SqoopOptions.InvalidOptionsException ioe) {
297 // expected.
298 }
299 }
300
301 public void testHCatImportWithMismatchedKeysAndVals() throws Exception {
302 String[] args = {
303 "--connect",
304 "jdbc:db:url",
305 "--table",
306 "dbtable",
307 "--hcatalog-table",
308 "table",
309 "--hcatalog-partition-keys",
310 "k1,k2",
311 "--hcatalog-partition-values",
312 "v1",
313 };
314 try {
315 SqoopOptions opts = parseImportArgs(args);
316 importTool.validateOptions(opts);
317 fail("Expected InvalidOptionsException");
318 } catch (SqoopOptions.InvalidOptionsException ioe) {
319 // expected.
320 }
321 }
322 public void testHCatImportWithEmptyKeysAndVals() throws Exception {
323 String[] args = {
324 "--connect",
325 "jdbc:db:url",
326 "--table",
327 "dbtable",
328 "--hcatalog-table",
329 "table",
330 "--hcatalog-partition-keys",
331 "k1,",
332 "--hcatalog-partition-values",
333 ",v1",
334 };
335 try {
336 SqoopOptions opts = parseImportArgs(args);
337 importTool.validateOptions(opts);
338 fail("Expected InvalidOptionsException");
339 } catch (SqoopOptions.InvalidOptionsException ioe) {
340 // expected.
341 }
342 }
343 public void testHCatImportWithBothHCatAndHivePartOptions() throws Exception {
344 String[] args = {
345 "--connect",
346 "jdbc:db:url",
347 "--table",
348 "dbtable",
349 "--hcatalog-table",
350 "table",
351 "--hcatalog-partition-keys",
352 "k1,k2",
353 "--hcatalog-partition-values",
354 "v1,v2",
355 "--hive-partition-key",
356 "k1",
357 "--hive-partition-value",
358 "v1",
359 };
360 SqoopOptions opts = parseImportArgs(args);
361 importTool.validateOptions(opts);
362 }
363 }