Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
HdfsUri.java
Go to the documentation of this file.
1 // Copyright 2013 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.analysis;
16 
17 import java.io.IOException;
18 
19 import org.apache.hadoop.fs.FileSystem;
20 import org.apache.hadoop.fs.Path;
21 import org.apache.hadoop.fs.permission.FsAction;
22 
29 import com.google.common.base.Preconditions;
30 
31 /*
32  * Represents a Hadoop FileSystem URI in a SQL statement.
33  */
34 public class HdfsUri {
35  private final String location_;
36 
37  // Set during analysis
38  private Path uriPath_;
39 
40  public HdfsUri(String location) {
41  Preconditions.checkNotNull(location);
42  this.location_ = location.trim();
43  }
44 
45  public Path getPath() {
46  Preconditions.checkNotNull(uriPath_);
47  return uriPath_;
48  }
49 
50  public void analyze(Analyzer analyzer, Privilege privilege)
51  throws AnalysisException {
52  analyze(analyzer, privilege, FsAction.NONE, true);
53  }
54 
55  public void analyze(Analyzer analyzer, Privilege privilege, FsAction perm)
56  throws AnalysisException {
57  analyze(analyzer, privilege, perm, true);
58  }
59 
60  public void analyze(Analyzer analyzer, Privilege privilege, boolean registerPrivReq)
61  throws AnalysisException {
62  analyze(analyzer, privilege, FsAction.NONE, registerPrivReq);
63  }
64 
71  public void analyze(Analyzer analyzer, Privilege privilege, FsAction perm,
72  boolean registerPrivReq) throws AnalysisException {
73  if (location_.isEmpty()) {
74  throw new AnalysisException("URI path cannot be empty.");
75  }
76 
77  uriPath_ = new Path(location_);
78  if (!uriPath_.isUriPathAbsolute()) {
79  throw new AnalysisException("URI path must be absolute: " + uriPath_);
80  }
81 
82  uriPath_ = FileSystemUtil.createFullyQualifiedPath(uriPath_);
83 
84  // Check if parent path exists and if impala is allowed to access it.
85  Path parentPath = uriPath_.getParent();
86  try {
87  FileSystem fs = uriPath_.getFileSystem(FileSystemUtil.getConfiguration());
88  StringBuilder errorMsg = new StringBuilder();
89  if (!FileSystemUtil.isPathReachable(parentPath, fs, errorMsg)) {
90  analyzer.addWarning(String.format("Path '%s' cannot be reached: %s",
91  parentPath, errorMsg.toString()));
92  } else if (perm != FsAction.NONE) {
93  FsPermissionChecker checker = FsPermissionChecker.getInstance();
94  if (!checker.getPermissions(fs, parentPath).checkPermissions(perm)) {
95  analyzer.addWarning(String.format(
96  "Impala does not have %s access to path '%s'",
97  perm.toString(), parentPath));
98  }
99  }
100  } catch (IOException e) {
101  throw new AnalysisException(e.getMessage(), e);
102  }
103 
104  if (registerPrivReq) {
105  analyzer.registerPrivReq(new PrivilegeRequest(
106  new AuthorizeableUri(uriPath_.toString()), privilege));
107  }
108  }
109 
110  @Override
111  public String toString() {
112  // If uriPath is null (this HdfsURI has not been analyzed yet) just return the raw
113  // location string the caller passed in.
114  return uriPath_ == null ? location_ : uriPath_.toString();
115  }
116 
117  public String getLocation() { return location_; }
118 }
void analyze(Analyzer analyzer, Privilege privilege, FsAction perm, boolean registerPrivReq)
Definition: HdfsUri.java:71
void analyze(Analyzer analyzer, Privilege privilege)
Definition: HdfsUri.java:50
Permissions getPermissions(FileSystem fs, Path path)
void analyze(Analyzer analyzer, Privilege privilege, FsAction perm)
Definition: HdfsUri.java:55
void analyze(Analyzer analyzer, Privilege privilege, boolean registerPrivReq)
Definition: HdfsUri.java:60
static Boolean isPathReachable(Path path, FileSystem fs, StringBuilder error_msg)