随着大数据的到来,传统的关系型数据库显得力不从心,noSQL数据库的出现为当前面临的问题提出了新的解决方案,它摈弃了传统的关系型数据库ACID的特征,采用分布式多点节的方式,更加适合大数据的存储和管理.

Hbase是基于Hadoop的三个开源数据库管理系统中的一个,由于开源、高效、稳定等特点,在业界颇受欢迎。本文将分别在Linux系统下以及windows下讲解如何安装和配置HBase。

(1). 下载安装包

Hbase官网下载页面下载安装包, 然后配置hbase-env.sh和hbase-default.xml。



    1. tar xfz hbase-0.94.2.tar.gz  
    2. cd hbase-0.94.2  
    3. chmod ugo+x ./bin/*.sh


    注意, 要修改 bin/ 目录下的脚本的的权限, 不然启动出错。

    2. 配置 hbase-env.sh 和 hbase-default.xml

    两个配置文件中的一个 hbase-env.sh 已经存在于 conf/ 目录下, 但 hbase-default.xml 并不在 conf/ 目录, 需要从 ./src/main/resources/ 目录拷贝:


    1. cp ./src/main/resources/hbase-default.xml ./conf/ 


    修改 hbase-env.sh 的如下几个地方:


    1. export JAVA_HOME=/home/wuzuyang/software/jdk1.7.0/  
    2. export HBASE_CLASSPATH=/home/wuzuyang/software/hbase-0.94.2/lib/  
    3. export HBASE_PID_DIR=${HBASE_HOME}/tmp  
    4. export HBASE_IDENT_STRING=my


    修改 hbase-default.xml 的如下几个地方:

    1. <name>hbase.rootdir</name> 
    2. <value>/home/work/software/hbase-0.94.2/data</value> 
    3.  
    4. <name>hbase.tmp.dir</name> 
    5. <value>/home/work/software/hbase-0.94.2/tmp</value> 
    6.  
    7. <name>hbase.defaults.for.version</name> 
    8. <value>0.94.2</value>

    3. 启动和停止 HBase:


      1. # 启动  
      2. ./bin/start-hbase.sh  
      3. # 停止  
      4. ./bin/stop-hbase.sh


      如果无法正常停止, 需要手动 kill 进程.

      4. 测试 HBase 是否成功启动:


      1. $ ./bin/hbase shell  
      2. HBase Shell; enter 'help<RETURN>' for list of supported commands.  
      3. Type "exit<RETURN>" to leave the HBase Shell  
      4. Version 0.94.2, r1395367, Sun Oct  7 19:11:01 UTC 2012  
      5.  
      6. hbase(main):001:0> list  
      7. TABLE


      5. 编译 HBase Java 应用程序:


        1. javac -cp ./hbase-0.94.2.jar:./lib/hadoop-core-1.0.3.jar Test.java;  
        2. java -cp ./:./*:./lib/* Test


        以上是在linux环境下的hbase的配置.

        下面是在windows下的配置


           1.Java

        安装JDK:去官网下,安装,配置环境变量

        用鼠标右击“我的电脑”->属性->高级->环境变量

        系统变量->新建->变量名:JAVA_HOME 变量值:D:\Program Files\Java\jdk1.6.0_10(JDK安装路径)

        系统变量->编辑->变量名:Path 在变量值的最前面加上:%JAVA_HOME%\bin;(若已经有Path项,无须另外新建,直接在后面加,但需用 ; 与前面已有项分隔开)

        系统变量->新建->变量名:CLASSPATH 变量值:

        .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;

        最后测试下环境变量是否设置成功:在左下角的搜索框中键入cmd回车,输入javac

        如果出现一大串调试信息证明已安装,如出现 'javac' 不是内部或外部命令,也不是可运行的程序或批处理文件。 说明此次Java环境变量配置出错了。

        2. Cygwin

        Cygwin是一个在windows平台上运行的unix模拟环境,是cygnus solutions公司开发的自由软件(该公司开发了很多好东西,著名的还有eCos,不过现已被Redhat收购)。它对于学习unix/linux操作环境,或者从unix到windows的应用程序移植,或者进行某些特殊的开发工作,尤其是使用gnu工具集在windows上进行嵌入式系统开发,非常有用。随着嵌入式系统开发在国内日渐流行,越来越多的开发者对cygwin产生了兴趣。

        在官方主页http://www.cygwin.com/找到setup.exe下载并运行(保存,有用),"Install from Internet",会有两个选择目录界面,一个是选择Cygwin 安装的目录,另一个是为安装包选择一个临时目录。当 Cygwin 安装完成后,您可以删除这个目录和它的内容。注意安装程序记住了这个目录,并在更新时只下载比这个目录中的包更新的版本。如果您删除这个目录并重新安装, setup将重新安装您选择的所有内容而不管这些包的安装程序在运行目录中已经存在。然后选择一个服务器(找个.cn的应该快吧,我选的差不多100k/s)。跳出Select Package界面选择要安装的组件,HASE只需Cygwin,因此默认即可。设置环境变量:

        系统变量新增:CYGWIN_HOME,值为安装目录

        变量PATH最后添加值: %CYGWIN_HOME%\bin

        卸载:uninstall Cygwin you can simply delete the Root(安装目录) and Local Package directory(临时目录), and the shortcuts that were created during installation.

        3.SSH

        运行前面保存的setup.exe,下一步直到组件选择,安装一下(找到后点skip)

        1.OpenSSH
        2.tcp_wrappers
        3.diffutils
        4.Zlib

        4.HBASE

        官网下载http://hadoop.apache.org/hbase/releases.html压缩包,解压即可

        因为HBASE要安装到Cygwin中,为求方便,最好放在Cygwin安装目录的/usr/local/下,类似/usr/local/hbase-<version>的目录。

         

        5.配置

        1) java

        1.为java建立一个快捷方式到Java home directory(原文,应该是指的jre)在Cygwin命令行下输入:

        LN -s /cygdrive/c/Program\ Files/Java/<jre name> /usr/local/<jre name>

        c/Program\ Files/Java/<jre name>指的是安装jre的路径,如我的是D:\Program Files\Java\jre6即写成d/Program\ Files/Java/jre6

        2.测试是否成功:

        输入:CD /usr/local/<jre name>

        输入:./bin/java –version

        如果输出java的版本号,说明安装正确

        2) SSH

        1.确保管理员权限下运行,通过以下命令确认SSH的权限配置正确:

        .chmod +r /etc/passwd to make the passwords file readable for all
          .chmod u+w /etc/passwd to make the passwords file writable for the owner
          .chmod +r /etc/group to make the groups file readable for all
          .chmod u+w /etc/group to make the groups file writable for the owner
          .chmod 755 /var to make the var folder writable to owner and readable and executable to all

        2.编辑/etc/hosts.allow文件,确保以下两行放在文件的开头:

          .ALL : localhost 127.0.0.1/32 : allow

        2.ALL : [::1]/128 : allow

        3. 配置ssh主机,(管理员权限下运行)输入命令:ssh-host-config,

        (如果询问overwrite an existing /etc/ssh_config,输入yes;)

        (如果询问overwrite an existing /etc/sshd_config,输入yes;)

        当屏幕显示关于“privilege seperation”,输入yes后回车;

        当屏幕显示关于“create local user sshd”,输入yes后回车;

        当屏幕显示关于“install sshd as a service”,输入yes后回车;

        当屏幕显示关于“CYGWIN=”,输入缺省的ntsec(ntsec tty?)后回车;

        (如果询问create the sshd account,输入yes;)

        如果询问use a different user name as service account,输入缺省的no即可;

        如果询问create the cyg_server account,输入yes,并为账号设定密码(你再输入的时候命令行窗口的光标没有变化,不用管它,输入-回车再输入-回车即可);

        当屏幕提示“Host configuration finished. Have fun!”时,就说明已经正常的完成了ssh主机的配置。

        4. 启动ssh服务,也就是启动ssh的服务器功能,在cygwin的控制台中输入如下命令:net start sshd(无效,不解)或者cygrunsrv –-start sshd

        屏幕提示“CYGWIN sshd服务已经启动成果。”,或者没有任何错误提示(对于cygrunsrv命令而言),说明ssh服务已经正常启动。

        5. 建用户组与密码(使Windows和Cygwin的账户一致or协调?),在cygwin的控制台中输入如下命令:

        mkpasswd –cl > /etc/passwd

        mkgroup --local > /etc/group

        至此服务器的配置已经完成,可以基于用户与密码实现从远程登陆SSH服务器。(后面的测试发现,用户名就是Windows的用户名,密码也一样,前面设置的密码岂不没有意义了?)

        6.测试

        打开一个新的Cygwin命令窗口,输入命令:whoami,确认你的用户账号。

        在cygwin的控制台输入如下命令:

        ssh username@hostname

        其中hostname为建立SSH服务器的计算机IP地址或机器名,username为用于登陆该计算机的用户名称,如果根据前面的服务器配置步骤,应当就是该计算现有的某个账户名称。如我的是ssh ytictsoft@10.99.0.54

        如果是所用的客户端计算机是第一次登陆SSH服务器,命令行中会提示“Are you sure you want to continue connecting (yes/no) ?”,只要输入yes即可,SSH服务器会自动将这次的登陆信息存储在/.ssh/known_host文件中。

        接着屏幕中会提示输入登陆账户的密码,也就是ssh服务器中登陆账户的密码,当显示“Fanfare!!! You are successfully logged in to this server!!!”时(我的怎么没有任何信息提示),说明已经成功登陆到ssh服务器计算机内了,需要注意的是,此时在当前控制台内输入的命令都将在ssh服务器计算机里运行。

        如果要退出登陆,只要在命令行中输入exit即可,屏幕中会显示“logout Connection to [hostname] closed.”

        3) HBASE

        HBSE安装路径下的./conf/hbase-env.sh是用来配置它依赖的运行环境的,打开并将下面的语句拷贝到后面

          

        export HBASE_IDENT_STRING=$HOSTNAME as this most likely does not inlcude spaces.(加红部分是原文有的但我实在没看明白是命令语句的一部分还是解释语句,反正我感觉肯定不是命令)

        HBSE安装路径下的./conf/hbase-default.xml为配置文件(不知是不是版本问题,我的是0.90.3,该文件实际上在\src\main\resources里)。改以下值:

          .hbase.rootdir must read e.g. file:///D:/cygwin/root/tmp/hbase/data(版本不一样,实际上应该是file:///D:/cygwin/root/tmp/hbase_${user.name}/hbase,实际上没有hbase文件夹,我自己建了一个)

          .hbase.tmp.dir must read D:/cygwin/root/tmp/hbase/tmp(实际上应该是D:/cygwin/tmp/hbase_${user.name})

        3.hbase.zookeeper.quorum must read 127.0.0.1 because for some reason localhost doesn't seem to resolve properly on Cygwin.

        最后Make sure the configured hbase.rootdir and hbase.tmp.dir directories exist and have the proper rights set up e.g. by issuing a chmod 777 on them.

        HBSE安装路径下的./conf/hbase-default.xml为配置文件(hbase-default.xml是供参考的默认配置文件,在\src\main\resources里,实际上的配置文件应该是./conf/hbase-site.xml)。改以下值(参考hbase-default.xml的写法,增加到hbase-site.xml的<configuration></configuration>里):

          .hbase.rootdir must read e.g. file:///D:/cygwin/root/tmp/hbase/data(版本不一样,实际上应该是file:///D:/cygwin/root/tmp/hbase_${user.name}/hbase,实际上没有hbase文件夹,我自己建了一个)

          .hbase.tmp.dir must read D:/cygwin/root/tmp/hbase/tmp(实际上应该是D:/cygwin/tmp/hbase_${user.name})

        3.hbase.zookeeper.quorum must read 127.0.0.1 because for some reason localhost doesn't seem to resolve properly on Cygwin.

        最后Make sure the configured hbase.rootdir and hbase.tmp.dir directories exist and have the proper rights set up e.g. by issuing a chmod 777 on them.

        测试

        This should conclude the installation and configuration of HBase on Windows using Cygwin. So it's time to test it.
        1. Start a Cygwin terminal, if you haven't already.
        2. Change directory to HBase installation using CD /usr/local/hbase-<version>, preferably using auto-completion.
        3.  Start HBase using the command ./bin/start-hbase.sh
        1. When prompted to accept the SSH fingerprint, answer yes.
        2. When prompted, provide your password. Maybe multiple times.
        3. When the command completes, the HBase server should have started.
        4. However, to be absolutely certain, check the logs in the ./logs directory for any exceptions.
        4. Next we start the HBase shell using the command ./bin/hbase shell
        5. We run some simple test commands
        1. Create a simple table using command create 'test', 'data'
        2. Verify the table exists using the command list
        3. Insert data into the table using e.g.
        4. put 'test', 'row1', 'data:1', 'value1'
        5. put 'test', 'row2', 'data:2', 'value2'
        6. put 'test', 'row3', 'data:3', 'value3'
        7. List all rows in the table using the command scan 'test' that should list all the rows previously inserted. Notice how 3 new columns where added without changing the schema!
        8. Finally we get rid of the table by issuing disable 'test' followed by drop 'test' and verified by list which should give an empty listing.
        6.  Leave the shell by exit
        7. To stop the HBase server issue the ./bin/stop-hbase.sh command. And wait for it to complete!!! Killing the process might corrupt your data on disk.
        8. In case of problems,
        1. verify the HBase logs in the ./logs directory.
        2. Try to fix the problem
        3. Get help on the forums or IRC (#hbase@freenode.net). People are very active and keen to help out!
        4. Stopr, restart and retest the server.