@Test public void testObjectNotFoundException() throws Exception { try { HCatClient client = HCatClient.create(new Configuration(hcatConf)); String dbName = "testObjectNotFoundException_DBName"; String tableName = "testObjectNotFoundException_TableName"; client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE); try { // Test that fetching a non-existent db-name yields ObjectNotFound. client.getDatabase(dbName); assertTrue("Expected ObjectNotFoundException.", false); } catch (Exception exception) { LOG.info("Got exception: ", exception); assertTrue( "Expected ObjectNotFoundException. Got:" + exception.getClass(), exception instanceof ObjectNotFoundException); } client.createDatabase(HCatCreateDBDesc.create(dbName).build()); try { // Test that fetching a non-existent table-name yields ObjectNotFound. client.getTable(dbName, tableName); assertTrue("Expected ObjectNotFoundException.", false); } catch (Exception exception) { LOG.info("Got exception: ", exception); assertTrue( "Expected ObjectNotFoundException. Got:" + exception.getClass(), exception instanceof ObjectNotFoundException); } String partitionColumn = "part"; List<HCatFieldSchema> columns = Arrays.asList(new HCatFieldSchema("col", Type.STRING, "")); ArrayList<HCatFieldSchema> partitionColumns = new ArrayList<HCatFieldSchema>( Arrays.asList(new HCatFieldSchema(partitionColumn, Type.STRING, ""))); client.createTable( HCatCreateTableDesc.create(dbName, tableName, columns) .partCols(partitionColumns) .build()); Map<String, String> partitionSpec = new HashMap<String, String>(); partitionSpec.put(partitionColumn, "foobar"); try { // Test that fetching a non-existent partition yields ObjectNotFound. client.getPartition(dbName, tableName, partitionSpec); assertTrue("Expected ObjectNotFoundException.", false); } catch (Exception exception) { LOG.info("Got exception: ", exception); assertTrue( "Expected ObjectNotFoundException. Got:" + exception.getClass(), exception instanceof ObjectNotFoundException); } client.addPartition( HCatAddPartitionDesc.create(dbName, tableName, "", partitionSpec).build()); // Test that listPartitionsByFilter() returns an empty-set, if the filter selects no // partitions. assertEquals( "Expected empty set of partitions.", 0, client.listPartitionsByFilter(dbName, tableName, partitionColumn + " < 'foobar'").size()); try { // Test that listPartitionsByFilter() throws HCatException if the partition-key is // incorrect. partitionSpec.put("NonExistentKey", "foobar"); client.getPartition(dbName, tableName, partitionSpec); assertTrue("Expected HCatException.", false); } catch (Exception exception) { LOG.info("Got exception: ", exception); assertTrue( "Expected HCatException. Got:" + exception.getClass(), exception instanceof HCatException); assertFalse( "Did not expect ObjectNotFoundException.", exception instanceof ObjectNotFoundException); } } catch (Throwable t) { LOG.error("Unexpected exception!", t); assertTrue("Unexpected exception! " + t.getMessage(), false); } }
@Test public void testDropPartitionsWithPartialSpec() throws Exception { try { HCatClient client = HCatClient.create(new Configuration(hcatConf)); final String dbName = "myDb"; final String tableName = "myTable"; client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE); client.createDatabase(HCatCreateDBDesc.create(dbName).build()); List<HCatFieldSchema> columnSchema = 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, "")); client.createTable( HCatCreateTableDesc.create(dbName, tableName, columnSchema) .partCols(new ArrayList<HCatFieldSchema>(partitionSchema)) .build()); Map<String, String> partitionSpec = new HashMap<String, String>(); partitionSpec.put("grid", "AB"); partitionSpec.put("dt", "2011_12_31"); client.addPartition( HCatAddPartitionDesc.create(dbName, tableName, "", partitionSpec).build()); partitionSpec.put("grid", "AB"); partitionSpec.put("dt", "2012_01_01"); client.addPartition( HCatAddPartitionDesc.create(dbName, tableName, "", partitionSpec).build()); partitionSpec.put("dt", "2012_01_01"); partitionSpec.put("grid", "OB"); client.addPartition( HCatAddPartitionDesc.create(dbName, tableName, "", partitionSpec).build()); partitionSpec.put("dt", "2012_01_01"); partitionSpec.put("grid", "XB"); client.addPartition( HCatAddPartitionDesc.create(dbName, tableName, "", partitionSpec).build()); Map<String, String> partialPartitionSpec = new HashMap<String, String>(); partialPartitionSpec.put("dt", "2012_01_01"); client.dropPartitions(dbName, tableName, partialPartitionSpec, true); List<HCatPartition> partitions = client.getPartitions(dbName, tableName); assertEquals("Unexpected number of partitions.", 1, partitions.size()); assertArrayEquals( "Mismatched partition.", new String[] {"2011_12_31", "AB"}, partitions.get(0).getValues().toArray()); client.dropDatabase(dbName, false, HCatClient.DropDBMode.CASCADE); } catch (Exception unexpected) { LOG.error("Unexpected exception!", unexpected); assertTrue("Unexpected exception! " + unexpected.getMessage(), false); } }
@Test public void testPartitionsHCatClientImpl() throws Exception { HCatClient client = HCatClient.create(new Configuration(hcatConf)); String dbName = "ptnDB"; String tableName = "pageView"; client.dropDatabase(dbName, true, HCatClient.DropDBMode.CASCADE); HCatCreateDBDesc dbDesc = HCatCreateDBDesc.create(dbName).ifNotExists(true).build(); client.createDatabase(dbDesc); ArrayList<HCatFieldSchema> cols = new ArrayList<HCatFieldSchema>(); cols.add(new HCatFieldSchema("userid", Type.INT, "id columns")); cols.add(new HCatFieldSchema("viewtime", Type.BIGINT, "view time columns")); cols.add(new HCatFieldSchema("pageurl", Type.STRING, "")); cols.add(new HCatFieldSchema("ip", Type.STRING, "IP Address of the User")); ArrayList<HCatFieldSchema> ptnCols = new ArrayList<HCatFieldSchema>(); ptnCols.add(new HCatFieldSchema("dt", Type.STRING, "date column")); ptnCols.add(new HCatFieldSchema("country", Type.STRING, "country column")); HCatCreateTableDesc tableDesc = HCatCreateTableDesc.create(dbName, tableName, cols) .fileFormat("sequencefile") .partCols(ptnCols) .build(); client.createTable(tableDesc); Map<String, String> firstPtn = new HashMap<String, String>(); firstPtn.put("dt", "04/30/2012"); firstPtn.put("country", "usa"); HCatAddPartitionDesc addPtn = HCatAddPartitionDesc.create(dbName, tableName, null, firstPtn).build(); client.addPartition(addPtn); Map<String, String> secondPtn = new HashMap<String, String>(); secondPtn.put("dt", "04/12/2012"); secondPtn.put("country", "brazil"); HCatAddPartitionDesc addPtn2 = HCatAddPartitionDesc.create(dbName, tableName, null, secondPtn).build(); client.addPartition(addPtn2); Map<String, String> thirdPtn = new HashMap<String, String>(); thirdPtn.put("dt", "04/13/2012"); thirdPtn.put("country", "argentina"); HCatAddPartitionDesc addPtn3 = HCatAddPartitionDesc.create(dbName, tableName, null, thirdPtn).build(); client.addPartition(addPtn3); List<HCatPartition> ptnList = client.listPartitionsByFilter(dbName, tableName, null); assertTrue(ptnList.size() == 3); HCatPartition ptn = client.getPartition(dbName, tableName, firstPtn); assertTrue(ptn != null); client.dropPartitions(dbName, tableName, firstPtn, true); ptnList = client.listPartitionsByFilter(dbName, tableName, null); assertTrue(ptnList.size() == 2); List<HCatPartition> ptnListTwo = client.listPartitionsByFilter(dbName, tableName, "country = \"argentina\""); assertTrue(ptnListTwo.size() == 1); client.markPartitionForEvent(dbName, tableName, thirdPtn, PartitionEventType.LOAD_DONE); boolean isMarked = client.isPartitionMarkedForEvent(dbName, tableName, thirdPtn, PartitionEventType.LOAD_DONE); assertTrue(isMarked); client.close(); }