PageRenderTime 34ms CodeModel.GetById 11ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.ql.stats;
  19. import java.io.Serializable;
  20. import org.apache.commons.logging.Log;
  21. import org.apache.commons.logging.LogFactory;
  22. import org.apache.hadoop.conf.Configuration;
  23. import org.apache.hadoop.hive.common.JavaUtils;
  24. import org.apache.hadoop.hive.conf.HiveConf;
  25. import org.apache.hadoop.util.ReflectionUtils;
  26. /**
  27. * A factory of stats publisher and aggregator implementations of the
  28. * StatsPublisher and StatsAggregator interfaces.
  29. */
  30. public final class StatsFactory {
  31. static final private Log LOG = LogFactory.getLog(StatsFactory.class.getName());
  32. private static Class <? extends Serializable> publisherImplementation;
  33. private static Class <? extends Serializable> aggregatorImplementation;
  34. private static Configuration jobConf;
  35. /**
  36. * Sets the paths of the implementation classes of publishing
  37. * and aggregation (IStatsPublisher and IStatsAggregator interfaces).
  38. * The paths are determined according to a configuration parameter which
  39. * is passed as the user input for choosing the implementation as MySQL, HBase, ...
  40. */
  41. public static boolean setImplementation(String configurationParam, Configuration conf) {
  42. ClassLoader classLoader = JavaUtils.getClassLoader();
  43. if (configurationParam.equals(StatsSetupConst.HBASE_IMPL_CLASS_VAL)) {
  44. // Case: hbase
  45. try {
  46. publisherImplementation = (Class<? extends Serializable>)
  47. Class.forName("org.apache.hadoop.hive.hbase.HBaseStatsPublisher", true, classLoader);
  48. aggregatorImplementation = (Class<? extends Serializable>)
  49. Class.forName("org.apache.hadoop.hive.hbase.HBaseStatsAggregator", true, classLoader);
  50. } catch (ClassNotFoundException e) {
  51. LOG.error("HBase Publisher/Aggregator classes cannot be loaded.", e);
  52. return false;
  53. }
  54. } else if (configurationParam.contains(StatsSetupConst.JDBC_IMPL_CLASS_VAL)) {
  55. // Case: jdbc:mysql or jdbc:derby
  56. try {
  57. publisherImplementation = (Class<? extends Serializable>)
  58. Class.forName("org.apache.hadoop.hive.ql.stats.jdbc.JDBCStatsPublisher", true, classLoader);
  59. aggregatorImplementation = (Class<? extends Serializable>)
  60. Class.forName("org.apache.hadoop.hive.ql.stats.jdbc.JDBCStatsAggregator", true, classLoader);
  61. } catch (ClassNotFoundException e) {
  62. LOG.error("JDBC Publisher/Aggregator classes cannot be loaded.", e);
  63. return false;
  64. }
  65. } else {
  66. // try default stats publisher/aggregator
  67. String defPublisher = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_STATS_DEFAULT_PUBLISHER);
  68. String defAggregator = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_STATS_DEFAULT_AGGREGATOR);
  69. // ERROR no default publisher/aggregator is defined
  70. if (defPublisher == null || defAggregator == null) {
  71. return false;
  72. }
  73. try{
  74. publisherImplementation = (Class<? extends Serializable>)
  75. Class.forName(defPublisher, true, classLoader);
  76. aggregatorImplementation = (Class<? extends Serializable>)
  77. Class.forName(defAggregator, true, classLoader);
  78. } catch (ClassNotFoundException e) {
  79. LOG.error("JDBC Publisher/Aggregator classes cannot be loaded.", e);
  80. return false;
  81. }
  82. }
  83. jobConf = conf;
  84. return true;
  85. }
  86. /**
  87. * Returns a Stats publisher implementation class for the IStatsPublisher interface
  88. * For example HBaseStatsPublisher for the HBase implementation
  89. */
  90. public static StatsPublisher getStatsPublisher() {
  91. return (StatsPublisher) ReflectionUtils.newInstance(publisherImplementation, jobConf);
  92. }
  93. /**
  94. * Returns a Stats Aggregator implementation class for the IStatsAggregator interface
  95. * For example HBaseStatsAggregator for the HBase implementation
  96. */
  97. public static StatsAggregator getStatsAggregator() {
  98. return (StatsAggregator) ReflectionUtils.newInstance(aggregatorImplementation, jobConf);
  99. }
  100. }