尚硅谷大数据技术之Hive.docx
编辑启动脚本vim $HIVE_HOME/bin/hiveservices.sh
添加执行权限chmod +x $HIVE_HOME/bin/hiveservices.sh

  1. #!/bin/bash
  2. HIVE_LOG_DIR=$HIVE_HOME/logs
  3. if [ ! -d $HIVE_LOG_DIR ]
  4. then
  5. mkdir -p $HIVE_LOG_DIR
  6. fi
  7. #检查进程是否运行正常,参数 1 为进程名,参数 2 为进程端口
  8. function check_process()
  9. {
  10. pid=$(ps -ef 2>/dev/null | grep -v grep | grep -i $1 | awk '{print $2}')
  11. ppid=$(netstat -nltp 2>/dev/null | grep $2 | awk '{print $7}' | cut -d '/' -f 1)
  12. echo $pid
  13. [[ "$pid" =~ "$ppid" ]] && [ "$ppid" ] && return 0 || return 1
  14. }
  15. function hive_start()
  16. {
  17. metapid=$(check_process HiveMetastore 9083)
  18. cmd="nohup hive --service metastore >$HIVE_LOG_DIR/metastore.log 2>&1 &"
  19. [ -z "$metapid" ] && eval $cmd || echo "Metastroe 服务已启动"
  20. server2pid=$(check_process HiveServer2 10000)
  21. cmd="nohup hiveserver2 >$HIVE_LOG_DIR/hiveServer2.log 2>&1 &"
  22. [ -z "$server2pid" ] && eval $cmd || echo "HiveServer2 服务已启动"
  23. }
  24. function hive_stop()
  25. {
  26. metapid=$(check_process HiveMetastore 9083)
  27. [ "$metapid" ] && kill $metapid || echo "Metastore 服务未启动"
  28. server2pid=$(check_process HiveServer2 10000)
  29. [ "$server2pid" ] && kill $server2pid || echo "HiveServer2 服务未启动"
  30. }
  31. case $1 in
  32. "start")
  33. hive_start
  34. ;;
  35. "stop")
  36. hive_stop
  37. ;;
  38. "restart")
  39. hive_stop
  40. sleep 2
  41. hive_start
  42. ;;
  43. "status")
  44. check_process HiveMetastore 9083 >/dev/null && echo "Metastore 服务运行正常" || echo "Metastore 服务运行异常"
  45. check_process HiveServer2 10000 >/dev/null && echo "HiveServer2 服务运行正常" || echo "HiveServer2 服务运行异常"
  46. ;;
  47. *)
  48. echo Invalid Args!
  49. echo 'Usage: '$(basename $0)' start|stop|restart|status'
  50. ;;
  51. esac

常用命令

启动beeline客户端:bin/beeline -u jdbc:hive2://hadoop102:10000 -n lhd
若启动失败则执行:rm -rf /tmp/*

bin/hive -e “sql 语句” 不进入客户端执行sql语句
bin/hive -f “hive.sql” 执行hive.sql文件里面的sql语句
dfs -ls /; 查看hdfs文件系统
cat .hivehistory 查看历史执行命令
dfs -ls /; 查看历史执行语句