hbase-cleanup.sh

本文介绍了一个用于HBase的数据清理脚本,该脚本能够从ZooKeeper和HDFS中清除与HBase相关的数据,但仅在没有活跃的HBase进程时执行此操作。清理过程包括检查和删除ZooKeeper上的节点以及HDFS上的目录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/usr/bin/env bash
#  没有进行批注
#/**
# * Licensed to the Apache Software Foundation (ASF) under one
# * or more contributor license agreements.  See the NOTICE file
# * distributed with this work for additional information
# * regarding copyright ownership.  The ASF licenses this file
# * to you under the Apache License, Version 2.0 (the
# * "License"); you may not use this file except in compliance
# * with the License.  You may obtain a copy of the License at
# *
# *     http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */
#
# cleans hbase related data from zookeeper and hdfs if no hbase process is alive.--------------------------------------
#
# Environment Variables
#
#   HBASE_REGIONSERVERS    File naming remote hosts.
#     Default is ${HADOOP_CONF_DIR}/regionservers
#   HADOOP_CONF_DIR  Alternate conf dir. Default is ${HADOOP_HOME}/conf.
#   HBASE_CONF_DIR  Alternate hbase conf dir. Default is ${HBASE_HOME}/conf.
#   HADOOP_SLAVE_SLEEP Seconds to sleep between spawning remote commands.
#   HADOOP_SLAVE_TIMEOUT Seconds to wait for timing out a remote command.
#   HADOOP_SSH_OPTS Options passed to ssh when running remote commands.
#


usage="Usage: hbase-cleanup.sh (zk|hdfs|all)"


bin=`dirname "$0"`
bin=`cd "$bin">/dev/null; pwd`


# This will set HBASE_HOME, etc.
. "$bin"/hbase-config.sh


case $1 in
  --cleanZk|--cleanHdfs|--cleanAll) 
    matches="yes" ;;
  *) ;;
esac
if [ $# -ne 1 -o "$matches" = "" ]; then
  echo $usage
  exit 1;
fi


format_option=$1;


distMode=`$bin/hbase --config ${HBASE_CONF_DIR} org.apache.hadoop.hbase.util.HBaseConfTool hbase.cluster.distributed | head -n 1`


if [ "$distMode" == 'false' ];then
  echo "Skipping hbase data clearing in standalone mode." 2>&1
  exit 1;
fi


zparent=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.parent`
if [ "$zparent" == "null" ]; then zparent="/hbase"; fi


hrootdir=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool hbase.rootdir`
if [ "$hrootdir" == "null" ]; then hrootdir="file:///tmp/hbase-${USER}/hbase"; fi


check_for_znodes() {
  command=$1;
  case $command in
    regionservers)
      zchild=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.rs`
      if [ "$zchild" == "null" ]; then zchild="rs"; fi
      ;;
    backupmasters)
      zchild=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.backup.masters`
      if [ "$zchild" == "null" ]; then zchild="backup-masters"; fi
      ;;
  esac
  znodes=`"$bin"/hbase zkcli ls $zparent/$zchild 2>&1 | tail -1 | sed "s/\[//" | sed "s/\]//"`
  if [ "$znodes" != "" ]; then
    echo -n "ZNode(s) [${znodes}] of $command are not expired. Exiting without cleaning hbase data."
    echo #force a newline
    exit 1;
  else
    echo -n "All ZNode(s) of $command are expired."
  fi
  echo #force a newline
}


execute_zk_command() {
  command=$1;
  "$bin"/hbase zkcli $command 2>&1
}


execute_hdfs_command() {
  command=$1;
  "$bin"/hbase org.apache.hadoop.fs.FsShell $command 2>&1
}


clean_up() {
  case $1 in
  --cleanZk) 
    execute_zk_command "rmr ${zparent}";
    ;;
  --cleanHdfs)
    execute_hdfs_command "-rmr ${hrootdir}"
    ;;
  --cleanAll)
    execute_zk_command "rmr ${zparent}";
    execute_hdfs_command "-rmr ${hrootdir}"
    ;;
  *)
    ;;
  esac
}


check_znode_exists() {
  command=$1
  "$bin"/hbase zkcli stat $command 2>&1 | grep "Node does not exist\|Connection refused"
}


check_znode_exists $zparent
if [ $? -ne 0 ]; then
  # make sure the online region server(s) znode(s) have been deleted before continuing
  check_for_znodes regionservers
  # make sure the backup master(s) znode(s) has been deleted before continuing
  check_for_znodes backupmasters
  # make sure the master znode has been deleted before continuing
  zmaster=`$bin/hbase org.apache.hadoop.hbase.util.HBaseConfTool zookeeper.znode.master`
  if [ "$zmaster" == "null" ]; then zmaster="master"; fi
    zmaster=$zparent/$zmaster
    check_znode_exists $zmaster
  if [ $? -ne 0 ]; then
    echo -n "Master ZNode is not expired. Exiting without cleaning hbase data."
    echo #force a new line
    exit 1
  else
    echo "Active Master ZNode also expired."
  fi
  echo #force a newline
