Hadoop生态之数据库HBase的本地安装、基本操作以及Hbase JAVA API

What?

Hbase

在这里插入图片描述
HBase是一个分布式的,面向列的开源数据库,对应于Goole Bigtable,
适合于非结构化数据存储的数据库。
HBase基于列,在需要实时读写、随机访问超大规模数据集时,可以使用HBase。

上图描述Hadoop Ecosystem中的各层结构。其中,HBase位于结构化存储层,Hadoop HDFS为HBase提供了高可靠性的底层存储支持。Hadoop MapReduce为HBase提供了高性能的计算能力,ZooKeeper为HBase提供了稳定服务和故障切换(failover)机制。此外,Pig和Hive还为HBase提供了高层语言支持,使得在HBase上进行数据统计处理变的非常简单。Sqoop则为HBase提供了方便的RDBMS数据导入功能,使得传统数据库数据向HBase中迁移变的非常方便。

在这里插入图片描述
HBase Master负责管理所有的HRegion,HBase Master并不存储HBase服务器的任何数据,HBase逻辑上的表可能会划分为多个HRegion,然后存储在HRegion Server群中,HBase Master Server中存储的是从数据到HRegion Server的映射。

一台机器只能运行一个HRegion服务器,数据的操作会记录在Hlog中,在读取数据时候,HRegion会先访问缓存,如果缓存中没有数据才回到Hstore中上找,每一个列都会有一个Hstore集合,每个Hstore集合包含了很多具体的HstoreFile文件,这些文件是B树结构的,方便快速读取。

How?

Hbase本地安装

1.下载hbase安装包

mkdir -p /data/hbase1  
cd /data/hbase1  
wget http://192.168.1.100:60000/allfiles/hbase1/hbase-1.0.0-cdh5.4.5.tar.gz
 tar -xzvf /data/hbase1/hbase-1.0.0-cdh5.4.5.tar.gz  -C  /apps 
cd /apps  
mv /apps/hbase-1.0.0-cdh5.4.5/  /apps/hbase  

2.将hbase添加至环境变量
sudo vim ~/.bashrc

#hbase  
export HBASE_HOME=/apps/hbase  
export PATH=\$HBASE_HOME/bin:\$PATH 

source ~/.bashrc
hbase version

3.在hbase-env,sh中添加配置信息
cd /apps/hbase/conf
vim hbase-env.sh

export JAVA_HOME=/apps/java  
export HBASE_MANAGES_ZK=true  
export HBASE_CLASSPATH=/apps/hbase/conf  

4.配置hbase-site.xml
mkdir -p /data/tmp/zookeeper-hbase
sudo chown -R zhangyu:zhangyu /data/tmp/zookeeper-hbase
vim hbase-site.xml

<property>  
    <name>hbase.master</name>  
    <value>localhost</value>  
</property>  
 <property>  
     <name>hbase.rootdir</name>  
     <value>hdfs://localhost:9000/hbase</value>  
 </property>  
 <property>  
     <name>hbase.cluster.distributed</name>  
     <value>true</value>  
 </property>  
 <property>  
     <name>hbase.zookeeper.quorum</name>  
     <value>localhost</value>  
 </property>  
 <property>  
     <name>hbase.zookeeper.property.dataDir</name>  
     <value>/data/tmp/zookeeper-hbase</value>  
 </property>  
 hbase.master:HBase主节点地址。

hbase.rootdir:HBase文件所存储的位置。

hbase.cluster.distributed:HBase是否为分布式模式。

hbase.zookeeper.quorum:这里是配置ZooKeeper的服务器的地方。

hbase.zookeeper.property.dataDir:这里表示HBase在ZooKeeper上存储数据的位置。

注意:这里hbase.zookeeper.property.dataDir目录,需要提前创建。

5.编辑hbase/conf/regionservers
vim /apps/hbase/conf/regionservers

localhost 

6.启动hadooop和hbase
cd /apps/hadoop/sbin
./start-all.sh
cd /apps/hbase/bin/
./start-hbase.sh
jps
在这里插入图片描述

  1. 测试,如果我们使用SecureCRT这样的远程访问工具,输错命令时,直接按Backspace键,是不能删除掉前面的文字的。在这里可以使用Ctrl键+Backspace键来删除前面输错的文字。

    hbase shell
    输入list的命令,查看当前有哪些HTable表
    list
    create ‘tb’,‘mycf’
    在这里插入图片描述

