public String toThriftJSONString() throws IOException {
    org.apache.hadoop.hive.ql.plan.api.Query q = getQueryPlan();
    try {
      TMemoryBuffer tmb = new TMemoryBuffer(q.toString().length() * 5);
      TJSONProtocol oprot = new TJSONProtocol(tmb);
      q.write(oprot);
      return tmb.toString(StandardCharsets.UTF_8);
    } catch (TException e) {
      LOG.warn("Unable to produce query plan Thrift string", e);
      return q.toString();
    }

  }
Extract all the counters from tasks and operators.
ql/src/java/org/apache/hadoop/hive/ql/QueryPlan.java