Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
CreateDataSrcStmt.java
Go to the documentation of this file.
1 // Copyright 2014 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 org.apache.hadoop.fs.permission.FsAction;
18 import org.apache.hadoop.hive.metastore.MetaStoreUtils;
19 
23 import com.cloudera.impala.thrift.TCreateDataSourceParams;
24 import com.cloudera.impala.thrift.TDataSource;
25 import com.google.common.base.Joiner;
26 import com.google.common.base.Preconditions;
27 
31 public class CreateDataSrcStmt extends StatementBase {
32  private final String dataSrcName_;
33  private final String className_;
34  private final String apiVersionString_;
35  private final HdfsUri location_;
36  private final boolean ifNotExists_;
38 
39  public CreateDataSrcStmt(String dataSrcName, HdfsUri location, String className,
40  String apiVersionString, boolean ifNotExists) {
41  Preconditions.checkNotNull(dataSrcName);
42  Preconditions.checkNotNull(className);
43  Preconditions.checkNotNull(apiVersionString);
44  Preconditions.checkNotNull(location);
45  dataSrcName_ = dataSrcName.toLowerCase();
46  location_ = location;
47  className_ = className;
48  apiVersionString_ = apiVersionString;
49  ifNotExists_ = ifNotExists;
50  }
51 
52  @Override
53  public void analyze(Analyzer analyzer) throws AnalysisException {
54  if (!MetaStoreUtils.validateName(dataSrcName_)) {
55  throw new AnalysisException("Invalid data source name: " + dataSrcName_);
56  }
57  if (!ifNotExists_ && analyzer.getCatalog().getDataSource(dataSrcName_) != null) {
59  dataSrcName_);
60  }
61 
62  apiVersion_ = ApiVersion.parseApiVersion(apiVersionString_);
63  if (apiVersion_ == null) {
64  throw new AnalysisException("Invalid API version: '" + apiVersionString_ +
65  "'. Valid API versions: " + Joiner.on(", ").join(ApiVersion.values()));
66  }
67 
68  location_.analyze(analyzer, Privilege.ALL, FsAction.READ);
69  // TODO: Check class exists and implements API version
70  // TODO: authorization check
71  }
72 
73  @Override
74  public String toSql() {
75  StringBuilder sb = new StringBuilder();
76  sb.append("CREATE DATA SOURCE ");
77  if (ifNotExists_) sb.append("IF NOT EXISTS ");
78  sb.append(dataSrcName_);
79  sb.append(" LOCATION '");
80  sb.append(location_.getLocation());
81  sb.append("' CLASS '");
82  sb.append(className_);
83  sb.append("' API_VERSION '");
84  sb.append(apiVersion_.name());
85  sb.append("'");
86  return sb.toString();
87  }
88 
89  public TCreateDataSourceParams toThrift() {
90  return new TCreateDataSourceParams(
91  new TDataSource(dataSrcName_, location_.toString(), className_,
92  apiVersion_.name())).setIf_not_exists(ifNotExists_);
93  }
94 }
static final String DATA_SRC_ALREADY_EXISTS_ERROR_MSG
Definition: Analyzer.java:115
CreateDataSrcStmt(String dataSrcName, HdfsUri location, String className, String apiVersionString, boolean ifNotExists)