Index: webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java =================================================================== --- webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java (revision 1417555) +++ webhcat/java-client/src/test/java/org/apache/hcatalog/api/TestHCatClient.java (working copy) @@ -19,6 +19,7 @@ import java.math.BigInteger; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -45,6 +46,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; public class TestHCatClient { @@ -370,4 +373,77 @@ assertTrue("The expected exception was never thrown.", isExceptionCaught); } } + + @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.DROP_DB_MODE.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 columns = Arrays.asList(new HCatFieldSchema("col", Type.STRING, "")); + ArrayList partitionColumns = new ArrayList( + Arrays.asList(new HCatFieldSchema(partitionColumn, Type.STRING, ""))); + client.createTable(HCatCreateTableDesc.create(dbName, tableName, columns) + .partCols(partitionColumns) + .build()); + + Map partitionSpec = new HashMap(); + 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); + } + } } Index: webhcat/java-client/src/main/java/org/apache/hcatalog/api/ObjectNotFoundException.java =================================================================== --- webhcat/java-client/src/main/java/org/apache/hcatalog/api/ObjectNotFoundException.java (revision 0) +++ webhcat/java-client/src/main/java/org/apache/hcatalog/api/ObjectNotFoundException.java (revision 0) @@ -0,0 +1,38 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hcatalog.api; + +import org.apache.hcatalog.common.HCatException; + +/** + * This exception is thrown when a Database, Table or Partition + * specified in an HCatalog query is not found. + */ +public class ObjectNotFoundException extends HCatException { + + private static final long serialVersionUID = 1L; + + /** + * @param message Exception message. + * @param cause The wrapped Throwable that caused this exception. + */ + public ObjectNotFoundException(String message, Throwable cause) { + super(message, cause); + } +} Index: webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java =================================================================== --- webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java (revision 1417555) +++ webhcat/java-client/src/main/java/org/apache/hcatalog/api/ConnectionFailureException.java (working copy) @@ -28,8 +28,8 @@ private static final long serialVersionUID = 1L; /** - * @param message - * @param cause + * @param message Exception message. + * @param cause The wrapped Throwable that caused this exception. */ public ConnectionFailureException(String message, Throwable cause) { super(message, cause); Index: webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java =================================================================== --- webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java (revision 1417555) +++ webhcat/java-client/src/main/java/org/apache/hcatalog/api/HCatClientHMSImpl.java (working copy) @@ -78,7 +78,7 @@ db = new HCatDatabase(hiveDB); } } catch (NoSuchObjectException exp) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while fetching database", exp); } catch (MetaException exp) { throw new HCatException("MetaException while fetching database", @@ -125,7 +125,7 @@ hmsClient.dropDatabase(checkDB(dbName), true, ifExists, isCascade); } catch (NoSuchObjectException e) { if (!ifExists) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while dropping db.", e); } } catch (InvalidOperationException e) { @@ -167,7 +167,7 @@ throw new ConnectionFailureException( "TException while fetching table.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while fetching table.", e); } return table; @@ -189,7 +189,7 @@ } catch (MetaException e) { throw new HCatException("MetaException while creating table.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while creating table.", e); } catch (TException e) { throw new ConnectionFailureException( @@ -224,7 +224,7 @@ throw new HCatException( "MetaException in create table like command.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException in create table like command.", e); } catch (TException e) { @@ -241,7 +241,7 @@ hmsClient.dropTable(checkDB(dbName), tableName, true, ifExists); } catch (NoSuchObjectException e) { if (!ifExists) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while dropping table.", e); } } catch (MetaException e) { @@ -276,7 +276,7 @@ throw new ConnectionFailureException( "TException while renaming table", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while renaming table", e); } catch (InvalidOperationException e) { throw new HCatException( @@ -295,7 +295,7 @@ hcatPtns.add(new HCatPartition(ptn)); } } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while retrieving partition.", e); } catch (MetaException e) { throw new HCatException( @@ -327,7 +327,7 @@ throw new ConnectionFailureException( "TException while retrieving partition.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while retrieving partition.", e); } return partition; @@ -359,7 +359,7 @@ throw new ConnectionFailureException( "TException while adding partition.", e); } catch (NoSuchObjectException e) { - throw new HCatException("The table " + partInfo.getTableName() + throw new ObjectNotFoundException("The table " + partInfo.getTableName() + " is could not be found.", e); } } @@ -375,7 +375,7 @@ ifExists); } catch (NoSuchObjectException e) { if (!ifExists) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while dropping partition.", e); } } catch (MetaException e) { @@ -402,7 +402,7 @@ throw new HCatException("MetaException while fetching partitions.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while fetching partitions.", e); } catch (TException e) { throw new ConnectionFailureException( @@ -422,7 +422,7 @@ throw new HCatException( "MetaException while marking partition for event.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while marking partition for event.", e); } catch (UnknownTableException e) { @@ -458,7 +458,7 @@ throw new HCatException( "MetaException while checking partition for event.", e); } catch (NoSuchObjectException e) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while checking partition for event.", e); } catch (UnknownTableException e) { @@ -571,7 +571,7 @@ throw new ConnectionFailureException( "TException while retrieving existing table.", e1); } catch (NoSuchObjectException e1) { - throw new HCatException( + throw new ObjectNotFoundException( "NoSuchObjectException while retrieving existing table.", e1); } @@ -653,7 +653,7 @@ throw new ConnectionFailureException( "TException while adding partition.", e); } catch (NoSuchObjectException e) { - throw new HCatException("The table " + throw new ObjectNotFoundException("The table " + partInfoList.get(0).getTableName() + " is could not be found.", e); }