import java.io.PrintWriter;
import java.net.URL;
import java.util.List;
import java.util.concurrent.ExecutionException;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.twill.api.AbstractTwillRunnable;
import org.apache.twill.api.ClassAcceptor;
import org.apache.twill.api.TwillController;
import org.apache.twill.api.TwillRunnerService;
import org.apache.twill.api.logging.PrinterLogHandler;
import org.apache.twill.internal.Services;
import org.apache.twill.yarn.YarnTwillRunnerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Service;
publicclassHelloWorld {publicstaticfinal Logger LOG = LoggerFactory.getLogger(HelloWorld.class);
/**
* Hello World runnable that is provided to TwillRunnerService to be run.
*/privatestaticclassHelloWorldRunnableextendsAbstractTwillRunnable {@Overridepublicvoidrun() {
while(true) {
LOG.info("Hello World. My first distributed application.");
}
// LOG.info("Hello World. My first distributed application.");
}
@Overridepublicvoidstop() {
}
}
publicstaticvoidmain(String[] args) {
if (args.length < 1) {
System.err.println("Arguments format: <host:port of zookeeper server>");
System.exit(1);
}
String zkStr = args[0];
String home = System.getProperty("hadoop.home.dir");
// fall back to the system/user-global env variableif (home == null) {
home = System.getenv("HADOOP_HOME");
}
// System.setProperty("hadoop.home.dir","/usr/local/hadoop");// String zkStr = "localhost:2181";
YarnConfiguration yarnConfiguration = new YarnConfiguration();
final TwillRunnerService twillRunner = new YarnTwillRunnerService(yarnConfiguration, zkStr);
twillRunner.start();
String yarnClasspath =
yarnConfiguration.get(YarnConfiguration.YARN_APPLICATION_CLASSPATH,
Joiner.on(",").join(YarnConfiguration.DEFAULT_YARN_APPLICATION_CLASSPATH));
List<String> applicationClassPaths = Lists.newArrayList();
Iterables.addAll(applicationClassPaths, Splitter.on(",").split(yarnClasspath));
final TwillController controller = twillRunner.prepare(new HelloWorldRunnable())
.addLogHandler(new PrinterLogHandler(new PrintWriter(System.out, true)))
.withApplicationClassPaths(applicationClassPaths)
.withBundlerClassAcceptor(new HadoopClassExcluder())
.start();
try {
Services.getCompletionFuture((Service) controller).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
static class HadoopClassExcluder extends ClassAcceptor {
@Overridepublicbooleanaccept(String className, URL classUrl, URL classPathUrl) {
// exclude hadoop but not hbase packagereturn !(className.startsWith("org.apache.hadoop") && !className.startsWith("org.apache.hadoop.hbase"));
}
}
}
check the log of container can not find command /bin/java A: java path in mac is /usr/bin/java. So I decided to add a soft link between /bin/java and /usr/bin/java sudo ln -s /usr/bin/java /bin/java
Unix “ln -s” command not permitted in OSX El Capitan Beta3 A: