hive的shims相關類分析

來源:互聯網
上載者:User

標籤:hive shims類


  在hive的源碼中經常可以看到shims相關的類,shims相關類是用來相容不同的hadoop和hive版本的,以HadoopShims為例org.apache.hadoop.hive.shims.HadoopShims是一個介面,具體的實作類別為


org.apache.hadoop.hive.shims.Hadoop20Shimsorg.apache.hadoop.hive.shims.Hadoop20SShimsorg.apache.hadoop.hive.shims.Hadoop23Shims

具體對應的那個shims類在ShimLoader類(org.apache.hadoop.hive.shims.ShimLoader)中有所定義,以HADOOP_SHIM_CLASSES 為例:

 /**   * The names of the classes for shimming Hadoop for each major version.   */  private static final HashMap<String, String> HADOOP_SHIM_CLASSES =      new HashMap<String, String>();  // 首先定義了一個hashmap  static {    HADOOP_SHIM_CLASSES.put( "0.20", "org.apache.hadoop.hive.shims.Hadoop20Shims" );  // 0.20    HADOOP_SHIM_CLASSES.put( "0.20S", "org.apache.hadoop.hive.shims.Hadoop20SShims" );  // 1.x和0.20 security版本的hadoop    HADOOP_SHIM_CLASSES.put( "0.23", "org.apache.hadoop.hive.shims.Hadoop23Shims" ); // 2.x版本的hadoop  } // 向hashmap中插入對應的項,key為version資訊,value為實際對應的類

ShimLoader類通過getHadoopShims方法返回具體的HadoopShims 

private static HadoopShims hadoopShims;......  public static synchronized HadoopShims getHadoopShims() {    if (hadoopShims == null) {      hadoopShims = loadShims(HADOOP_SHIM_CLASSES, HadoopShims.class); // 調用loadShims方法    }    return hadoopShims;  }

而loadShims方法通過判斷hadoop的版本,來擷取hashmap中對應的設定類

  private static <T> T loadShims(Map<String, String> classMap, Class<T> xface) {    String vers = getMajorVersion();  // 擷取hadoop的版本    String className = classMap.get(vers);    return createShim(className, xface);  }

具體哪個hadoop版本是由getMajorVersion擷取,比如我們線上使用的是2.0.0-cdh4.6.0,那麼getMajorVersion返回的值為0.23,對應的HadoopShims 實作類別為org.apache.hadoop.hive.shims.Hadoop23Shims

 public static String getMajorVersion() {    String vers = VersionInfo.getVersion();  // org.apache.hadoop.util.VersionInfo類的getVersion方法   比如2.0.0-cdh4.6.0    String[] parts = vers.split( "\\.");    if (parts. length < 2) {      throw new RuntimeException("Illegal Hadoop Version: " + vers +          " (expected A.B.* format)");    }    // Special handling for Hadoop 1.x and 2.x    switch (Integer.parseInt(parts[0])) {    case 0:      break;    case 1:      return "0.20S";    case 2:      return "0.23";    default:      throw new IllegalArgumentException("Unrecognized Hadoop major version number: " + vers);    }    String majorVersion = parts[0] + "." + parts[1];    // If we are running a security release, we won‘t have UnixUserGroupInformation    // (removed by HADOOP-6299 when switching to JAAS for Login)    try {      Class.forName("org.apache.hadoop.security.UnixUserGroupInformation" );    } catch (ClassNotFoundException cnf) {      if ("0.20".equals(majorVersion)) {        majorVersion += "S";      }    }    return majorVersion;  }

而不同的shims中規定了不同版本下的屬性和方法等,比如判斷是否localmode:

1). Hadoop20SShims 類(Hadoop 0.20 with Security和1.x版本 )

  @Override  public boolean isLocalMode(Configuration conf) {    return "local".equals(getJobLauncherRpcAddress(conf));  }  @Override  public String getJobLauncherRpcAddress( Configuration conf) { //通過判斷mapred.job.tracker的設定    return conf.get( "mapred.job.tracker");  }


2).Hadoop20Shims類 (Hadoop 0.20版本) 同上
3).Hadoop23Shims  (Hadoop 2.x版本)

  @Override  public boolean isLocalMode( Configuration conf) {    return "local".equals(conf.get("mapreduce.framework.name" ));   //根據mapreduce.framework.name設定  }

本文出自 “菜光光的部落格” 部落格,請務必保留此出處http://caiguangguang.blog.51cto.com/1652935/1564601

hive的shims相關類分析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.