SQOOP-3309: Implement HiveServer2 client
[sqoop.git] / src / java / org / apache / sqoop / tool / CreateHiveTableTool.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.tool;
20
21 import java.io.IOException;
22
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.OptionBuilder;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.apache.hadoop.util.StringUtils;
28
29 import org.apache.sqoop.SqoopOptions;
30 import org.apache.sqoop.SqoopOptions.InvalidOptionsException;
31 import org.apache.sqoop.cli.RelatedOptions;
32 import org.apache.sqoop.cli.ToolOptions;
33 import org.apache.sqoop.hive.HiveClient;
34 import org.apache.sqoop.hive.HiveClientFactory;
35
36 /**
37 * Tool that creates a Hive table definition.
38 */
39 public class CreateHiveTableTool extends BaseSqoopTool {
40
41 public static final Log LOG = LogFactory.getLog(
42 CreateHiveTableTool.class.getName());
43
44 private final HiveClientFactory hiveClientFactory;
45
46 public CreateHiveTableTool(HiveClientFactory hiveClientFactory) {
47 super("create-hive-table");
48 this.hiveClientFactory = hiveClientFactory;
49 }
50
51 public CreateHiveTableTool() {
52 this(new HiveClientFactory());
53 }
54
55 @Override
56 /** {@inheritDoc} */
57 public int run(SqoopOptions options) {
58 if (!init(options)) {
59 return 1;
60 }
61
62 try {
63 HiveClient hiveClient = hiveClientFactory.createHiveClient(options, manager);
64 hiveClient.createTable();
65 } catch (IOException ioe) {
66 LOG.error("Encountered IOException running create table job: "
67 + StringUtils.stringifyException(ioe));
68 rethrowIfRequired(options, ioe);
69 return 1;
70 } finally {
71 destroy(options);
72 }
73
74 return 0;
75 }
76
77 @Override
78 /** Configure the command-line arguments we expect to receive */
79 public void configureOptions(ToolOptions toolOptions) {
80
81 toolOptions.addUniqueOptions(getCommonOptions());
82
83 RelatedOptions hiveOpts = getHiveOptions(false);
84 hiveOpts.addOption(OptionBuilder.withArgName("table-name")
85 .hasArg()
86 .withDescription("The db table to read the definition from")
87 .withLongOpt(TABLE_ARG)
88 .create());
89 toolOptions.addUniqueOptions(hiveOpts);
90
91 toolOptions.addUniqueOptions(getOutputFormatOptions());
92 }
93
94 @Override
95 /** {@inheritDoc} */
96 public void printHelp(ToolOptions toolOptions) {
97 super.printHelp(toolOptions);
98 System.out.println("");
99 System.out.println(
100 "At minimum, you must specify --connect and --table");
101 }
102
103 @Override
104 /** {@inheritDoc} */
105 public void applyOptions(CommandLine in, SqoopOptions out)
106 throws InvalidOptionsException {
107
108 if (in.hasOption(TABLE_ARG)) {
109 out.setTableName(in.getOptionValue(TABLE_ARG));
110 }
111
112 out.setHiveImport(true);
113
114 applyCommonOptions(in, out);
115 applyHiveOptions(in, out);
116 applyOutputFormatOptions(in, out);
117 }
118
119 @Override
120 /** {@inheritDoc} */
121 public void validateOptions(SqoopOptions options)
122 throws InvalidOptionsException {
123
124 if (hasUnrecognizedArgs(extraArguments)) {
125 throw new InvalidOptionsException(HELP_STR);
126 }
127
128 validateCommonOptions(options);
129 validateOutputFormatOptions(options);
130 validateHiveOptions(options);
131
132 if (options.getTableName() == null) {
133 throw new InvalidOptionsException(
134 "--table is required for table definition importing." + HELP_STR);
135 }
136 }
137 }
138