From 3fee25eb8fa51362f3ec6ff220d223a4f62c69be Mon Sep 17 00:00:00 2001 From: Bilung Lee Date: Tue, 11 Sep 2012 17:56:56 -0700 Subject: [PATCH] SQOOP-599: Import to HBase is not working on secure cluster --- build.xml | 2 +- .../sqoop/mapreduce/HBaseImportJob.java | 42 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/build.xml b/build.xml index edcda6ec..17037ca5 100644 --- a/build.xml +++ b/build.xml @@ -174,7 +174,7 @@ - + diff --git a/src/java/org/apache/sqoop/mapreduce/HBaseImportJob.java b/src/java/org/apache/sqoop/mapreduce/HBaseImportJob.java index 6a784d21..a2a167bc 100644 --- a/src/java/org/apache/sqoop/mapreduce/HBaseImportJob.java +++ b/src/java/org/apache/sqoop/mapreduce/HBaseImportJob.java @@ -19,6 +19,9 @@ package org.apache.sqoop.mapreduce; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; @@ -28,6 +31,7 @@ import org.apache.hadoop.hbase.client.HBaseAdmin; import org.apache.hadoop.hbase.client.HTable; import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil; +import org.apache.hadoop.hbase.security.User; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.mapreduce.Job; @@ -138,6 +142,44 @@ protected void jobSetup(Job job) throws IOException, ImportException { HBaseAdmin admin = new HBaseAdmin(conf); + // Add authentication token to the job if we're running on secure cluster. + // + // We're currently supporting HBase version 0.90 that do not have security + // patches which means that it do not have required method + // "obtainAuthTokenForJob". + // + // We're using reflection API to see if this method is available and call + // it only if it's present. + // + // After we will remove support for HBase 0.90 we can simplify the code to + // following code fragment: + /* + try { + User user = User.getCurrent(); + user.obtainAuthTokenForJob(conf, job); + } catch(InterruptedException ex) { + throw new ImportException("Can't get authentication token", ex); + } + */ + try { + // Get the method + Method obtainAuthTokenForJob = User.class.getMethod( + "obtainAuthTokenForJob", Configuration.class, Job.class); + + // Get current user + User user = User.getCurrent(); + + // Obtain security token if needed (it's no-op on non secure cluster) + obtainAuthTokenForJob.invoke(user, conf, job); + } catch (NoSuchMethodException e) { + LOG.info("It seems that we're running on HBase without security" + + " additions. Security additions will not be used during this job."); + } catch (InvocationTargetException e) { + throw new ImportException("Can't get authentication token", e); + } catch (IllegalAccessException e) { + throw new ImportException("Can't get authentication token", e); + } + // Check to see if the table exists. HTableDescriptor tableDesc = new HTableDescriptor(tableName); byte [] familyBytes = Bytes.toBytes(familyName);