001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.oozie.action.hadoop;
019    
020    import java.io.File;
021    import java.io.FileOutputStream;
022    import java.io.OutputStream;
023    
024    import org.apache.hadoop.conf.Configuration;
025    import org.apache.hadoop.fs.Path;
026    import com.cloudera.sqoop.Sqoop;
027    
028    public class SqoopMain extends LauncherMain {
029    
030        public static final String SQOOP_DEFAULT_CONF = "sqoop-default.xml";
031        public static final String SQOOP_SITE_CONF = "sqoop-site.xml";
032    
033        public static void main(String[] args) throws Exception {
034            run(SqoopMain.class, args);
035        }
036    
037        protected void run(String[] args) throws Exception {
038            System.out.println();
039            System.out.println("Oozie Sqoop action configuration");
040            System.out.println("=================================================================");
041    
042            // loading action conf prepared by Oozie
043            Configuration actionConf = new Configuration(false);
044    
045            String actionXml = System.getProperty("oozie.action.conf.xml");
046    
047            if (actionXml == null) {
048                throw new RuntimeException("Missing Java System Property [oozie.action.conf.xml]");
049            }
050            if (!new File(actionXml).exists()) {
051                throw new RuntimeException("Action Configuration XML file [" + actionXml + "] does not exist");
052            }
053    
054            actionConf.addResource(new Path("file:///", actionXml));
055    
056            // See http://issues.apache.org/jira/browse/HIVE-1411
057            actionConf.set("datanucleus.plugin.pluginRegistryBundleCheck", "LOG");
058    
059            // Propagate delegation related props from launcher job to Hive job
060            if (System.getenv("HADOOP_TOKEN_FILE_LOCATION") != null) {
061                actionConf.set("mapreduce.job.credentials.binary",
062                        System.getenv("HADOOP_TOKEN_FILE_LOCATION"));
063                System.out.println("------------------------");
064                System.out.println("Setting env property for mapreduce.job.credentials.binary to:"
065                        + System.getenv("HADOOP_TOKEN_FILE_LOCATION"));
066                System.out.println("------------------------");
067                System.setProperty("mapreduce.job.credentials.binary",
068                        System.getenv("HADOOP_TOKEN_FILE_LOCATION"));
069            } else {
070                System.out.println("Non-Kerberos execution");
071            }
072    
073            // Write the action configuration out to sqoop-site.xml
074            // AND hive-site.xml as properties contained in the configuration
075            // can target either Sqoop proper or Hive (in the event
076            // of a Sqoop hive-import operation.
077            OutputStream os = new FileOutputStream(SQOOP_SITE_CONF);
078            actionConf.writeXml(os);
079            os.close();
080    
081    //        os = new FileOutputStream(HIVE_SITE_CONF);
082    //        actionConf.writeXml(os);
083    //        os.close();
084    //
085    
086            String command = actionConf.get("oozie.sqoop.command");
087            if (command == null) {
088                throw new RuntimeException("Action Configuration does not have [oozie.sqoop.command] property");
089            }
090    
091            String[] sqoopArgs = command.split(" ");
092    
093            System.out.println("Sqoop command arguments :");
094            for (String arg : sqoopArgs) {
095                System.out.println("             " + arg);
096            }
097    
098            System.out.println("=================================================================");
099            System.out.println();
100            System.out.println(">>> Invoking Sqoop command line now >>>");
101            System.out.println();
102            System.out.flush();
103    
104            runSqoopJob(sqoopArgs);
105    
106            System.out.println();
107            System.out.println("<<< Invocation of Sqoop command completed <<<");
108            System.out.println();
109    
110    
111        }
112    
113        protected void runSqoopJob(String[] args) throws Exception {
114            // running as from the command line
115            Sqoop.main(args);
116        }
117    
118        public static void setSqoopCommand(Configuration conf, String command) {
119            conf.set("oozie.sqoop.command", command);
120        }
121    }