首页 > 技术文章 > HDFS之HBase伪分布安装

googlemeoften 2016-08-22 15:37 原文

1、HBase简介

  HBase是Apache Hadoop中的一个子项目,Hbase依托于Hadoop的HDFS作为最基本存储基础单元,通过使用hadoop的DFS工具就可以看到这些这些数据

  存储文件夹的结构,还可以通过Map/Reduce的框架(算法)对HBase进行操作,如右侧的图所示:

  HBase在产品中还包含了Jetty,在HBase启动时采用嵌入式的方式来启动Jetty,因此可以通过web界面对HBase进行管理和查看当前运行的一些状态,非常轻巧。

2、为什么采用HBase?

  HBase 不同于一般的关系数据库,它是一个适合于非结构化数据存储的数据库.所谓非结构化数据存储就是说HBase是基于列的而不是基于行的模式,这样方面读写你的大数据内容。

  HBase是介于Map Entry(key & value)和DB Row之间的一种数据存储方式。就点有点类似于现在流行的Memcache,但不仅仅是简单的一个key对应一个 value,

  你很可能需要存储多个属性的数据结构,但没有传统数据库表中那么多的关联关系,这就是所谓的松散数据。

  简单来说,你在HBase中的表创建的可以看做是一张很大的表,而这个表的属性可以根据需求去动态增加,在HBase中没有表与表之间关联查询。

  你只需要 告诉你的数据存储到Hbase的那个column families 就可以了,不需要指定它的具体类型:char,varchar,int,tinyint,text等等。但是你需要注意HBase中不包含事务此类的功 能。

  Apache HBase 和Google Bigtable 有非常相似的地方,一个数据行拥有一个可选择的键和任意数量的列。表是疏松的存储的,因此用户可以给行定义各种不同的列,

  对于这样的功能在大项目中非常实用,可以简化设计和升级的成本。

3、Hbase安装:

 1、下载jar,并且解药到对应的目录

tar-zxvf hbase-0.98.21-hadoop1-bin.tar.gz -C /opt/install/

 

 2、配置环境变量

sudo vim /etc/prfile
//添加HABSE_HOME
export HBASE_HOME=/opt/install/hbase
//修改PATH export PATH
=$JAVA_HOME/bin:$HBASE_HOME/bin:$HADOOP_HOME/bin:$PATH

 

   3、 修改$HBASE_HOME/conf/hbase-env.sh,修改内容如下:

export JAVA_HOME=/opt/install/jdk1.7.0_79

//使用HBase自带的zookeeper
export HBASE_MANAGES_ZK=true

 

   4、修改$HBASE_HOME/conf/hbase-site.xml,修改内容如下:

<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://192.168.203.128:9000/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.zookeeper.quorum</name>
<value>192.168.203.168</value>
</property>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>

 

  5、 (可选)修改文件regionservers:

192.168.203.128

  6、 启动hbase,执行命令start-hbase.sh

  ******启动hbase之前,确保hadoop是运行正常的,并且可以写入文件*******


  7、 验证:

(1)执行jps,发现新增加了3个java进程,分别是HMaster、HRegionServer、HQuorumPeer
(2)使用浏览器访问http://hadoop0:60010

 

4、hbase shell操作

  4.1、hbase相关shell命令

 

 4.2对表的操作:

创建表
>create 'users','user_id','address','info'
表users,有三个列族user_id,address,info
列出全部表
>list 
得到表的描述
>describe 'users'

创建表
>create 'users_tmp','user_id','address','info'
删除表
>disable 'users_tmp'
>drop 'users_tmp'

  

 4.3、对记录的操作

//添加记录
put 'users','xiaoming','info:age','24';
put 'users','xiaoming','info:birthday','1987-06-17';
put 'users','xiaoming','info:company','alibaba';
put 'users','xiaoming','address:contry','china';
put 'users','xiaoming','address:province','zhejiang';
put 'users','xiaoming','address:city','hangzhou';
put 'users','zhangyifei','info:birthday','1987-4-17';
put 'users','zhangyifei','info:favorite','movie';
put 'users','zhangyifei','info:company','alibaba';
put 'users','zhangyifei','address:contry','china';
put 'users','zhangyifei','address:province','guangdong';
put 'users','zhangyifei','address:city','jieyang';
put 'users','zhangyifei','address:town','xianqiao';

//取得一个id的所有数据
get 'users','xiaoming'

//获取一个id,一个列族的所有数据
get 'users','xiaoming','info'

//获取一个id,一个列族中一个列的
所有数据
get 'users','xiaoming','info:age'

 

5、Java操作

package cn.edu.hadoop.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;

public class LearnHbase {
    public static void main(String[] args) throws Exception {
        // table();
        record();
    }

    /**
     * 表相关的操作
     * 
     * @throws IOException
     * @throws ZooKeeperConnectionException
     * @throws MasterNotRunningException
     */
    public static void table() throws Exception {
        // 创建表
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://master:9000/hbase");
        // 使用eclipse时必须添加这个,否则无法定位
        conf.set("hbase.zookeeper.quorum", "master");

        HBaseAdmin admin = new HBaseAdmin(conf);
        boolean isexists = admin.tableExists("table");

        if (!isexists) {
            HTableDescriptor desc = new HTableDescriptor("table");
            desc.addFamily(new HColumnDescriptor("family"));
            admin.createTable(desc);
        } else {
            System.out.println("exists.....");
        }

        // 删除表
        admin.disableTable("table");
        admin.deleteTable("table");
    }

    public static void record() throws Exception {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://master:9000/hbase");
        // 使用eclipse时必须添加这个,否则无法定位
        conf.set("hbase.zookeeper.quorum", "master");

        HTable table = new HTable(conf, "users");
        Put put = new Put("xiaoming".getBytes());
        put.add("address".getBytes(), "age".getBytes(), "40".getBytes());
        table.put(put);
        Get get = new Get("xiaoming".getBytes());
        Result result = table.get(get);
        byte[] bytes = result.getValue("address".getBytes(), "age".getBytes());
        System.out.println(new String(bytes));

        table.close();
    }
}

 

推荐阅读