diff --git a/metastore/src/java/org/apache/hadoop/hive/metastore/Warehouse.java b/metastore/src/java/org/apache/hadoop/hive/metastore/Warehouse.java index 25119abf97382df7c0615edbaff29ba20624a137..62b018407924804358528b7719e3d66284525df7 100755 --- a/metastore/src/java/org/apache/hadoop/hive/metastore/Warehouse.java +++ b/metastore/src/java/org/apache/hadoop/hive/metastore/Warehouse.java @@ -124,7 +124,7 @@ public static void closeFs(FileSystem fs) throws MetaException { /** - * Hadoop File System reverse lookups paths with raw ip addresses The File + * Hadoop File System reverse lookups paths with raw ip addresses. The File * System URI always contains the canonical DNS name of the Namenode. * Subsequently, operations on paths with raw ip addresses cause an exception * since they don't match the file system URI. @@ -132,12 +132,19 @@ public static void closeFs(FileSystem fs) throws MetaException { * This routine solves this problem by replacing the scheme and authority of a * path with the scheme and authority of the FileSystem that it maps to. * + * Note: getDnsPath only works with absolute (not relative) paths. + * * @param path * Path to be canonicalized * @return Path with canonical scheme and authority */ public static Path getDnsPath(Path path, Configuration conf) throws MetaException { FileSystem fs = getFs(path, conf); + if (!path.isAbsolute()) { + String message = String.format("'%s' is not an absolute path.", path.toString()); + throw new MetaException(message); + } + return (new Path(fs.getUri().getScheme(), fs.getUri().getAuthority(), path .toUri().getPath())); } diff --git a/metastore/src/test/org/apache/hadoop/hive/metastore/TestWarehouse.java b/metastore/src/test/org/apache/hadoop/hive/metastore/TestWarehouse.java new file mode 100644 index 0000000000000000000000000000000000000000..8ebcdfa03b85a826dc2a6a4599174ae7d6e91b35 --- /dev/null +++ b/metastore/src/test/org/apache/hadoop/hive/metastore/TestWarehouse.java @@ -0,0 +1,40 @@ +/** + * 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.hadoop.hive.metastore; + +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.metastore.api.MetaException; +import org.junit.Test; + +public class TestWarehouse { + @Test + public void testValidAbsoluteDNSPath() throws MetaException { + HiveConf configuration = new HiveConf(); + Path absolutePath = new Path("/absolute/path"); + Warehouse.getDnsPath(absolutePath, configuration); + } + + @Test(expected=MetaException.class) + public void testInvalidRelativeDNSPath() throws MetaException { + HiveConf configuration = new HiveConf(); + Path relativePath = new Path("relative/path"); + Warehouse.getDnsPath(relativePath, configuration); + } +}