LogUtils.initHiveLog4j();
} catch (LogInitializationException e) {
}
CliSessionState ss = new CliSessionState(new HiveConf(SessionState.class));
ss.in = System.in;
try {
ss.out = new PrintStream(System.out, true, "UTF-8");
ss.err = new PrintStream(System.err, true, "UTF-8");
} catch (UnsupportedEncodingException e) {
System.exit(1);
}
HiveConf conf = ss.getConf();
HiveConf.setVar(conf, ConfVars.SEMANTIC_ANALYZER_HOOK, HCatSemanticAnalyzer.class.getName());
SessionState.start(ss);
Options options = new Options();
// -e 'quoted-query-string'
options.addOption(OptionBuilder
.hasArg()
.withArgName("exec")
.withDescription("hcat command given from command line")
.create('e'));
// -f <query-file>
options.addOption(OptionBuilder
.hasArg()
.withArgName("file")
.withDescription("hcat commands in file")
.create('f'));
// -g
options.addOption(OptionBuilder
.hasArg().
withArgName("group").
withDescription("group for the db/table specified in CREATE statement").
create('g'));
// -p
options.addOption(OptionBuilder
.hasArg()
.withArgName("perms")
.withDescription("permissions for the db/table specified in CREATE statement")
.create('p'));
// -D
options.addOption(OptionBuilder
.hasArgs(2)
.withArgName("property=value")
.withValueSeparator()
.withDescription("use hadoop value for given property")
.create('D'));
// [-h|--help]
options.addOption(new Option("h", "help", false, "Print help information"));
Parser parser = new GnuParser();
CommandLine cmdLine = null;
try {
cmdLine = parser.parse(options, args);
} catch (ParseException e) {
printUsage(options, ss.err);
System.exit(1);
}
// -e
String execString = (String) cmdLine.getOptionValue('e');
// -f
String fileName = (String) cmdLine.getOptionValue('f');
// -h
if (cmdLine.hasOption('h')) {
printUsage(options, ss.out);
System.exit(0);
}
if (execString != null && fileName != null) {
ss.err.println("The '-e' and '-f' options cannot be specified simultaneously");
printUsage(options, ss.err);
System.exit(1);
}
// -p
String perms = (String) cmdLine.getOptionValue('p');
if (perms != null) {
validatePermissions(ss, conf, perms);
}
// -g
String grp = (String) cmdLine.getOptionValue('g');
if (grp != null) {
conf.set(HCatConstants.HCAT_GROUP, grp);
}
// -D
setConfProperties(conf, cmdLine.getOptionProperties("D"));