org.apache.hadoop.hive.ql.processors.SetProcessor Maven / Gradle / Ivy
The newest version!
/**
* 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.ql.processors;
import static org.apache.hadoop.hive.serde.serdeConstants.SERIALIZATION_NULL_FORMAT;
import static org.apache.hadoop.hive.serde.serdeConstants.STRING_TYPE_NAME;
import static org.apache.hadoop.hive.serde2.MetadataTypedColumnsetSerDe.defaultNullString;
import static org.apache.hadoop.hive.conf.SystemVariables.*;
import java.util.Map;
import java.util.Properties;
import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.hadoop.hive.conf.HiveConf;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.Schema;
import org.apache.hadoop.hive.ql.metadata.Hive;
import org.apache.hadoop.hive.ql.parse.VariableSubstitution;
import org.apache.hadoop.hive.ql.session.SessionState;
/**
* SetProcessor.
*
*/
public class SetProcessor implements CommandProcessor {
private static final String prefix = "set: ";
public static boolean getBoolean(String value) {
if (value.equals("on") || value.equals("true")) {
return true;
}
if (value.equals("off") || value.equals("false")) {
return false;
}
throw new IllegalArgumentException(prefix + "'" + value
+ "' is not a boolean");
}
private void dumpOptions(Properties p) {
SessionState ss = SessionState.get();
SortedMap sortedMap = new TreeMap();
sortedMap.put("silent", (ss.getIsSilent() ? "on" : "off"));
for (Object one : p.keySet()) {
String oneProp = (String) one;
String oneValue = p.getProperty(oneProp);
sortedMap.put(oneProp, oneValue);
}
// Inserting hive variables
for (String s : ss.getHiveVariables().keySet()) {
sortedMap.put(HIVEVAR_PREFIX + s, ss.getHiveVariables().get(s));
}
for (Map.Entry entries : sortedMap.entrySet()) {
ss.out.println(entries.getKey() + "=" + entries.getValue());
}
for (Map.Entry entry : mapToSortedMap(System.getenv()).entrySet()) {
ss.out.println(ENV_PREFIX+entry.getKey() + "=" + entry.getValue());
}
for (Map.Entry entry :
propertiesToSortedMap(System.getProperties()).entrySet() ) {
ss.out.println(SYSTEM_PREFIX+entry.getKey() + "=" + entry.getValue());
}
}
private void dumpOption(String s) {
SessionState ss = SessionState.get();
if (ss.getConf().get(s) != null) {
ss.out.println(s + "=" + ss.getConf().get(s));
} else if (ss.getHiveVariables().containsKey(s)) {
ss.out.println(s + "=" + ss.getHiveVariables().get(s));
} else {
ss.out.println(s + " is undefined");
}
}
@Override
public void init() {
}
public CommandProcessorResponse executeSetVariable(String varname, String varvalue) {
try {
return new CommandProcessorResponse(setVariable(varname, varvalue));
} catch (Exception e) {
return new CommandProcessorResponse(1, e.getMessage(), "42000",
e instanceof IllegalArgumentException ? null : e);
}
}
public static int setVariable(String varname, String varvalue) throws Exception {
SessionState ss = SessionState.get();
if (varvalue.contains("\n")){
ss.err.println("Warning: Value had a \\n character in it.");
}
varname = varname.trim();
if (varname.startsWith(ENV_PREFIX)){
ss.err.println("env:* variables can not be set.");
return 1;
} else if (varname.startsWith(SYSTEM_PREFIX)){
String propName = varname.substring(SYSTEM_PREFIX.length());
System.getProperties().setProperty(propName, new VariableSubstitution().substitute(ss.getConf(),varvalue));
} else if (varname.startsWith(HIVECONF_PREFIX)){
String propName = varname.substring(HIVECONF_PREFIX.length());
setConf(varname, propName, varvalue, false);
} else if (varname.startsWith(HIVEVAR_PREFIX)) {
String propName = varname.substring(HIVEVAR_PREFIX.length());
ss.getHiveVariables().put(propName, new VariableSubstitution().substitute(ss.getConf(),varvalue));
} else if (varname.startsWith(METACONF_PREFIX)) {
String propName = varname.substring(METACONF_PREFIX.length());
Hive hive = Hive.get(ss.getConf());
hive.setMetaConf(propName, new VariableSubstitution().substitute(ss.getConf(), varvalue));
} else {
setConf(varname, varname, varvalue, true);
}
return 0;
}
// returns non-null string for validation fail
private static void setConf(String varname, String key, String varvalue, boolean register)
throws IllegalArgumentException {
HiveConf conf = SessionState.get().getConf();
String value = new VariableSubstitution().substitute(conf, varvalue);
if (conf.getBoolVar(HiveConf.ConfVars.HIVECONFVALIDATION)) {
HiveConf.ConfVars confVars = HiveConf.getConfVars(key);
if (confVars != null) {
if (!confVars.isType(value)) {
StringBuilder message = new StringBuilder();
message.append("'SET ").append(varname).append('=').append(varvalue);
message.append("' FAILED because ").append(key).append(" expects ");
message.append(confVars.typeString()).append(" type value.");
throw new IllegalArgumentException(message.toString());
}
String fail = confVars.validate(value);
if (fail != null) {
StringBuilder message = new StringBuilder();
message.append("'SET ").append(varname).append('=').append(varvalue);
message.append("' FAILED in validation : ").append(fail).append('.');
throw new IllegalArgumentException(message.toString());
}
} else if (key.startsWith("hive.")) {
throw new IllegalArgumentException("hive configuration " + key + " does not exists.");
}
}
conf.verifyAndSet(key, value);
if (register) {
SessionState.get().getOverriddenConfigurations().put(key, value);
}
}
private SortedMap propertiesToSortedMap(Properties p){
SortedMap sortedPropMap = new TreeMap();
for (Map.Entry