PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.0.0-rc0/hive/external/hbase-handler/src/java/org/apache/hadoop/hive/hbase/HBaseStatsPublisher.java

#
Java | 140 lines | 87 code | 20 blank | 33 comment | 5 complexity | 3c75f4a15cc7c20ab3581169045b3b72 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.hbase;
  19. import java.io.IOException;
  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.hbase.HBaseConfiguration;
  24. import org.apache.hadoop.hbase.HColumnDescriptor;
  25. import org.apache.hadoop.hbase.HTableDescriptor;
  26. import org.apache.hadoop.hbase.client.Get;
  27. import org.apache.hadoop.hbase.client.HBaseAdmin;
  28. import org.apache.hadoop.hbase.client.HTable;
  29. import org.apache.hadoop.hbase.client.Put;
  30. import org.apache.hadoop.hbase.client.Result;
  31. import org.apache.hadoop.hbase.client.RowLock;
  32. import org.apache.hadoop.hbase.util.Bytes;
  33. import org.apache.hadoop.hive.ql.stats.*;
  34. /**
  35. * A class that implements the StatsPublisher interface through HBase.
  36. */
  37. public class HBaseStatsPublisher implements StatsPublisher {
  38. private HTable htable;
  39. private byte[] rowCountFamily, rowCountColumn;
  40. private final Log LOG = LogFactory.getLog(this.getClass().getName());
  41. /**
  42. * Does the necessary HBase initializations.
  43. */
  44. public boolean connect(Configuration hiveconf) {
  45. try {
  46. HBaseConfiguration hbaseConf = new HBaseConfiguration(hiveconf);
  47. HBaseAdmin hbase = new HBaseAdmin(hbaseConf);
  48. rowCountFamily = Bytes.toBytes(HBaseStatsSetupConstants.PART_STAT_ROW_COUNT_COLUMN_FAMILY);
  49. rowCountColumn = Bytes.toBytes(HBaseStatsSetupConstants.PART_STAT_ROW_COUNT_COLUMN_NAME);
  50. htable = new HTable(HBaseStatsSetupConstants.PART_STAT_TABLE_NAME);
  51. // for performance reason, defer update until the closeConnection
  52. htable.setAutoFlush(false);
  53. } catch (IOException e) {
  54. LOG.error("Error during HBase connection. " + e);
  55. return false;
  56. }
  57. return true;
  58. }
  59. /**
  60. * Writes temporary statistics into HBase;
  61. */
  62. public boolean publishStat(String rowID, String key, String value) {
  63. if (key != StatsSetupConst.ROW_COUNT) {
  64. LOG.warn("Warning. Invalid statistic. Currently " +
  65. "row count is the only supported statistic");
  66. return false;
  67. }
  68. // Write in HBase
  69. try {
  70. Get get = new Get(Bytes.toBytes(rowID));
  71. Result result = htable.get(get);
  72. int val = Integer.parseInt(value);
  73. int oldVal = 0;
  74. if (!result.isEmpty()) {
  75. oldVal = Integer.parseInt(Bytes.toString(result.getValue(rowCountFamily, rowCountColumn)));
  76. }
  77. if (oldVal < val) {
  78. Put row = new Put(Bytes.toBytes(rowID));
  79. row.add(rowCountFamily, rowCountColumn, Bytes.toBytes(Integer.toString(val)));
  80. htable.put(row);
  81. }
  82. return true;
  83. } catch (IOException e) {
  84. LOG.error("Error during publishing statistics. " + e);
  85. return false;
  86. }
  87. }
  88. public boolean closeConnection() {
  89. // batch update
  90. try {
  91. htable.flushCommits();
  92. return true;
  93. } catch (IOException e) {
  94. LOG.error("Cannot commit changes in stats publishing.", e);
  95. return false;
  96. }
  97. }
  98. /**
  99. * Does the necessary HBase initializations.
  100. */
  101. public boolean init(Configuration hiveconf) {
  102. try {
  103. HBaseConfiguration hbaseConf = new HBaseConfiguration(hiveconf);
  104. HBaseAdmin hbase = new HBaseAdmin(hbaseConf);
  105. rowCountFamily = Bytes.toBytes(HBaseStatsSetupConstants.PART_STAT_ROW_COUNT_COLUMN_FAMILY);
  106. rowCountColumn = Bytes.toBytes(HBaseStatsSetupConstants.PART_STAT_ROW_COUNT_COLUMN_NAME);
  107. // Creating table if not exists
  108. if (!hbase.tableExists(HBaseStatsSetupConstants.PART_STAT_TABLE_NAME)) {
  109. HTableDescriptor table = new HTableDescriptor(HBaseStatsSetupConstants.PART_STAT_TABLE_NAME);
  110. HColumnDescriptor rowCount = new HColumnDescriptor(rowCountFamily);
  111. table.addFamily(rowCount);
  112. hbase.createTable(table);
  113. }
  114. } catch (IOException e) {
  115. LOG.error("Error during HBase initialization. " + e);
  116. return false;
  117. }
  118. return true;
  119. }
  120. }