Hbase Shell基本操作

在这里插入图片描述

接着上面安装的那一步做。确保启动了hadoop和hbase,hbase shell进入命令行

.使用status命令查看服务器状态
status

.使用whoami命令可查看当前用户
whoami
.使用list命令来查看一下都有哪些表
list
.我们来创建一张表,表的参数如下:
表名为table_name,列族名为f1(语法:create <table>, {NAME => <family>, VERSIONS => <VERSIONS>}

create 'table_name','f1'  

我们也可以指定数据保存的版本数,如:create 'table_name2',{NAME => 'f1', VERSIONS => 2}
.使用exists命令查看table_name表是否存在

exists 'table_name'  

.使用desc命令来查看一下table_name表结构(语法:describe <table>

desc 'table_name'  

.修改table_name的表结构,将TTL(生存周期)改为30天,这里要注意,修改表结构前必须先disable使表失效,修改完成后再使用enable命令,使表重新生效(可用is_enabled 'table_name’或is_disabled 'table_name’判断表的状态)

disable 'table_name'  
alter 'table_name',{NAME=>'f1',TTL=>'2592000'}  
enable 'table_name'  

这里2592000为30天的秒数,再次使用desc命令会发现表的TTL已经改为了2592000

.现在我们使用put命令向table_name表中插入一行数据

(语法:put <table>,<rowkey>,<family:column>,<value>,<timestamp>

put 'table_name','rowkey001','f1:col1','value1'  
put 'table_name','rowkey001','f1:col2','value2'  
put 'table_name','rowkey002','f1:col1','value1'  

这其中,'table_name’为表名,'rowkey001’为rowkey,‘f1:col1’ f1为列族,col1为列,'value1’为值,同一个列族下可以有多个列,同一个rowkey视为同一行。
.使用get命令来查询一下table_name表,rowkey001中的f1下的col1的值

(语法:get <table>,<rowkey>,[<family:column>,....]

get 'table_name','rowkey001', 'f1:col1'  

另一种用法:

get 'table_name','rowkey001', {COLUMN=>'f1:col1'}  

15.查询表table_name,rowkey001中的f1下的所有列值

get 'table_name','rowkey001'  

16.使用scan命令扫描全表(语法:scan <table>, {COLUMNS => [ <family:column>,.... ], LIMIT => num}

scan 'table_name'  

也可以限定扫描表的前几行数据,我们扫描前1行数据

scan 'table_name',{LIMIT=>1}  

由此也可以看出,rowkey相同的数据视为一行数据
.使用count命令,查看table_name表中的数据行数

(语法:count <table>, {INTERVAL => intervalNum, CACHE => cacheNum}

INTERVAL设置多少行显示一次及对应的rowkey,默认1000;CACHE每次去取的缓存区大小,默认是10,调整该参数可提高查询速度

查询表table_name中的数据行数,每10条显示一次,缓存区为200

count 'table_name', {INTERVAL => 10, CACHE => 200}  

由于我们的数据只有2行,所以查询结果为2
.使用delete命令删除table_name表中,rowkey001中的f1:col2的数据

(语法:delete <table>, <rowkey>, <family:column> , <timestamp>,必须指定列名)

delete 'table_name','rowkey001','f1:col2'  

这里需要注意,如果该列保存有多个版本的数据,将一并被删除
.使用deleteall命令,删除table_name表中rowkey002这行数据
(语法:deleteall <table>, <rowkey>, <family:column> , <timestamp>,可以不指定列名,删除整行数据)

deleteall 'table_name','rowkey002'  

.使用truncate命令,删除table_name表中的所有数据
(语法: truncate

其具体过程是: disable table -> drop table -> create table

truncate 'table_name'  

Hbase JAVA API

HBase与Hadoop一样,都是用Java编写的,所以HBase对Java支持是必须的,HBase Java API核心类介绍如下:

1.HBaseConfiguration类

HBaseConfiguration是每一个HBase Client都会使用到的对象,它代表HBase配置信息,有两种构造方式:

①public HBaseConfiguration()

②public HBaseConfiguration(final Configuration c)

2.创建表

创建表通过HBaseAdmin对象操作。HBaseAdmin负责META表信息的处理。

HBaseAdmin提供了createTable方法。

public void createTable(HTableDescriptor desc)

HTableDescriptor表示表的Schema,提供的常用方法有以下两个:

①setMaxFileSize:指定最大的Region大小。

②setMemStoreFlushSize:指定MemStore Flush到HDFS的文件大小。

3.删除表

删除表也是通过HBaseAdmin来操作,删除表之前首先要disable表。这是一个非常耗时的操作,所以不建议频繁删除表。

disable Table和deleteTable分别用来执行disable和delete操作。

4.插入数据

HTable通过put方法插入数据。可以传递单个put对象或List put对象分别实现单条插入和批量插入。

①public void put(final Put put) throws IOException

②public void put(final Listputs) throws IOException

Put提供三种构造方式。

①public Put (byte [] row)

②public Put (byte [] row,RowLock rowLock)

③public Put(Put putToCopy)

5.查询数据

查询分为单条随机查询和批量查询。单条查询通过Row Key在Table中查询某一行的数据,HTable提供了get方法完成单条查询。批量查询通过制定一段Row Key的范围来查询,HTable提供了getScanner方法完成批量查询。

与hadoop api的编写一样,首先需要新建项目,导入相应的工具包,编写相应的api文件

现在介绍导入工具包的另一种方法

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
然后,选中hbasedemolib里面的所有文件,单击右键Build Path=>Add to Build Path选项,就将所有jar包加载到项目里面了
打开文件夹,点击第一个文件,按住shift,点击最后一个包,就可以实现全选
在这里插入图片描述

创建CreateMyTable类文件,编写代码

package myhbase;  
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.HBaseAdmin;  
public class CreateMyTable {  
    public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {  
        String tableName = "mytb";  
        String columnFamily = "mycf";  
        create(tableName, columnFamily);  
    }  
  
    public static Configuration getConfiguration() {  
        Configuration conf = HBaseConfiguration.create();  
        conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");  
        conf.set("hbase.zookeeper.quorum", "localhost");  
        return conf;  
    }  
    public static void create(String tableName, String columnFamily)  
            throws MasterNotRunningException, ZooKeeperConnectionException,  
            IOException {  
        HBaseAdmin hBaseAdmin = new HBaseAdmin(getConfiguration());  
        if (hBaseAdmin.tableExists(tableName)) {  
            System.err.println("Table exists!");  
        } else {  
            HTableDescriptor tableDesc = new HTableDescriptor(tableName);  
            tableDesc.addFamily(new HColumnDescriptor(columnFamily));  
            hBaseAdmin.createTable(tableDesc);  
            System.err.println("Create Table SUCCESS!");  
        }  
    }  
}  

可以通过 hbase shell中
list
describe ‘mytb’;

DeleteMyTable

package myhbase;  
import java.io.IOException;  
import org.apache.hadoop.conf.Configuration;  
import org.apache.hadoop.hbase.HBaseConfiguration;  
import org.apache.hadoop.hbase.client.HBaseAdmin;  
public class DeleteMyTable {  
    public static void main(String[] args) throws IOException {  
        String tableName = "mytb";  
        delete(tableName);  
    }  
  
    public static Configuration getConfiguration() {  
        Configuration conf = HBaseConfiguration.create();  
        conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");  
        conf.set("hbase.zookeeper.quorum", "localhost");  
        return conf;  
    }  
  
    public static void delete(String tableName) throws IOException {  
        HBaseAdmin hAdmin = new HBaseAdmin(getConfiguration());  
        if(hAdmin.tableExists(tableName)){  
            try {  
                hAdmin.disableTable(tableName);  
                hAdmin.deleteTable(tableName);  
                System.err.println("Delete table Success");  
            } catch (IOException e) {  
                System.err.println("Delete table Failed ");  
            }  
        }else{  
            System.err.println("table not exists");  
        }  
    }  
}  

写入数据PutData

某电商网站,后台有买家信息表buyer,每注册一名新用户网站后台会产生一条日志,并写入HBase中。

数据格式为:用户ID(buyer_id),注册日期(reg_date),注册IP(reg_ip),卖家状态(buyer_status,0表示冻结 ,1表示正常),以“\t”分割,数据内容如下:

用户ID   注册日期  注册IP   卖家状态  
20385,2010-05-04,124.64.242.30,1  
20386,2010-05-05,117.136.0.172,1  
20387,2010-05-06 ,114.94.44.230,1  

将数据以buyer_id作为行键写入到HBase的buyer表中,插入之前,需确保buyer表已存在,若不存在,提前创建。

create 'buyer','reg_date' 



package myhbase;  
import java.io.IOException;  
import org.apache.hadoop.conf.Configuration;  
import org.apache.hadoop.hbase.HBaseConfiguration;  
import org.apache.hadoop.hbase.MasterNotRunningException;  
import org.apache.hadoop.hbase.ZooKeeperConnectionException;  
import org.apache.hadoop.hbase.client.HTable;  
import org.apache.hadoop.hbase.client.Put;  
import org.apache.hadoop.hbase.util.Bytes;  
public class PutData {  
    public static void main(String[] args) throws MasterNotRunningException,  
            ZooKeeperConnectionException, IOException {  
        String tableName = "buyer";  
        String columnFamily = "reg_date";  
        put(tableName, "20385", columnFamily, "2010-05-04:reg_ip", "124.64.242.30");  
        put(tableName, "20385", columnFamily, "2010-05-04:buyer_status", "1");  
  
        put(tableName, "20386", columnFamily, "2010-05-05:reg_ip", "117.136.0.172");  
        put(tableName, "20386", columnFamily, "2010-05-05:buyer_status", "1");  
  
        put(tableName, "20387", columnFamily, "2010-05-06:reg_ip", "114.94.44.230");  
        put(tableName, "20387", columnFamily, "2010-05-06:buyer_status", "1");  
  
    }  
    public static Configuration getConfiguration() {  
        Configuration conf = HBaseConfiguration.create();  
        conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");  
        conf.set("hbase.zookeeper.quorum", "localhost");  
        return conf;  
    }  
    public static void put(String tableName, String row, String columnFamily,  
            String column, String data) throws IOException {  
        HTable table = new HTable(getConfiguration(), tableName);  
        Put put = new Put(Bytes.toBytes(row));  
        put.add(Bytes.toBytes(columnFamily),  
                Bytes.toBytes(column),  
                Bytes.toBytes(data));  
        table.put(put);  
        System.err.println("SUCCESS");  
    }  
}  

执行完成后,进入HBase中查看buyer表结果。
scan ‘buyer’

查询 GetData

package myhbase;  
import java.io.IOException;  
import org.apache.hadoop.conf.Configuration;  
import org.apache.hadoop.hbase.HBaseConfiguration;  
import org.apache.hadoop.hbase.client.Get;  
import org.apache.hadoop.hbase.client.HTable;  
import org.apache.hadoop.hbase.client.Result;  
import org.apache.hadoop.hbase.util.Bytes;  
public class GetData {  
    public static void main(String[] args) throws IOException {  
        String tableName = "buyer";  
        get(tableName, "20386");  
    }  
    public static Configuration getConfiguration() {  
        Configuration conf = HBaseConfiguration.create();  
        conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");  
        conf.set("hbase.zookeeper.quorum", "localhost");  
        return conf;  
    }  
    public static void get(String tableName, String rowkey) throws IOException {  
        HTable table = new HTable(getConfiguration(), tableName);  
        Get get = new Get(Bytes.toBytes(rowkey));  
        Result result = table.get(get);  
        byte[] value1 = result.getValue("reg_date".getBytes(), "2010-05-05:reg_ip".getBytes());  
        byte[] value2 = result.getValue("reg_date".getBytes(), "2010-05-05:buyer_status".getBytes());  
        System.err.println("line1:SUCCESS");  
        System.err.println("line2:"  
                + new String(value1) + "\t"  
                + new String(value2));  
    }  
}  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值