else
  echo "HBase parent znode ${zparent} does not exist."
fi


# cleans zookeeper and/or hdfs data.
clean_up $format_option
<think>好的,我现在需要帮助用户了解如何正确配置HBasehbase-env.sh文件。首先,我应该回忆一下HBase的相关知识,尤其是配置文件的部分。hbase-env.shHBase环境变量设置的重要文件,里面包含了很多关键的配置参数,比如Java路径、堆内存大小、日志目录等。用户可能希望知道具体的配置步骤和常见参数的说明。 首先,我应该检查用户提供的引用内容。用户引用了三个来源,其中引用[1]提到进入hbase的conf目录并编辑hbase-env.sh,引用[2]涉及关闭和启动HBase时的问题,引用[3]是关于Spark的环境配置,可能和类路径有关联。用户的问题集中在hbase-env.sh的配置教程和参数说明,所以需要集中在这个文件上。 接下来,我需要组织回答的结构。通常,教程类的回答需要分步骤说明,比如如何找到文件、常用的配置参数有哪些、每个参数的作用以及配置后的验证步骤。同时,需要结合用户提供的引用内容,尤其是引用[1]中的路径提示,确保用户能找到正确的文件位置。 然后,考虑用户可能的疑问点。例如,用户可能在配置JAVA_HOME时遇到问题,或者堆内存设置不合适导致性能问题,日志目录的配置是否会影响日志记录等。此外,用户可能不清楚哪些参数是必须配置的,哪些是可选的,或者如何根据集群规模调整参数。 另外,需要注意引用来源的正确应用。比如,用户提到在启动HBase时可能需要生成pid文件,如引用[2]所述,所以在配置完成后的启动步骤中需要提醒用户验证进程是否正确启动,确保stop-hbase.sh能找到相应的pid文件。 还需要注意LaTeX格式的要求,所有行内数学表达式需要用$...$,独立公式用$$...$$,但当前问题涉及的是配置文件,可能数学公式较少,但如果有涉及变量或路径表达式,需要正确应用格式。例如,路径如$JAVA_HOME$需要用行内格式。 另外,用户要求生成相关问题,需要根据配置hbase-env.sh的常见疑问来设计,比如参数调优、常见错误解决、环境变量设置等。 最后,确保回答符合中文要求,结构清晰,分步骤说明,引用正确来源,并自然融入引用标识。同时,检查LaTeX语法是否正确,避免格式错误。</think>### HBase hbase-env.sh 配置教程及参数说明 #### **1. 定位配置文件** HBase的环境配置文件`hbase-env.sh`位于HBase安装目录的`conf`子目录下。例如: ```bash cd /home/xq/apps/hbase-1.2.0/conf # 根据实际安装路径调整 vim hbase-env.sh ``` [^1] #### **2. 关键参数配置说明** 以下为`hbase-env.sh`中常见参数及作用: - **`JAVA_HOME`** 指定Java安装路径,必须与HBase兼容(如Java 8): ```bash export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 ``` - **`HBASE_HEAPSIZE`** 设置HBase进程的堆内存大小,默认1GB。生产环境建议根据数据量调整: ```bash export HBASE_HEAPSIZE=4G ``` - **`HBASE_MANAGES_ZK`** 控制HBase是否启动内置ZooKeeper(默认`true`)。若使用外部ZooKeeper集群,需设为`false`: ```bash export HBASE_MANAGES_ZK=false ``` - **`HBASE_LOG_DIR`** 定义日志文件存储路径,默认在HBase安装目录下的`logs`子目录: ```bash export HBASE_LOG_DIR=/var/log/hbase ``` - **`HBASE_PID_DIR`** 指定进程ID(PID)文件存储路径,避免出现`No such file or directory`错误[^2]: ```bash export HBASE_PID_DIR=/var/hadoop/pids ``` - **`HBASE_CLASSPATH`** 扩展HBase的类路径,例如集成Hadoop或Spark时需添加相关依赖: ```bash export HBASE_CLASSPATH=$HADOOP_HOME/etc/hadoop ``` 类似Spark的类路径配置可参考[^3]。 #### **3. 配置后验证** 1. 保存文件并重启HBase: ```bash ./stop-hbase.sh # 先停止(若已运行) ./start-hbase.sh ``` 2. 检查日志与进程: ```bash jps # 应显示HMaster、HRegionServer等进程 tail -f $HBASE_LOG_DIR/hbase-root-master.log # 查看实时日志 ``` #### **4. 注意事项** - 修改配置后需重启HBase生效。 - 堆内存过小可能导致OOM错误,过大可能影响系统其他服务。 - 若使用外部ZooKeeper,需单独配置`zoo.cfg`文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值