hive.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. environment = {
  64. "no_proxy": format("{ambari_server_hostname}")
  65. }
  66. cmd = format("/bin/sh -c 'cd /usr/lib/ambari-agent/ && curl -kf -x \"\" "
  67. "--retry 5 "
  68. "{jdk_location}{check_db_connection_jar_name} "
  69. "-o {check_db_connection_jar_name}'")
  70. Execute(cmd,
  71. not_if=format("[ -f {check_db_connection_jar} ]"),
  72. environment = environment)
  73. if name == 'metastore':
  74. File(params.start_metastore_path,
  75. mode=0755,
  76. content=StaticFile('startMetastore.sh')
  77. )
  78. if params.init_metastore_schema:
  79. create_schema_cmd = format("export HIVE_CONF_DIR={hive_server_conf_dir} ; "
  80. "{hive_bin}/schematool -initSchema "
  81. "-dbType {hive_metastore_db_type} "
  82. "-userName {hive_metastore_user_name} "
  83. "-passWord {hive_metastore_user_passwd!p}")
  84. check_schema_created_cmd = format("export HIVE_CONF_DIR={hive_server_conf_dir} ; "
  85. "{hive_bin}/schematool -info "
  86. "-dbType {hive_metastore_db_type} "
  87. "-userName {hive_metastore_user_name} "
  88. "-passWord {hive_metastore_user_passwd!p}")
  89. Execute(create_schema_cmd,
  90. not_if = check_schema_created_cmd
  91. )
  92. elif name == 'hiveserver2':
  93. File(params.start_hiveserver2_path,
  94. mode=0755,
  95. content=Template(format('{start_hiveserver2_script}'))
  96. )
  97. if name != "client":
  98. crt_directory(params.hive_pid_dir)
  99. crt_directory(params.hive_log_dir)
  100. crt_directory(params.hive_var_lib)
  101. def fill_conf_dir(component_conf_dir):
  102. import params
  103. Directory(component_conf_dir,
  104. owner=params.hive_user,
  105. group=params.user_group,
  106. recursive=True
  107. )
  108. XmlConfig("mapred-site.xml",
  109. conf_dir=component_conf_dir,
  110. configurations=params.config['configurations']['mapred-site'],
  111. configuration_attributes=params.config['configuration_attributes']['mapred-site'],
  112. owner=params.hive_user,
  113. group=params.user_group,
  114. mode=0644)
  115. crt_file(format("{component_conf_dir}/hive-default.xml.template"))
  116. crt_file(format("{component_conf_dir}/hive-env.sh.template"))
  117. log4j_exec_filename = 'hive-exec-log4j.properties'
  118. if (params.log4j_exec_props != None):
  119. File(format("{component_conf_dir}/{log4j_exec_filename}"),
  120. mode=0644,
  121. group=params.user_group,
  122. owner=params.hive_user,
  123. content=params.log4j_exec_props
  124. )
  125. elif (os.path.exists("{component_conf_dir}/{log4j_exec_filename}.template")):
  126. File(format("{component_conf_dir}/{log4j_exec_filename}"),
  127. mode=0644,
  128. group=params.user_group,
  129. owner=params.hive_user,
  130. content=StaticFile(format("{component_conf_dir}/{log4j_exec_filename}.template"))
  131. )
  132. log4j_filename = 'hive-log4j.properties'
  133. if (params.log4j_props != None):
  134. File(format("{component_conf_dir}/{log4j_filename}"),
  135. mode=0644,
  136. group=params.user_group,
  137. owner=params.hive_user,
  138. content=params.log4j_props
  139. )
  140. elif (os.path.exists("{component_conf_dir}/{log4j_filename}.template")):
  141. File(format("{component_conf_dir}/{log4j_filename}"),
  142. mode=0644,
  143. group=params.user_group,
  144. owner=params.hive_user,
  145. content=StaticFile(format("{component_conf_dir}/{log4j_filename}.template"))
  146. )
  147. def crt_directory(name):
  148. import params
  149. Directory(name,
  150. recursive=True,
  151. owner=params.hive_user,
  152. group=params.user_group,
  153. mode=0755)
  154. def crt_file(name):
  155. import params
  156. File(name,
  157. owner=params.hive_user,
  158. group=params.user_group
  159. )
  160. def jdbc_connector():
  161. import params
  162. if params.hive_jdbc_driver in params.hive_jdbc_drivers_list:
  163. environment = {
  164. "no_proxy": format("{ambari_server_hostname}")
  165. }
  166. Execute(('curl', '-kf', '-x', "", '--retry', '10', params.driver_curl_source, '-o', params.driver_curl_target),
  167. not_if=format("test -f {target}"),
  168. path=["/bin", "/usr/bin/"],
  169. environment=environment,
  170. sudo = True)
  171. Execute(('cp', params.driver_curl_target, params.target),
  172. not_if=format("test -f {target}"),
  173. creates=params.target,
  174. path=["/bin", "/usr/bin/"],
  175. sudo = True)