HBase API
原创大约 5 分钟
就像操作MySQL数据库基本上都是通过编程语言实现的那样,操作HBase中的数据也需要通过API来完成。
Java API
先引入依赖。
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>2.4.18</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-mapreduce</artifactId>
<version>2.4.18</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.2.0</version>
<scope>provided</scope>
</dependency>
对表中的数据增删改查
。
package com.itechthink.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* 使用Java API操作HBase
* 1. 创建和删除表
* 2. 表数据的增删改查
*
*/
public class HBaseCRUD {
public static void main(String[] args) throws Exception {
// 获取HBase数据库连接
Connection conn = getConn();
// 添加数据
// put(conn);
// 查询数据
// get(conn);
/*
* 查询多版本的数据
* 当列的值有多个历史版本的时候
*
* 修改列族info的最大历史版本存储数量
* alter 'user',{NAME=>'info',VERSIONS=>3}
*
* 然后再执行下面命令,向列族info中的age列中添加几次数据,实现多历史版本数据存储
* put 'user', 'lixingyun', 'info:age', '19'
* put 'user', 'lixingyun', 'info:age', '20'
*
*/
// getMoreVersion(conn);
// 删除数据
// delete(conn);
// 获取管理权限,负责对HBase中的表进行DDL操作
Admin admin = conn.getAdmin();
// 创建表
// createTable(admin);
// 删除表
// deleteTable(admin);
// 关闭admin连接
admin.close();
// 关闭连接
conn.close();
}
/**
* 获取连接
*
*/
private static Connection getConn() throws IOException {
// 获取配置
Configuration conf = HBaseConfiguration.create();
// 指定HBase使用的zk的地址,多个用逗号隔开
conf.set("hbase.zookeeper.quorum", "hadoop");
// 指定HBase在hdfs上的根目录
conf.set("hbase.rootdir", "hdfs://hadoop:9000/hbase");
// 创建连接HBase的线程池
return ConnectionFactory.createConnection(conf);
}
/**
* 添加数据
*
*/
private static void put(Connection conn) throws IOException {
// 获取Table,指定要操作的表名,表需要提前创建好
Table table = conn.getTable(TableName.valueOf("user"));
// 指定RowKey,返回put对象
Put put = new Put(Bytes.toBytes("lixingyun"));
// 向put对象中指定列族、列、值(对应的命令)
// put 'user', 'lixingyun', 'info:age', '18'
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("18"));
// put 'user', 'lixingyun', 'info:sex', 'man'
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("gender"), Bytes.toBytes("man"));
// put 'user', 'lixingyun', 'level:type', 'vip'
put.addColumn(Bytes.toBytes("level"), Bytes.toBytes("type"), Bytes.toBytes("vip"));
// 向表中添加数据
table.put(put);
// 关闭table连接
table.close();
}
/**
* 查询数据
*
*/
private static void get(Connection conn) throws IOException {
// 获取Table,指定要操作的表名,表需要提前创建好
Table table = conn.getTable(TableName.valueOf("user"));
// 指定RowKey,返回Get对象
Get get = new Get(Bytes.toBytes("lixingyun"));
// 可以在这里指定要查询指定RowKey数据哪些列族中的列(可选)
// 如果不指定,默认查询指定RowKey所有列的内容
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("age"));
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("gender"));
Result result = table.get(get);
// 如果不清楚HBase中到底有哪些列族和列,可以使用listCells()获取所有cell(单元格),cell对应的是某一列的数据
List<Cell> cells = result.listCells();
for (Cell cell : cells) {
// 获取的信息都是字节类型的,可以通过new String(bytes)转为字符串
// 列族
byte[] famaily_bytes = CellUtil.cloneFamily(cell);
// 列
byte[] column_bytes = CellUtil.cloneQualifier(cell);
// 值
byte[] value_bytes = CellUtil.cloneValue(cell);
System.out.println("列族:" + new String(famaily_bytes) + ",列:" + new String(column_bytes) + ",值:" + new String(value_bytes));
}
System.out.println("---------------------------");
// 如果明确知道HBase中有哪些列族和列,可以使用getValue(family,qualifier)直接获取指定列族中指定列的数据
byte[] age_bytes = result.getValue(Bytes.toBytes("info"), Bytes.toBytes("age"));
System.out.println("age列的值:" + new String(age_bytes));
// 关闭table连接
table.close();
}
/**
* 查询多版本的数据
*
*/
private static void getMoreVersion(Connection conn) throws IOException {
// 获取Table,指定要操作的表名,表需要提前创建好
Table table = conn.getTable(TableName.valueOf("user"));
// 指定RowKey,返回Get对象
Get get = new Get(Bytes.toBytes("lixingyun"));
// 读取cell中的所有历史版本数据,不设置此配置的时候默认只读取最新版本的数据
// 可以通过get.readVersions(2)来指定获取多少个历史版本的数据
get.readAllVersions();
Result result = table.get(get);
// 获取指定列族中指定列的所有历史版本数据,前提是要设置get.readAllVersions()或者get.readVersions(2),否则只会获取最新数据
List<Cell> columnCells = result.getColumnCells(Bytes.toBytes("info"), Bytes.toBytes("age"));
for (Cell cell : columnCells) {
byte[] value_bytes = CellUtil.cloneValue(cell);
long timestamp = cell.getTimestamp();
System.out.println("值为:" + new String(value_bytes) + ",时间戳:" + timestamp);
}
// 关闭table连接
table.close();
}
/**
* 删除数据
*
*/
private static void delete(Connection conn) throws IOException {
// 获取Table,指定要操作的表名,表需要提前创建好
Table table = conn.getTable(TableName.valueOf("user"));
// 指定RowKey,返回Delete对象
Delete delete = new Delete(Bytes.toBytes("lixingyun"));
// 可以在这里指定要删除指定RowKey数据哪些列族中的列(可选)
// delete.addColumn(Bytes.toBytes("info"),Bytes.toBytes("age"));
table.delete(delete);
// 关闭table连接
table.close();
}
/**
* 创建表
*
*/
private static void createTable(Admin admin) throws IOException {
// 指定列族信息
ColumnFamilyDescriptor familyDesc1 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("info"))
// 在这里可以给列族设置一些属性
// 指定最多存储多少个历史版本数据
.setMaxVersions(3)
.build();
ColumnFamilyDescriptor familyDesc2 = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("level"))
// 在这里可以给列族设置一些属性
// 指定最多存储多少个历史版本数据
.setMaxVersions(2)
.build();
ArrayList<ColumnFamilyDescriptor> familyList = new ArrayList<ColumnFamilyDescriptor>();
familyList.add(familyDesc1);
familyList.add(familyDesc2);
// 获取TableDescriptor对象
TableDescriptor desc = TableDescriptorBuilder.newBuilder(TableName.valueOf("test"))
// 指定列族
.setColumnFamilies(familyList)
.build();
// 创建表
admin.createTable(desc);
}
/**
* 删除表
*
*/
private static void deleteTable(Admin admin) throws IOException {
// 删除表前要先禁用表
admin.disableTable(TableName.valueOf("test"));
admin.deleteTable(TableName.valueOf("test"));
}
}
Go API
package main
import (
"context"
"fmt"
"github.com/tsuna/gohbase"
"github.com/tsuna/gohbase/hrpc"
)
func main() {
connect()
......
}
func connect() {
// 创建一个连接到HBase单节点的客户端
client := gohbase.NewClient("localhost")
defer client.Close()
......
}
感谢支持
更多内容,请移步《超级个体》。