presto(一)——cli之命令行解析

1、官网给出的启动CLI

presto-cli/target/presto-cli-*-executable.jar

肯定有main方法,于是我们去找presto-cil项目中的main方法。

presto(一)——cli之命令行解析_第1张图片
有两个类有main方法

应该是Presto.java中的那个。

public static void main(String[] args)
            throws Exception
    {
        //利用airline在做cli命令行的解析
        Console console = singleCommand(Console.class).parse(args);

        //如果命令行中的可选项为:--help或者version则跳过
        if (console.helpOption.showHelpIfRequested() ||
                console.versionOption.showVersionIfRequested()) {
            return;
        }

        //启动执行cli
        console.run();
    }

2、airline命令行解析框架

import io.airlift.airline.Command;
import io.airlift.airline.HelpOption;
import io.airlift.airline.Option;
import io.airlift.airline.SingleCommand;

import javax.inject.Inject;

/**
 * Created by gsd on 2017/2/12.
 */
@Command(name = "ping", description = "network test utility")
public class Ping
{
    @Inject
    public HelpOption helpOption;
  // preso就是这么弄的,只不过他封装在ClientOptions类中
    @Option(name = "--server", title = "server", description = "Presto server location (default: localhost:8080)")
    public String server = "localhost:8080";
    @Option(name = {"-c", "--count"}, description = "Send count packets")
    public int count = 1;

    public static void main(String... args)
    {
        Ping ping = SingleCommand.singleCommand(Ping.class).parse(args);
        if (ping.helpOption.showHelpIfRequested()) {
            return;
        }
        ping.run();
    }

    public void run()
    {
        System.out.println("Ping count: " + count);
    }
}

执行: java --help


presto(一)——cli之命令行解析_第2张图片
把presto的cli说明打印部分出来

重要还是run方法

3、presto cli console中的run方法

 public void run()
    {
        //建立连接,通过--server参数来识别127.0.0.1:8080,
        // 如果没有端口号默认会给你80。如下是我们经常敲入的
        // ./presto-cli --server 127.0.0.1:8080 --catalog hive --schema default
        ClientSession session = clientOptions.toClientSession();
        KerberosConfig kerberosConfig = clientOptions.toKerberosConfig();
        //是否带有执行语句
        boolean hasQuery = !Strings.isNullOrEmpty(clientOptions.execute);
        //是否来自文件
        boolean isFromFile = !Strings.isNullOrEmpty(clientOptions.file);

        //既不带有执行语句也不来自文件,对输出流支持ANSI sequences.
        if (!hasQuery && !isFromFile) {
            AnsiConsole.systemInstall();
        }

        //初始化日志
        initializeLogging(clientOptions.logLevelsFile);

        String query = clientOptions.execute;
        //如果带有查询语句
        if (hasQuery) {
            query += ";";
        }
        //如果来自文件
        if (isFromFile) {
            //如果带有查询语句,则抛出异常
            if (hasQuery) {
                throw new RuntimeException("both --execute and --file specified");
            }
            try {
                query = Files.toString(new File(clientOptions.file), UTF_8);
                hasQuery = true;
            }
            catch (IOException e) {
                throw new RuntimeException(format("Error reading from file %s: %s", clientOptions.file, e.getMessage()));
            }
        }

        //默认是false
        AtomicBoolean exiting = new AtomicBoolean();
        //启动一个线程,如果出现异常,则中断主线程
        interruptThreadOnExit(Thread.currentThread(), exiting);

        try (QueryRunner queryRunner = QueryRunner.create(
                session,
                Optional.ofNullable(clientOptions.socksProxy),
                Optional.ofNullable(clientOptions.keystorePath),
                Optional.ofNullable(clientOptions.keystorePassword),
                Optional.ofNullable(clientOptions.truststorePath),
                Optional.ofNullable(clientOptions.truststorePassword),
                Optional.ofNullable(clientOptions.krb5Principal),
                Optional.ofNullable(clientOptions.krb5RemoteServiceName),
                clientOptions.authenticationEnabled,
                kerberosConfig)) {
            if (hasQuery) {
                //执行完语句并退出
                executeCommand(queryRunner, query, clientOptions.outputFormat);
            }
            else {
                //阻塞等待命令行sql语句输入
                runConsole(queryRunner, session, exiting);
            }
        }
    }

4、下一个博文,执行语句。

将基于:executeCommand(queryRunner, query, clientOptions.outputFormat);

你可能感兴趣的:(presto(一)——cli之命令行解析)