@Override public DatasetDescriptor update(String name, DatasetDescriptor descriptor) { Compatibility.checkDatasetName(name); Compatibility.checkDescriptor(descriptor); if (!exists(name)) { throw new DatasetNotFoundException("Table not found: " + name); } Table table = getHcat().getTable(HiveUtils.DEFAULT_DB, name); HiveUtils.updateTableSchema(table, descriptor); getHcat().alterTable(table); return descriptor; }
@Override public Map<String, Object> readRow(String tableName, Object keyObject) throws Exception { if (!(keyObject instanceof Map) && !(keyObject instanceof String) && !(keyObject instanceof byte[])) { throw new IllegalArgumentException( "Unsupported key type - " + keyObject.getClass().getName()); } Map<String, Object> result; HCatTable table = hcatClient.getTable("default", tableName); String hbaseTableName = HiveUtils.getTableName(table); HTableInterface tableInterface = tableFactory.getTable(hbaseConfiguration, hbaseTableName); try { List<HCatFieldSchema> columns = table.getCols(); HCatFieldSchema keyColumn = columns.get(0); // we use the serializer to build the row key HiveSerializer serializer = new HiveSerializer(table); final byte[] rowKey; if (keyObject instanceof Map) { rowKey = serializer.serializeHiveType(keyColumn, null, keyObject, 0); } else if (keyObject instanceof String) { rowKey = Bytes.toBytes((String) keyObject); } else { rowKey = (byte[]) keyObject; } Get get = new Get(rowKey); get.setCacheBlocks(true); get.setMaxVersions(1); Result dbResult = tableInterface.get(get); HiveDeserializer deserializer = new HiveDeserializer(table, dbResult); result = deserializer.deserialize(); result.put("__rowkey", rowKey); } finally { tableInterface.close(); } return result; }
@Override public Collection<String> list() { Collection<String> tables = getHcat().getAllTables(HiveUtils.DEFAULT_DB); List<String> readableTables = Lists.newArrayList(); for (String name : tables) { Table table = getHcat().getTable(HiveUtils.DEFAULT_DB, name); if (isManaged(table) || isExternal(table)) { // readable table types try { // get a descriptor for the table. if this succeeds, it is readable HiveUtils.descriptorForTable(conf, table); readableTables.add(name); } catch (DatasetException e) { // not a readable table } catch (IllegalStateException e) { // not a readable table } catch (IllegalArgumentException e) { // not a readable table } catch (UnsupportedOperationException e) { // not a readable table } } } return readableTables; }