helloworld in twill

helloworld in twill

  1. install yarn and twill:
    yarn http://www.powerxing.com/install-hadoop-in-centos/
    zookeeper
  2. helloworld
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;

public class HelloWorld {
    public static final Logger LOG = LoggerFactory.getLogger(HelloWorld.class);

    /**
     * Hello World runnable that is provided to TwillRunnerService to be run.
     */
    private static class HelloWorldRunnable extends AbstractTwillRunnable {
        @Override
        public void run() {
            while(true) {
                LOG.info("Hello World. My first distributed application.");
            }
//            LOG.info("Hello World. My first distributed application.");
        }

        @Override
        public void stop() {
        }
    }

    public static void main(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 variable
        if (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 {
        @Override
        public boolean accept(String className, URL classUrl, URL classPathUrl) {
            // exclude hadoop but not hbase package
            return !(className.startsWith("org.apache.hadoop") && !className.startsWith("org.apache.hadoop.hbase"));
        }
    }
}
  1. 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
  2. Unix “ln -s” command not permitted in OSX El Capitan Beta3
    A:
    1. restart PC and keep press Command + R
    2. open terminal and input csrutil disable
    3. restart PC again.

finish

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值