Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TableLoader.java
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.catalog;
16 
17 import java.util.EnumSet;
18 
19 import org.apache.hadoop.hive.metastore.TableType;
20 import org.apache.hadoop.hive.metastore.api.NoSuchObjectException;
21 import org.apache.log4j.Logger;
22 
24 
29 public class TableLoader {
30  private static final Logger LOG = Logger.getLogger(TableLoader.class);
31 
32  // Set of supported table types.
33  private static EnumSet<TableType> SUPPORTED_TABLE_TYPES = EnumSet.of(
34  TableType.EXTERNAL_TABLE, TableType.MANAGED_TABLE, TableType.VIRTUAL_VIEW);
35 
37 
38  // Lock used to serialize calls to the Hive MetaStore to work around MetaStore
39  // concurrency bugs. Currently used to serialize calls to "getTable()" due to
40  // HIVE-5457.
41  private static final Object metastoreAccessLock_ = new Object();
42 
44  catalog_ = catalog;
45  }
46 
58  public Table load(Db db, String tblName, Table cachedValue) {
59  String fullTblName = db.getName() + "." + tblName;
60  LOG.info("Loading metadata for: " + fullTblName);
61  MetaStoreClient msClient = null;
62  Table table;
63  // turn all exceptions into TableLoadingException
64  try {
65  msClient = catalog_.getMetaStoreClient();
66  org.apache.hadoop.hive.metastore.api.Table msTbl = null;
67  // All calls to getTable() need to be serialized due to HIVE-5457.
68  synchronized (metastoreAccessLock_) {
69  msTbl = msClient.getHiveClient().getTable(db.getName(), tblName);
70  }
71  // Check that the Hive TableType is supported
72  TableType tableType = TableType.valueOf(msTbl.getTableType());
73  if (!SUPPORTED_TABLE_TYPES.contains(tableType)) {
74  throw new TableLoadingException(String.format(
75  "Unsupported table type '%s' for: %s", tableType, fullTblName));
76  }
77 
78  // Create a table of appropriate type and have it load itself
79  table = Table.fromMetastoreTable(catalog_.getNextTableId(), db, msTbl);
80  if (table == null) {
81  throw new TableLoadingException(
82  "Unrecognized table type for table: " + fullTblName);
83  }
84  table.load(cachedValue, msClient.getHiveClient(), msTbl);
85  table.validate();
86  } catch (TableLoadingException e) {
87  table = IncompleteTable.createFailedMetadataLoadTable(
88  TableId.createInvalidId(), db, tblName, e);
89  } catch (NoSuchObjectException e) {
90  TableLoadingException tableDoesNotExist = new TableLoadingException(
91  "Table " + fullTblName + " no longer exists in the Hive MetaStore. " +
92  "Run 'invalidate metadata " + fullTblName + "' to update the Impala " +
93  "catalog.");
94  table = IncompleteTable.createFailedMetadataLoadTable(
95  TableId.createInvalidId(), db, tblName, tableDoesNotExist);
96  } catch (Exception e) {
97  table = IncompleteTable.createFailedMetadataLoadTable(
98  catalog_.getNextTableId(), db, tblName, new TableLoadingException(
99  "Failed to load metadata for table: " + fullTblName + ". Running " +
100  "'invalidate metadata " + fullTblName + "' may resolve this problem.", e));
101  } finally {
102  if (msClient != null) msClient.release();
103  }
104  return table;
105  }
106 }
static EnumSet< TableType > SUPPORTED_TABLE_TYPES
Table load(Db db, String tblName, Table cachedValue)
TableLoader(CatalogServiceCatalog catalog)
final CatalogServiceCatalog catalog_