private Optional<Table> loadTable(HiveTableName hiveTableName) throws Exception { try { return retry() .stopOn(NoSuchObjectException.class, HiveViewNotSupportedException.class) .stopOnIllegalExceptions() .run( "getTable", stats .getGetTable() .wrap( () -> { try (HiveMetastoreClient client = clientProvider.createMetastoreClient()) { Table table = client.getTable( hiveTableName.getDatabaseName(), hiveTableName.getTableName()); if (table.getTableType().equals(TableType.VIRTUAL_VIEW.name()) && (!isPrestoView(table))) { throw new HiveViewNotSupportedException( new SchemaTableName( hiveTableName.getDatabaseName(), hiveTableName.getTableName())); } return Optional.of(table); } })); } catch (NoSuchObjectException e) { return Optional.empty(); } catch (TException e) { throw new PrestoException(HIVE_METASTORE_ERROR, e); } }
public static Table fromMetastoreApiTable(org.apache.hadoop.hive.metastore.api.Table table) { StorageDescriptor storageDescriptor = table.getSd(); if (storageDescriptor == null) { throw new PrestoException(HIVE_INVALID_METADATA, "Table is missing storage descriptor"); } Table.Builder tableBuilder = Table.builder() .setDatabaseName(table.getDbName()) .setTableName(table.getTableName()) .setOwner(nullToEmpty(table.getOwner())) .setTableType(table.getTableType()) .setDataColumns( storageDescriptor .getCols() .stream() .map(MetastoreUtil::fromMetastoreApiFieldSchema) .collect(toList())) .setPartitionColumns( table .getPartitionKeys() .stream() .map(MetastoreUtil::fromMetastoreApiFieldSchema) .collect(toList())) .setParameters( table.getParameters() == null ? ImmutableMap.of() : table.getParameters()) .setViewOriginalText(Optional.ofNullable(emptyToNull(table.getViewOriginalText()))) .setViewExpandedText(Optional.ofNullable(emptyToNull(table.getViewExpandedText()))); fromMetastoreApiStorageDescriptor( storageDescriptor, tableBuilder.getStorageBuilder(), table.getTableName()); return tableBuilder.build(); }
@Override public synchronized void createTable(Table table) { SchemaTableName schemaTableName = new SchemaTableName(table.getDbName(), table.getTableName()); Table tableCopy = table.deepCopy(); if (tableCopy.getSd() == null) { tableCopy.setSd(new StorageDescriptor()); } else if (tableCopy.getSd().getLocation() != null) { File directory = new File(new Path(tableCopy.getSd().getLocation()).toUri()); checkArgument(directory.exists(), "Table directory does not exist"); checkArgument( isParentDir(directory, baseDirectory), "Table directory must be inside of the metastore base directory"); } if (relations.putIfAbsent(schemaTableName, tableCopy) != null) { throw new TableAlreadyExistsException(schemaTableName); } if (tableCopy.getTableType().equals(TableType.VIRTUAL_VIEW.name())) { views.put(schemaTableName, tableCopy); } PrincipalPrivilegeSet privileges = table.getPrivileges(); if (privileges != null) { for (Entry<String, List<PrivilegeGrantInfo>> entry : privileges.getUserPrivileges().entrySet()) { String user = entry.getKey(); Set<HivePrivilegeInfo> userPrivileges = entry .getValue() .stream() .map(HivePrivilegeInfo::parsePrivilege) .flatMap(Collection::stream) .collect(toImmutableSet()); setTablePrivileges(user, USER, table.getDbName(), table.getTableName(), userPrivileges); } for (Entry<String, List<PrivilegeGrantInfo>> entry : privileges.getRolePrivileges().entrySet()) { String role = entry.getKey(); Set<HivePrivilegeInfo> rolePrivileges = entry .getValue() .stream() .map(HivePrivilegeInfo::parsePrivilege) .flatMap(Collection::stream) .collect(toImmutableSet()); setTablePrivileges(role, ROLE, table.getDbName(), table.getTableName(), rolePrivileges); } } }
private ConnectorTableMetadata getTableMetadata(SchemaTableName tableName) { try { Table table = metastore.getTable(tableName.getSchemaName(), tableName.getTableName()); if (table.getTableType().equals(TableType.VIRTUAL_VIEW.name())) { throw new TableNotFoundException(tableName); } List<HiveColumnHandle> handles = hiveColumnHandles(typeManager, connectorId, table, false); List<ColumnMetadata> columns = ImmutableList.copyOf(transform(handles, columnMetadataGetter(table, typeManager))); return new ConnectorTableMetadata(tableName, columns, table.getOwner()); } catch (NoSuchObjectException e) { throw new TableNotFoundException(tableName); } }