初识HBase

70 阅读3分钟

简介

Hbase为非关系数据库

Hbase是一个稀疏的多维度的排序(按行键排序)的映射表,

以每一个单元格存储相应的数据,读写均以单元格为单位,

每一个单元格的值均是未经过解释的字符串,即bytes数组

表的大致结构如下

列族(colFamily)
列限定符(col)列限定符(col)
行键(rowkey)单元格单元格
行键(rowkey)单元格单元格

系统安装

dblab.xmu.edu.cn/blog/588/

启动HBase

#跳转到hadoop安装目录
cd /usr/local/hadoop

#启动hadoop
./sbin/start-dfs.sh

#跳转到hbase安装目录
cd /usr/local/hbase

#启动hbase
./bin/start-hbase.sh

#停止hbase
bin/stop-hbase.sh

#启动Hbase的Shell命令行模式
bin/hbase shell

#退出Shell命令行模式
exit
ctrl+z

常用shell命令

#创建数据库表
create 'student','Sname','Ssex','Sage','Sdept','course'

#删除表(分两步,先让该表不可用,然后删除表)
disable 'student'  
drop 'student'

#查看表的结构信息
describe 'student'

#添加数据(一次只能添加一个单元格数据)(95001为行键)(course为列族,math为列限定符)
put 'student','95001','Sname','LiYing'
put 'student','95001','course:math','80'

#删除一个单元格的数据
delete 'student','95001','Ssex'

#删除一行数据
deleteall 'student','95001'

#清空表的数据
truncate 'sc'

#查看数据库表下的所有数据
scan 'student'

#查询某一rowkey数据
get 'student','lisi'

#查询某一单元格的数据
get 'student','lisi','score:Computer'

#查询表历史数据
create 'teacher',{NAME=>'username',VERSIONS=>5}

java实现

导入hbase安装目录中的lib文件中的所有jar包

常用类:

Hbase用java类来存储相应的信息,一般需要通过相应的静态方法就行类型转换或数据匹配。

HTableDescriptor:表的描述类,包装表的信息

HColumnDescriptor:包装列族的信息

Table:表

TableName:表名

Scan:扫描

ResultScanner:扫描结果(元组)的集合

Result:单个元组

Cell:单元格

Put:包装一个行键的数据,用于添加或修改数据(其每个Column表示一条put 语句)

Delete:同上

导入hbase目录下的所有jar包

常用方法:

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;

public class Test {
    private static Configuration configuration;
    private static Connection connection;
    private static Admin admin;

    // 建立连接
    public static void init() {
        configuration = HBaseConfiguration.create();
        configuration.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
        try {
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 关闭连接
    public static void close() {
        try {
            if (admin != null) {
                admin.close();
            }
            if (connection != null) {
                connection.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

admin.listTables(): HTableDescriptor[] 返回当前Hbase下的所有表的信息

public static void listTables() throws IOException {
    init();
	HTableDescriptor[] tables = admin.listTables();
    for (HTableDescriptor table : tables) {
        System.out.println(table.getNameAsString());
    }
	close();
}

table.getScanner(new Scan()):ResultScanner 获得扫描结果(元组集合)

result.rawCells():Cell[] 返回该元组的所有单元格数组

CellUtil.cloneRow(cell)获取cell的行名

cell.getTimestamp() 返回cell的时间戳

CellUtil.cloneFamily(cell) 返回cell的列族

CellUtil.cloneQualifier(cell) 返回cell的行键

CellUtil.cloneValue(cell) 返回cell的值

public static void getData(String tableName) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    ResultScanner scanner = table.getScanner(new Scan());
    for (Result result : scanner) {
        showCell(result);
    }
    table.close();
    close();
}

// 格式化输出
public static void showCell(Result result) {
    Cell[] cells = result.rawCells();
    for (Cell cell : cells) {
        System.out.println("RowName:" + new String(CellUtil.cloneRow(cell)));
        System.out.println("Timetamp:" + cell.getTimestamp());
        System.out.println("column Family:" + new String(CellUtil.cloneFamily(cell)));
        System.out.println("row Name:" + new String(CellUtil.cloneQualifier(cell)));
        System.out.println("value:" + new String(CellUtil.cloneValue(cell)));
    }
}

new Put("xxx".getBytes()) 以xxx为行键插入数据

put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes()); colFamily为列族,col为列限定符,val为cell值,一个Column表示一个put语句,即一个cell的添加或修改

table.put(put) 执行put 语句

如果没有列限定符,如“S_Name”,则将其作为列族,然后其下列限定符为“”(空字符串)

public static void insertRow(String tableName, String rowKey, String colFamily, String col, String val) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Put put = new Put(rowKey.getBytes());
    put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
    table.put(put);
    table.close();
    close();
}

delete.addFamily(Bytes.toBytes(colFamily)); 同上,但删除有两种形式,根据列限定符删除和列族删除

addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col))

public static void deleteColFamily(String tableName, String rowKey, String colFamily) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Delete delete = new Delete(rowKey.getBytes());
    delete.addFamily(Bytes.toBytes(colFamily));
    table.delete(delete);
    table.close();
    close();
}
public static void deleteCol(String tableName, String rowKey, String colFamily, String col) throws IOException {
    init();
    Table table = connection.getTable(TableName.valueOf(tableName));
    Delete delete = new Delete(rowKey.getBytes());
    delete.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col));
    table.delete(delete);
    table.close();
    close();
}

admin.disableTable(TableName.valueOf(tableName)); 停止该表的使用

admin.truncateTable(TableName.valueOf(tableName), false); 清空该表

public static void clearRows(String tableName)throws IOException{
    init();
    admin.disableTable(TableName.valueOf(tableName));
    admin.truncateTable(TableName.valueOf(tableName), false);
    close();
}

admin的方法

admin.tableExists(TableName.valueOf(tableName)); 	//判断HBase中是否存在该表
admin.disableTable(TableName.valueOf(tableName));  	//同	disable 'tableName'
admin.deleteTable(TableName.valueOf(tableName));   	//同	delete 'tableName'
admin.createTable(new HTableDescriptor(TableName.valueOf(tableName)));  //同	create 'tableName' …………

Scan的方法

scan.addFamily(Bytes.toBytes(column));							// 对扫描添加列族约束
scan.addColumn(Bytes.toBytes(cols[0]), Bytes.toBytes(cols[1]));	// 对扫描添加列族和列限定符约束

get方法

Table table = connection.getTable(TableName.valueOf("student"));
Get get = new Get("scofield".getBytes());
Result result = table.get(get);
System.out.println(new String(result.getValue("score".getBytes(), "English".getBytes())));