2424
2525import org .apache .commons .collections4 .MapUtils ;
2626import org .apache .commons .lang3 .StringUtils ;
27+ import org .apache .hadoop .conf .Configuration ;
28+ import org .apache .hadoop .security .UserGroupInformation ;
2729
2830import lombok .NonNull ;
2931import lombok .extern .slf4j .Slf4j ;
3032
3133import java .io .IOException ;
32- import java .net .InetSocketAddress ;
33- import java .net .Socket ;
3434import java .sql .Connection ;
3535import java .sql .DatabaseMetaData ;
3636import java .sql .DriverManager ;
4141import java .util .HashMap ;
4242import java .util .List ;
4343import java .util .Map ;
44+ import java .util .Properties ;
4445
4546@ Slf4j
4647public class HiveJdbcDataSourceChannel implements DataSourceChannel {
@@ -61,15 +62,15 @@ public List<String> getTables(
6162 Map <String , String > requestParams ,
6263 String database ,
6364 Map <String , String > option ) {
64- return getTables ( pluginName , requestParams , database , option );
65+ return getTableNames ( requestParams , database );
6566 }
6667
6768 @ Override
6869 public List <String > getDatabases (
6970 @ NonNull String pluginName , @ NonNull Map <String , String > requestParams ) {
7071 try {
71- return getDataBaseNames (pluginName , requestParams );
72- } catch (SQLException e ) {
72+ return getDataBaseNames (requestParams );
73+ } catch (SQLException | IOException e ) {
7374 log .error ("Query Hive databases error, request params is {}" , requestParams , e );
7475 throw new DataSourcePluginException ("Query Hive databases error," , e );
7576 }
@@ -104,33 +105,69 @@ public Map<String, List<TableField>> getTableFields(
104105 }
105106
106107 protected boolean checkJdbcConnectivity (Map <String , String > requestParams ) {
107- try (Connection ignored = init (requestParams )) {
108+ try (Connection ignored = getHiveConnection (requestParams )) {
108109 return true ;
109110 } catch (Exception e ) {
110111 throw new DataSourcePluginException (
111112 "check jdbc connectivity failed, " + e .getMessage (), e );
112113 }
113114 }
114115
115- protected Connection init (Map <String , String > requestParams ) throws SQLException {
116+ protected Connection getHiveConnection (Map <String , String > requestParams )
117+ throws IOException , SQLException {
116118 if (MapUtils .isEmpty (requestParams )) {
117119 throw new DataSourcePluginException (
118120 "Hive jdbc request params is null, please check your config" );
119121 }
120- String url = requestParams .get (HiveJdbcOptionRule .URL .key ());
121- return DriverManager .getConnection (url );
122+ String driverClass =
123+ requestParams .getOrDefault (
124+ HiveJdbcOptionRule .DRIVER .key (), "org.apache.hive.jdbc.HiveDriver" );
125+ try {
126+ Class .forName (driverClass );
127+ } catch (ClassNotFoundException e ) {
128+ throw new DataSourcePluginException (
129+ "Hive jdbc driver " + driverClass + " not found" , e );
130+ }
131+ Properties connProps = new Properties ();
132+ boolean isKerberosEnabled =
133+ Boolean .parseBoolean (requestParams .get (HiveJdbcOptionRule .USE_KERBEROS .key ()));
134+ if (isKerberosEnabled ) {
135+ String krb5ConfPath = requestParams .get (HiveJdbcOptionRule .KRB5_PATH .key ());
136+ if (StringUtils .isNotEmpty (krb5ConfPath )) {
137+ System .setProperty ("java.security.krb5.conf" , krb5ConfPath );
138+ }
139+ Configuration conf = new Configuration ();
140+ conf .set ("hadoop.security.authentication" , "Kerberos" );
141+ UserGroupInformation .setConfiguration (conf );
142+ String principal = requestParams .get (HiveJdbcOptionRule .KERBEROS_PRINCIPAL .key ());
143+ connProps .setProperty ("principal" , principal );
144+ String keytabPath = requestParams .get (HiveJdbcOptionRule .KERBEROS_KEYTAB_PATH .key ());
145+ UserGroupInformation .loginUserFromKeytab (principal , keytabPath );
146+ }
147+
148+ String user = requestParams .get (HiveJdbcOptionRule .USER .key ());
149+ String password = requestParams .get (HiveJdbcOptionRule .PASSWORD .key ());
150+ if (StringUtils .isNotEmpty (user )) {
151+ connProps .setProperty ("user" , user );
152+ }
153+ if (StringUtils .isNotEmpty (password )) {
154+ connProps .setProperty ("password" , password );
155+ }
156+
157+ String jdbcUrl = requestParams .get (HiveJdbcOptionRule .URL .key ());
158+ return DriverManager .getConnection (jdbcUrl , connProps );
122159 }
123160
124- protected List <String > getDataBaseNames (String pluginName , Map <String , String > requestParams )
125- throws SQLException {
161+ protected List <String > getDataBaseNames (Map <String , String > requestParams )
162+ throws SQLException , IOException {
126163 List <String > dbNames = new ArrayList <>();
127- try (Connection connection = init (requestParams );
128- Statement statement = connection .createStatement (); ) {
129- ResultSet re = statement .executeQuery ("SHOW DATABASES; " );
164+ try (Connection connection = getHiveConnection (requestParams );
165+ Statement statement = connection .createStatement ()) {
166+ ResultSet re = statement .executeQuery ("SHOW DATABASES" );
130167 // filter system databases
131168 while (re .next ()) {
132- String dbName = re .getString ("database " );
133- if (StringUtils .isNotBlank (dbName ) && isNotSystemDatabase (pluginName , dbName )) {
169+ String dbName = re .getString ("database_name " );
170+ if (StringUtils .isNotBlank (dbName ) && isNotSystemDatabase (dbName )) {
134171 dbNames .add (dbName );
135172 }
136173 }
@@ -140,25 +177,27 @@ protected List<String> getDataBaseNames(String pluginName, Map<String, String> r
140177
141178 protected List <String > getTableNames (Map <String , String > requestParams , String dbName ) {
142179 List <String > tableNames = new ArrayList <>();
143- try (Connection connection = init (requestParams ); ) {
180+ try (Connection connection = getHiveConnection (requestParams )) {
144181 ResultSet resultSet =
145- connection .getMetaData ().getTables (dbName , null , null , new String [] {"TABLE" });
182+ connection
183+ .getMetaData ()
184+ .getTables (dbName , dbName , null , new String [] {"TABLE" });
146185 while (resultSet .next ()) {
147186 String tableName = resultSet .getString ("TABLE_NAME" );
148187 if (StringUtils .isNotBlank (tableName )) {
149188 tableNames .add (tableName );
150189 }
151190 }
152191 return tableNames ;
153- } catch (SQLException e ) {
192+ } catch (SQLException | IOException e ) {
154193 throw new DataSourcePluginException ("get table names failed" , e );
155194 }
156195 }
157196
158197 protected List <TableField > getTableFields (
159198 Map <String , String > requestParams , String dbName , String tableName ) {
160199 List <TableField > tableFields = new ArrayList <>();
161- try (Connection connection = init (requestParams ); ) {
200+ try (Connection connection = getHiveConnection (requestParams )) {
162201 DatabaseMetaData metaData = connection .getMetaData ();
163202 String primaryKey = getPrimaryKey (metaData , dbName , tableName );
164203 ResultSet resultSet = metaData .getColumns (dbName , null , tableName , null );
@@ -177,7 +216,7 @@ protected List<TableField> getTableFields(
177216 tableField .setNullable (isNullable );
178217 tableFields .add (tableField );
179218 }
180- } catch (SQLException e ) {
219+ } catch (SQLException | IOException e ) {
181220 throw new DataSourcePluginException ("get table fields failed" , e );
182221 }
183222 return tableFields ;
@@ -186,25 +225,14 @@ protected List<TableField> getTableFields(
186225 private String getPrimaryKey (DatabaseMetaData metaData , String dbName , String tableName )
187226 throws SQLException {
188227 ResultSet primaryKeysInfo = metaData .getPrimaryKeys (dbName , "%" , tableName );
189- while (primaryKeysInfo .next ()) {
228+ if (primaryKeysInfo .next ()) {
190229 return primaryKeysInfo .getString ("COLUMN_NAME" );
191230 }
192231 return null ;
193232 }
194233
195- @ SuppressWarnings ("checkstyle:MagicNumber" )
196- private static boolean checkHostConnectable (String host , int port ) {
197- try (Socket socket = new Socket ()) {
198- socket .connect (new InetSocketAddress (host , port ), 1000 );
199- return true ;
200- } catch (IOException e ) {
201- return false ;
202- }
203- }
204-
205- private boolean isNotSystemDatabase (String pluginName , String dbName ) {
206- // FIXME,filters system databases
207- return true ;
234+ private boolean isNotSystemDatabase (String dbName ) {
235+ return !HiveJdbcConstants .HIVE_SYSTEM_DATABASES .contains (dbName .toLowerCase ());
208236 }
209237
210238 private boolean convertToBoolean (Object value ) {
0 commit comments