hive.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #!/usr/bin/env python
  2. """
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. """
  17. from resource_management import *
  18. import sys
  19. import os
  20. def hive(name=None):
  21. import params
  22. if name == 'hiveserver2':
  23. params.HdfsDirectory(params.hive_apps_whs_dir,
  24. action="create_delayed",
  25. owner=params.hive_user,
  26. mode=0777
  27. )
  28. params.HdfsDirectory(params.hive_hdfs_user_dir,
  29. action="create_delayed",
  30. owner=params.hive_user,
  31. mode=params.hive_hdfs_user_mode
  32. )
  33. params.HdfsDirectory(None, action="create")
  34. Directory(params.hive_conf_dir_prefix,
  35. mode=0755
  36. )
  37. # We should change configurations for client as well as for server.
  38. # The reason is that stale-configs are service-level, not component.
  39. for conf_dir in params.hive_conf_dirs_list:
  40. fill_conf_dir(conf_dir)
  41. XmlConfig("hive-site.xml",
  42. conf_dir=params.hive_config_dir,
  43. configurations=params.config['configurations']['hive-site'],
  44. configuration_attributes=params.config['configuration_attributes']['hive-site'],
  45. owner=params.hive_user,
  46. group=params.user_group,
  47. mode=0644)
  48. if params.hive_specific_configs_supported and name == 'hiveserver2':
  49. XmlConfig("hiveserver2-site.xml",
  50. conf_dir=params.hive_server_conf_dir,
  51. configurations=params.config['configurations']['hiveserver2-site'],
  52. configuration_attributes=params.config['configuration_attributes']['hiveserver2-site'],
  53. owner=params.hive_user,
  54. group=params.user_group,
  55. mode=0644)
  56. File(format("{hive_config_dir}/hive-env.sh"),
  57. owner=params.hive_user,
  58. group=params.user_group,
  59. content=InlineTemplate(params.hive_env_sh_template)
  60. )
  61. if name == 'metastore' or name == 'hiveserver2':
  62. jdbc_connector()
  63. File(format("/usr/lib/ambari-agent/{check_db_connection_jar_name}"),
  64. content = DownloadSource(format("{jdk_location}{check_db_connection_jar_name}")),
  65. )
  66. if name == 'metastore':
  67. File(params.start_metastore_path,
  68. mode=0755,
  69. content=StaticFile('startMetastore.sh')
  70. )
  71. if params.init_metastore_schema:
  72. create_schema_cmd = format("export HIVE_CONF_DIR={hive_server_conf_dir} ; "
  73. "{hive_bin}/schematool -initSchema "
  74. "-dbType {hive_metastore_db_type} "
  75. "-userName {hive_metastore_user_name} "
  76. "-passWord {hive_metastore_user_passwd!p}")
  77. check_schema_created_cmd = as_user(format("export HIVE_CONF_DIR={hive_server_conf_dir} ; "
  78. "{hive_bin}/schematool -info "
  79. "-dbType {hive_metastore_db_type} "
  80. "-userName {hive_metastore_user_name} "
  81. "-passWord {hive_metastore_user_passwd!p}"), params.hive_user)
  82. Execute(create_schema_cmd,
  83. not_if = check_schema_created_cmd,
  84. user = params.hive_user
  85. )
  86. elif name == 'hiveserver2':
  87. File(params.start_hiveserver2_path,
  88. mode=0755,
  89. content=Template(format('{start_hiveserver2_script}'))
  90. )
  91. if name != "client":
  92. crt_directory(params.hive_pid_dir)
  93. crt_directory(params.hive_log_dir)
  94. crt_directory(params.hive_var_lib)
  95. def fill_conf_dir(component_conf_dir):
  96. import params
  97. Directory(component_conf_dir,
  98. owner=params.hive_user,
  99. group=params.user_group,
  100. recursive=True
  101. )
  102. XmlConfig("mapred-site.xml",
  103. conf_dir=component_conf_dir,
  104. configurations=params.config['configurations']['mapred-site'],
  105. configuration_attributes=params.config['configuration_attributes']['mapred-site'],
  106. owner=params.hive_user,
  107. group=params.user_group,
  108. mode=0644)
  109. crt_file(format("{component_conf_dir}/hive-default.xml.template"))
  110. crt_file(format("{component_conf_dir}/hive-env.sh.template"))
  111. log4j_exec_filename = 'hive-exec-log4j.properties'
  112. if (params.log4j_exec_props != None):
  113. File(format("{component_conf_dir}/{log4j_exec_filename}"),
  114. mode=0644,
  115. group=params.user_group,
  116. owner=params.hive_user,
  117. content=params.log4j_exec_props
  118. )
  119. elif (os.path.exists("{component_conf_dir}/{log4j_exec_filename}.template")):
  120. File(format("{component_conf_dir}/{log4j_exec_filename}"),
  121. mode=0644,
  122. group=params.user_group,
  123. owner=params.hive_user,
  124. content=StaticFile(format("{component_conf_dir}/{log4j_exec_filename}.template"))
  125. )
  126. log4j_filename = 'hive-log4j.properties'
  127. if (params.log4j_props != None):
  128. File(format("{component_conf_dir}/{log4j_filename}"),
  129. mode=0644,
  130. group=params.user_group,
  131. owner=params.hive_user,
  132. content=params.log4j_props
  133. )
  134. elif (os.path.exists("{component_conf_dir}/{log4j_filename}.template")):
  135. File(format("{component_conf_dir}/{log4j_filename}"),
  136. mode=0644,
  137. group=params.user_group,
  138. owner=params.hive_user,
  139. content=StaticFile(format("{component_conf_dir}/{log4j_filename}.template"))
  140. )
  141. def crt_directory(name):
  142. import params
  143. Directory(name,
  144. recursive=True,
  145. cd_access='a',
  146. owner=params.hive_user,
  147. group=params.user_group,
  148. mode=0755)
  149. def crt_file(name):
  150. import params
  151. File(name,
  152. owner=params.hive_user,
  153. group=params.user_group
  154. )
  155. def jdbc_connector():
  156. import params
  157. if params.hive_jdbc_driver in params.hive_jdbc_drivers_list and params.hive_use_existing_db:
  158. environment = {
  159. "no_proxy": format("{ambari_server_hostname}")
  160. }
  161. # TODO: should be removed after ranger_hive_plugin will not provide jdbc
  162. Execute(('rm', '-f', params.prepackaged_ojdbc_symlink),
  163. path=["/bin", "/usr/bin/"],
  164. sudo = True)
  165. File(params.downloaded_custom_connector,
  166. content = DownloadSource(params.driver_curl_source),
  167. )
  168. Execute(('cp', '--remove-destination', params.downloaded_custom_connector, params.target),
  169. #creates=params.target, TODO: uncomment after ranger_hive_plugin will not provide jdbc
  170. path=["/bin", "/usr/bin/"],
  171. sudo = True)
  172. else:
  173. #for default hive db (Mysql)
  174. Execute(('cp', '--remove-destination', format('/usr/share/java/{jdbc_jar_name}'), params.target),
  175. #creates=params.target, TODO: uncomment after ranger_hive_plugin will not provide jdbc
  176. path=["/bin", "/usr/bin/"],
  177. sudo=True
  178. )
  179. File(params.target,
  180. mode = 0644,
  181. )