presto(三)——客户端http请求

接上一篇文章 presto——词法解析
process(queryRunner, split.statement(), outputFormat, false);
具体实现:

 private static void process(QueryRunner queryRunner, 
                  String sql, OutputFormat outputFormat, 
                  boolean interactive)
    {
         //我们来关注startQuery方法到底在搞什么鬼
        try (Query query = queryRunner.startQuery(sql)) {
        .......
    }

public Query startQuery(String query)
    {
        return new Query(startInternalQuery(query));
    }

public StatementClient startInternalQuery(String query)
    {
        return new StatementClient(httpClient, 
                      queryResultsCodec, session.get(), query);
    }

1、StatementClient构造方法

public StatementClient(HttpClient httpClient, JsonCodec queryResultsCodec, ClientSession session, String query)
    {
        requireNonNull(httpClient, "httpClient is null");
        requireNonNull(queryResultsCodec, "queryResultsCodec is null");
        requireNonNull(session, "session is null");
        requireNonNull(query, "query is null");

        //在QueryRunner的create方法,读取命令行的参数实例化httpClient
        //主要包括:代理ip端口、通信算法等信息
        this.httpClient = httpClient;
        this.responseHandler = createFullJsonResponseHandler(queryResultsCodec);
         ....这里省略了几行...

        //session是命令行敲入的协调器的地址和服务器--server参数
        //uri=http://localhost:8080/v1/statement
        //method = POST
        //head={X-Presto-Time-Zone=[Asia/Shanghai], User-Agent=[StatementClient/unknown], X-Presto-Source=[presto-cli], X-Presto-Language=[zh-CN], X-Presto-User=[tinygao], X-Presto-Transaction-Id=[NONE]}
        //body =  query的字节数组
        Request request = buildQueryRequest(session, query);
        //远程调用http://localhost:8080/v1/statement  重点
        JsonResponse response = httpClient.execute(request, responseHandler);

        if (response.getStatusCode() != HttpStatus.OK.code() || !response.hasValue()) {
            throw requestFailedException("starting query", request, response);
        }
        //处理response数据
        processResponse(response);
    }

2、 远程请求分析

httpClient.execute(request, responseHandler);
请求这个http://localhost:8080/v1/statement

1、这个服务什么时候起来
2、客户端调用之后服务器做了什么事情
3、客户端对返回的结果做了什么

你可能感兴趣的:(presto(三)——客户端http请求)