PageRenderTime 37ms CodeModel.GetById 13ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/stats/StatsFactory.java

#
Java | 116 lines | 61 code | 16 blank | 39 comment | 8 complexity | fe3773d830eca4bbb75b5251ecc42aa9 MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.hive.ql.stats;
 20
 21import java.io.Serializable;
 22
 23import org.apache.commons.logging.Log;
 24import org.apache.commons.logging.LogFactory;
 25import org.apache.hadoop.conf.Configuration;
 26import org.apache.hadoop.hive.common.JavaUtils;
 27import org.apache.hadoop.hive.conf.HiveConf;
 28import org.apache.hadoop.util.ReflectionUtils;
 29
 30/**
 31 * A factory of stats publisher and aggregator implementations of the
 32 * StatsPublisher and StatsAggregator interfaces.
 33 */
 34public final class StatsFactory {
 35
 36  static final private Log LOG = LogFactory.getLog(StatsFactory.class.getName());
 37
 38  private static Class <? extends Serializable> publisherImplementation;
 39  private static Class <? extends Serializable> aggregatorImplementation;
 40  private static Configuration jobConf;
 41
 42  /**
 43   * Sets the paths of the implementation classes of publishing
 44   * and aggregation (IStatsPublisher and IStatsAggregator interfaces).
 45   * The paths are determined according to a configuration parameter which
 46   * is passed as the user input for choosing the implementation as MySQL, HBase, ...
 47   */
 48  public static boolean setImplementation(String configurationParam, Configuration conf) {
 49
 50    ClassLoader classLoader = JavaUtils.getClassLoader();
 51    if (configurationParam.equals(StatsSetupConst.HBASE_IMPL_CLASS_VAL)) {
 52      // Case: hbase
 53      try {
 54        publisherImplementation = (Class<? extends Serializable>)
 55          Class.forName("org.apache.hadoop.hive.hbase.HBaseStatsPublisher", true, classLoader);
 56
 57        aggregatorImplementation = (Class<? extends Serializable>)
 58          Class.forName("org.apache.hadoop.hive.hbase.HBaseStatsAggregator", true, classLoader);
 59      } catch (ClassNotFoundException e) {
 60        LOG.error("HBase Publisher/Aggregator classes cannot be loaded.", e);
 61        return false;
 62      }
 63    } else if (configurationParam.contains(StatsSetupConst.JDBC_IMPL_CLASS_VAL)) {
 64      // Case: jdbc:mysql or jdbc:derby
 65      try {
 66        publisherImplementation = (Class<? extends Serializable>)
 67          Class.forName("org.apache.hadoop.hive.ql.stats.jdbc.JDBCStatsPublisher", true, classLoader);
 68
 69        aggregatorImplementation = (Class<? extends Serializable>)
 70          Class.forName("org.apache.hadoop.hive.ql.stats.jdbc.JDBCStatsAggregator", true, classLoader);
 71      } catch (ClassNotFoundException e) {
 72        LOG.error("JDBC Publisher/Aggregator classes cannot be loaded.", e);
 73        return false;
 74      }
 75    } else {
 76      // try default stats publisher/aggregator
 77      String defPublisher = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_STATS_DEFAULT_PUBLISHER);
 78      String defAggregator = HiveConf.getVar(conf,  HiveConf.ConfVars.HIVE_STATS_DEFAULT_AGGREGATOR);
 79      // ERROR no default publisher/aggregator is defined
 80      if (defPublisher == null || defAggregator == null) {
 81        return false;
 82      }
 83      try{
 84        publisherImplementation = (Class<? extends Serializable>)
 85          Class.forName(defPublisher, true, classLoader);
 86        aggregatorImplementation = (Class<? extends Serializable>)
 87          Class.forName(defAggregator, true, classLoader);
 88      } catch (ClassNotFoundException e) {
 89        LOG.error("JDBC Publisher/Aggregator classes cannot be loaded.", e);
 90        return false;
 91      }
 92    }
 93
 94    jobConf = conf;
 95    return true;
 96  }
 97
 98  /**
 99   * Returns a Stats publisher implementation class for the IStatsPublisher interface
100   * For example HBaseStatsPublisher for the HBase implementation
101   */
102  public static StatsPublisher getStatsPublisher() {
103
104    return (StatsPublisher) ReflectionUtils.newInstance(publisherImplementation, jobConf);
105  }
106
107  /**
108   * Returns a Stats Aggregator implementation class for the IStatsAggregator interface
109   * For example HBaseStatsAggregator for the HBase implementation
110   */
111  public static StatsAggregator getStatsAggregator() {
112
113    return (StatsAggregator) ReflectionUtils.newInstance(aggregatorImplementation, jobConf);
114  }
115
116}