/tags/release-0.0.0-rc0/src/java/org/apache/hcatalog/cli/HCatDriver.java

# · Java · 129 lines · 91 code · 15 blank · 23 comment · 21 complexity · 1fbcf18b6842dbeb8cf4d803477f7bc4 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hcatalog.cli;
  19. import org.apache.hadoop.conf.Configuration;
  20. import org.apache.hadoop.fs.FileSystem;
  21. import org.apache.hadoop.fs.Path;
  22. import org.apache.hadoop.fs.permission.FsPermission;
  23. import org.apache.hadoop.hive.metastore.MetaStoreUtils;
  24. import org.apache.hadoop.hive.metastore.Warehouse;
  25. import org.apache.hadoop.hive.ql.Driver;
  26. import org.apache.hadoop.hive.ql.metadata.Hive;
  27. import org.apache.hadoop.hive.ql.metadata.HiveException;
  28. import org.apache.hadoop.hive.ql.metadata.Table;
  29. import org.apache.hadoop.hive.ql.processors.CommandProcessorResponse;
  30. import org.apache.hadoop.hive.ql.session.SessionState;
  31. import org.apache.hcatalog.common.HCatConstants;
  32. public class HCatDriver extends Driver {
  33. @Override
  34. public CommandProcessorResponse run(String command) {
  35. int ret = super.run(command).getResponseCode();
  36. SessionState ss = SessionState.get();
  37. if (ret == 0){
  38. // Only attempt to do this, if cmd was successful.
  39. ret = setFSPermsNGrp(ss);
  40. }
  41. // reset conf vars
  42. ss.getConf().set(HCatConstants.HCAT_CREATE_DB_NAME, "");
  43. ss.getConf().set(HCatConstants.HCAT_CREATE_TBL_NAME, "");
  44. return new CommandProcessorResponse(ret);
  45. }
  46. private int setFSPermsNGrp(SessionState ss) {
  47. Configuration conf =ss.getConf();
  48. String tblName = conf.get(HCatConstants.HCAT_CREATE_TBL_NAME,"");
  49. String dbName = conf.get(HCatConstants.HCAT_CREATE_DB_NAME, "");
  50. String grp = conf.get(HCatConstants.HCAT_GROUP,null);
  51. String permsStr = conf.get(HCatConstants.HCAT_PERMS,null);
  52. if(tblName.isEmpty() && dbName.isEmpty()){
  53. // it wasn't create db/table
  54. return 0;
  55. }
  56. if(null == grp && null == permsStr) {
  57. // there were no grp and perms to begin with.
  58. return 0;
  59. }
  60. FsPermission perms = FsPermission.valueOf(permsStr);
  61. if(!tblName.isEmpty()){
  62. Hive db = null;
  63. try{
  64. db = Hive.get();
  65. Table tbl = db.getTable(tblName);
  66. Path tblPath = tbl.getPath();
  67. FileSystem fs = tblPath.getFileSystem(conf);
  68. if(null != perms){
  69. fs.setPermission(tblPath, perms);
  70. }
  71. if(null != grp){
  72. fs.setOwner(tblPath, null, grp);
  73. }
  74. return 0;
  75. } catch (Exception e){
  76. ss.err.println(String.format("Failed to set permissions/groups on TABLE: <%s> %s",tblName,e.getMessage()));
  77. try { // We need to drop the table.
  78. if(null != db){ db.dropTable(tblName); }
  79. } catch (HiveException he) {
  80. ss.err.println(String.format("Failed to drop TABLE <%s> after failing to set permissions/groups on it. %s",tblName,e.getMessage()));
  81. }
  82. return 1;
  83. }
  84. }
  85. else{
  86. // looks like a db operation
  87. if (dbName.isEmpty() || dbName.equals(MetaStoreUtils.DEFAULT_DATABASE_NAME)){
  88. // We dont set perms or groups for default dir.
  89. return 0;
  90. }
  91. else{
  92. try{
  93. Path dbPath = new Warehouse(conf).getDefaultDatabasePath(dbName);
  94. FileSystem fs = dbPath.getFileSystem(conf);
  95. if(perms != null){
  96. fs.setPermission(dbPath, perms);
  97. }
  98. if(null != grp){
  99. fs.setOwner(dbPath, null, grp);
  100. }
  101. return 0;
  102. } catch (Exception e){
  103. ss.err.println(String.format("Failed to set permissions and/or group on DB: <%s> %s", dbName, e.getMessage()));
  104. try {
  105. Hive.get().dropDatabase(dbName);
  106. } catch (Exception e1) {
  107. ss.err.println(String.format("Failed to drop DB <%s> after failing to set permissions/group on it. %s", dbName, e1.getMessage()));
  108. }
  109. return 1;
  110. }
  111. }
  112. }
  113. }
  114. }