1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */18package org.apache.giraph;
1920import org.apache.commons.cli.CommandLine;
21import org.apache.giraph.io.formats.FileOutputFormatUtil;
22import org.apache.giraph.utils.ConfigurationUtils;
23import org.apache.giraph.conf.GiraphConfiguration;
24import org.apache.giraph.job.GiraphJob;
25/*if[PURE_YARN]26import org.apache.giraph.yarn.GiraphYarnClient;27end[PURE_YARN]*/28import org.apache.hadoop.conf.Configuration;
29import org.apache.hadoop.filecache.DistributedCache;
30import org.apache.hadoop.fs.Path;
31import org.apache.hadoop.util.Tool;
32import org.apache.hadoop.util.ToolRunner;
33import org.apache.log4j.Logger;
34import java.net.URI;
3536/**37 * Helper class to run Giraph applications by specifying the actual class name38 * to use (i.e. vertex, vertex input/output format, combiner, etc.).39 *40 * This is the default entry point for Giraph jobs running on any Hadoop41 * cluster, MRv1 or v2, including Hadoop-specific configuration and setup.42 */43publicclassGiraphRunnerimplements Tool {
44static {
45 Configuration.addDefaultResource("giraph-site.xml");
46 }
4748/** Class logger */49privatestaticfinal Logger LOG = Logger.getLogger(GiraphRunner.class);
50/** Writable conf */51private Configuration conf;
5253 @Override
54public Configuration getConf() {
55return conf;
56 }
5758 @Override
59publicvoid setConf(Configuration conf) {
60this.conf = conf;
61 }
6263 @Override
64/**65 * Drives a job run configured for "Giraph on Hadoop MR cluster"66 * @param args the command line arguments67 * @return job run exit code68 */69publicint run(String[] args) throws Exception {
70if (null == getConf()) { // for YARN profile71 conf = new Configuration();
72 }
73GiraphConfiguration giraphConf = newGiraphConfiguration(getConf());
74 CommandLine cmd = ConfigurationUtils.parseArgs(giraphConf, args);
75if (null == cmd) {
76return 0; // user requested help/info printout, don't run a job.77 }
7879// set up job for various platforms80final String vertexClassName = args[0];
81final String jobName = "Giraph: " + vertexClassName;
82/*if[PURE_YARN]83 GiraphYarnClient job = new GiraphYarnClient(giraphConf, jobName);84else[PURE_YARN]*/85GiraphJob job = newGiraphJob(giraphConf, jobName);
86 prepareHadoopMRJob(job, cmd);
87/*end[PURE_YARN]*/8889// run the job, collect results90if (LOG.isDebugEnabled()) {
91 LOG.debug("Attempting to run Vertex: " + vertexClassName);
92 }
93boolean verbose = !cmd.hasOption('q');
94return job.run(verbose) ? 0 : -1;
95 }
9697/**98 * Populate internal Hadoop Job (and Giraph IO Formats) with Hadoop-specific99 * configuration/setup metadata, propagating exceptions to calling code.100 * @param job the GiraphJob object to help populate Giraph IO Format data.101 * @param cmd the CommandLine for parsing Hadoop MR-specific args.102 */103privatevoid prepareHadoopMRJob(finalGiraphJob job, final CommandLine cmd)
104throws Exception {
105if (cmd.hasOption("vof") || cmd.hasOption("eof")) {
106if (cmd.hasOption("op")) {
107 FileOutputFormatUtil.setOutputPath(job.getInternalJob(),
108new Path(cmd.getOptionValue("op")));
109 }
110 }
111if (cmd.hasOption("cf")) {
112 DistributedCache.addCacheFile(new URI(cmd.getOptionValue("cf")),
113 job.getConfiguration());
114 }
115 }
116117/**118 * Execute GiraphRunner.119 *120 * @param args Typically command line arguments.121 * @throws Exception Any exceptions thrown.122 */123publicstaticvoid main(String[] args) throws Exception {
124 System.exit(ToolRunner.run(newGiraphRunner(), args));
125 }
126 }