SQOOP-3309: Implement HiveServer2 client
[sqoop.git] / src / java / org / apache / sqoop / hive / HiveServer2ConnectionFactory.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 * <p>
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * <p>
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.hive;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.security.UserGroupInformation;
24 import org.apache.sqoop.db.DriverManagerJdbcConnectionFactory;
25
26 import java.io.IOException;
27 import java.sql.Connection;
28
29 import static org.apache.commons.lang3.StringUtils.EMPTY;
30
31 public class HiveServer2ConnectionFactory extends DriverManagerJdbcConnectionFactory {
32
33 private static final Log LOG = LogFactory.getLog(HiveServer2ConnectionFactory.class.getName());
34
35 private static final String HS2_DRIVER_CLASS = "org.apache.hive.jdbc.HiveDriver";
36
37 public HiveServer2ConnectionFactory(String connectionString, String username, String password) {
38 super(HS2_DRIVER_CLASS, connectionString, username, password);
39 }
40
41 public HiveServer2ConnectionFactory(String connectionString, String username) {
42 this(connectionString, username, null);
43 }
44
45 @Override
46 public Connection createConnection() {
47 LOG.info("Creating connection to HiveServer2 as: " + getCurrentUser());
48 return super.createConnection();
49 }
50
51 private String getCurrentUser() {
52 try {
53 return UserGroupInformation.getCurrentUser().toString();
54 } catch (IOException e) {
55 LOG.error("Unable to determine current user.", e);
56 }
57 return EMPTY;
58 }
59
60 }