sourceMetaStore.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE);
sourceMetaStore.createDatabase(HCatCreateDBDesc.create(dbName).build());
List<HCatFieldSchema> columnSchema = new ArrayList<HCatFieldSchema>(
Arrays.asList(new HCatFieldSchema("foo", Type.INT, ""),
new HCatFieldSchema("bar", Type.STRING, "")));
List<HCatFieldSchema> partitionSchema = Arrays.asList(new HCatFieldSchema("dt", Type.STRING, ""),
new HCatFieldSchema("grid", Type.STRING, ""));
HCatTable sourceTable = new HCatTable(dbName, tableName).cols(columnSchema)
.partCols(partitionSchema)
.comment("Source table.");
sourceMetaStore.createTable(HCatCreateTableDesc.create(sourceTable).build());
// Verify that the sourceTable was created successfully.
sourceTable = sourceMetaStore.getTable(dbName, tableName);
assertNotNull("Table couldn't be queried for. ", sourceTable);
// Partitions added now should inherit table-schema, properties, etc.
Map<String, String> partitionSpec_1 = new HashMap<String, String>();
partitionSpec_1.put("grid", "AB");
partitionSpec_1.put("dt", "2011_12_31");
HCatPartition sourcePartition_1 = new HCatPartition(sourceTable, partitionSpec_1, "");
sourceMetaStore.addPartition(HCatAddPartitionDesc.create(sourcePartition_1).build());
assertEquals("Unexpected number of partitions. ",
sourceMetaStore.getPartitions(dbName, tableName).size(), 1);
// Verify that partition_1 was added correctly, and properties were inherited from the HCatTable.
HCatPartition addedPartition_1 = sourceMetaStore.getPartition(dbName, tableName, partitionSpec_1);
assertEquals("Column schema doesn't match.", addedPartition_1.getColumns(), sourceTable.getCols());
assertEquals("InputFormat doesn't match.", addedPartition_1.getInputFormat(), sourceTable.getInputFileFormat());
assertEquals("OutputFormat doesn't match.", addedPartition_1.getOutputFormat(), sourceTable.getOutputFileFormat());
assertEquals("SerDe doesn't match.", addedPartition_1.getSerDe(), sourceTable.getSerdeLib());
assertEquals("SerDe params don't match.", addedPartition_1.getSerdeParams(), sourceTable.getSerdeParams());
// Replicate table definition.
HCatClient targetMetaStore = HCatClient.create(new Configuration(replicationTargetHCatConf));
targetMetaStore.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE);
targetMetaStore.createDatabase(HCatCreateDBDesc.create(dbName).build());
// Make a copy of the source-table, as would be done across class-loaders.
HCatTable targetTable = targetMetaStore.deserializeTable(sourceMetaStore.serializeTable(sourceTable));
targetMetaStore.createTable(HCatCreateTableDesc.create(targetTable).build());
targetTable = targetMetaStore.getTable(dbName, tableName);
assertEquals("Created table doesn't match the source.",
targetTable.diff(sourceTable), HCatTable.NO_DIFF);
// Modify Table schema at the source.
List<HCatFieldSchema> newColumnSchema = new ArrayList<HCatFieldSchema>(columnSchema);
newColumnSchema.add(new HCatFieldSchema("goo_new", Type.DOUBLE, ""));
Map<String, String> tableParams = new HashMap<String, String>(1);
tableParams.put("orc.compress", "ZLIB");
sourceTable.cols(newColumnSchema) // Add a column.
.fileFormat("orcfile") // Change SerDe, File I/O formats.
.tblProps(tableParams)