基于Java API访问操作HBase实例

大数据技术 潘老师 3年前 (2021-04-30) 1780 ℃ (0) 扫码查看



HBase Shell可以基于后台访问HBase,本节将介绍如何基于Java API远程访问操作HBase,实现创建表、增加数据合查询数据。以下代码实例基于HBase的完全分布式环境来展开的,具体的环境情况可参考:

HBase下载、安装与配置(单机版与完全分布式模式)

文章目录 HBase官网下载 HBase安装与配置 HBase官网下载 HBase官网下载地址:点击去下载 我 […]

代码演示

我们新建一个maven项目,在pom.xml中添加如下依赖:

<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-client</artifactId>
  <version>2.4.2</version>
</dependency>
<dependency>
  <groupId>org.apache.hbase</groupId>
  <artifactId>hbase-server</artifactId>
  <version>2.4.2</version>
</dependency>

新建测试类如下:

package com.panizye;
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.HashMap;
import java.util.List;
import java.util.Map;
/**
* <h3>hbase</h3>
*
* @author panziye
* @description <p></p>
* @date 2021-04-29 13:09
**/
public class HBaseDemo {
//核心配置-管理Hbase的配置信息
private static Configuration conf = null;
//连接-管理Hbase连接
private static Connection conn = null;
//管理员-管理Hbase数据库的信息
private static Admin admin = null;
//表对象
private static Table table = null;
//我们这里使用test1表进行演示
private static String tableNameStr = "test1";
//main测试
public static void main(String[] args) {
//初始化放在了每次方法执行前
//1.创建表
createTable();
//2.插入数据
put();
//3.获取1
getCell();
//4.获取2
getRow();
//5.获取3
scanTable();
//6.删除行
deleteRow();
//7.删除表
deleteTable();
}
//1.初始化
public static void init(){
System.out.println("---------------初始化 START-----------------");
//配置文件
Configuration conf= HBaseConfiguration.create();
//设置zk集群
//务必在本地windows系统的hosts文件中配置ip-主机名映射关系,否则请直接用ip
//conf.set("hbase.zookeeper.quorum","192.168.217.100,192.168.217.101,192.168.217.102");
conf.set("hbase.zookeeper.quorum","master,slave1,slave2");
try {
//创建连接
conn = ConnectionFactory.createConnection(conf);
//获取管理员
admin = conn.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("---------------初始化 END-----------------");
}
//2.关闭
public static void close(){
System.out.println("---------------关闭 START-----------------");
try {
if(table != null){
table.close();
}
if(admin != null){
admin.close();
}
if(conn != null){
conn.close();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("---------------关闭 END-----------------");
}
//3.创建表
public static void createTable(){
init();
System.out.println("---------------创建表 START-----------------");
//表名
TableName tableName = TableName.valueOf(tableNameStr);
//列族-多个用数组循环
String[] columnFamilies = {"cf1","cf2"};
try {
//添加表描述
if(admin.tableExists(tableName)){
System.out.println("该表已存在....");
}else {
TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
for(String columnFamily:columnFamilies){
builder.setColumnFamily(ColumnFamilyDescriptorBuilder.of(columnFamily));
}
admin.createTable(builder.build());
System.out.println("表创建成功.....");
}
} catch (IOException e) {
e.printStackTrace();
}finally {
close();
}
System.out.println("---------------创建表 END-----------------");
}
//4.插入数据
public static void put(){
init();
System.out.println("---------------插入数据 START-----------------");
try {
// 取得一个数据表对象
table = conn.getTable(TableName.valueOf(tableNameStr));
// 需要插入数据库的数据集合
List<Put> putList = new ArrayList<Put>();
Put put;
// 生成数据集合
for(int i = 0; i < 3; i++){
//Row Key
String rowKey = "row" + i;
put = new Put(Bytes.toBytes(rowKey));
//列族
String columnFamily = "cf1";
//列
String cloumn = "name";
//数据
String cell = "panziye"+i;
put.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(cloumn), Bytes.toBytes(cell));
//存入集合
putList.add(put);
}
//插入数据
table.put(putList);
} catch (IOException e) {
e.printStackTrace();
}finally {
close();
}
System.out.println("---------------插入数据 END-----------------");
}
//5.获取数据-根据row key、columnFamily、column 读取
public static String getCell(){
init();
System.out.println("---------------读取1 START-----------------");
String value = null;
try {
// 取得一个数据表对象
table = conn.getTable(TableName.valueOf(tableNameStr));
//Row Key
String rowKey = "row1";
Get get = new Get(Bytes.toBytes(rowKey));
//columnFamily列族
String columnFamily = "cf1";
//column列
String column = "name";
get.addColumn(Bytes.toBytes(columnFamily),Bytes.toBytes(column));
//获取
Result result = table.get(get);
List<Cell> cells = result.listCells();
if (cells !=null && !cells.isEmpty()) {
//这里我们打印全部
for(Cell cell:cells){
String v = new String(CellUtil.cloneValue(cell), "UTF-8");
System.out.println("value==="+v);
}
//一般我们如果只拿最新数据直接获取cells.get(0)的值即可
value = new String(CellUtil.cloneValue(cells.get(0)), "UTF-8");
}
} catch (IOException e) {
e.printStackTrace();
}finally {
close();
}
System.out.println("---------------读取1 END-----------------");
return value;
}
//5.获取数据-根据rowkey 获取一行
public static Map<String,String> getRow(){
init();
System.out.println("---------------读取2 START-----------------");
Map<String,String> map = null;
try {
// 取得一个数据表对象
table = conn.getTable(TableName.valueOf(tableNameStr));
//Row Key
String rowKey = "row1";
Get get = new Get(Bytes.toBytes(rowKey));
//获取
Result result = table.get(get);
List<Cell> cells = result.listCells();
map = new HashMap<String,String>();
if (cells !=null && !cells.isEmpty()) {
for(Cell cell:cells){
String qualifier = new String(CellUtil.cloneQualifier(cell));
String value = new String(CellUtil.cloneValue(cell), "UTF-8");
map.put(qualifier,value);
System.out.println("map==="+map);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
close();
}
System.out.println("---------------读取2 END-----------------");
return map;
}
//5.获取数据-扫描全表的内容
public static List<Map<String, String>> scanTable(){
init();
System.out.println("---------------读取3 START-----------------");
List<Map<String, String>> list = new ArrayList<>();
try {
// 取得一个数据表对象
table = conn.getTable(TableName.valueOf(tableNameStr));
ResultScanner rs = null;
Scan scan = new Scan();
//Row Key-从起始Row Key到结束Row Key
String rowkeyStart = "row1";
String rowkeyEnd = "row2";
rs = table.getScanner(scan);
//获取
for (Result r : rs) {
Map<String, String> map = new HashMap<>();
for (Cell cell : r.listCells()) {
String qualifier = new String(CellUtil.cloneQualifier(cell));
String value = new String(CellUtil.cloneValue(cell), "UTF-8");
map.put(qualifier, value);
}
list.add(map);
}
System.out.println("list======="+list);
} catch (IOException e) {
e.printStackTrace();
}finally {
close();
}
System.out.println("---------------读取3 END-----------------");
return list;
}
//6.删除数据-根据Row Key删除行
public static void deleteRow(){
init();
System.out.println("---------------删除行 START-----------------");
try {
table = conn.getTable(TableName.valueOf(tableNameStr));
List<Delete> list = new ArrayList<>();
//需要删除的Row
String rowKey = "row0";
Delete d1 = new Delete(Bytes.toBytes(rowKey));
list.add(d1);
//删除
table.delete(list);
} catch (IOException e){
e.printStackTrace();
}  finally {
close();
}
System.out.println("---------------删除行 END-----------------");
}
//7.删除表
public static void deleteTable(){
init();
System.out.println("---------------删除表 START-----------------");
try {
TableName tableName = TableName.valueOf(tableNameStr);
if (admin.tableExists(tableName)) {
//执行disable
admin.disableTable(tableName);
//执行delete
admin.deleteTable(tableName);
}
} catch (IOException e){
e.printStackTrace();
}  finally {
close();
}
System.out.println("---------------删除表 END-----------------");
}
}

执行结果如下:

---------------初始化 START-----------------
log4j:WARN No appenders could be found for logger (org.apache.hadoop.util.Shell).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
---------------初始化 END-----------------
---------------创建表 START-----------------
表创建成功.....
---------------关闭 START-----------------
---------------关闭 END-----------------
---------------创建表 END-----------------
---------------初始化 START-----------------
---------------初始化 END-----------------
---------------插入数据 START-----------------
---------------关闭 START-----------------
---------------关闭 END-----------------
---------------插入数据 END-----------------
---------------初始化 START-----------------
---------------初始化 END-----------------
---------------读取1 START-----------------
value===panziye1
---------------关闭 START-----------------
---------------关闭 END-----------------
---------------读取1 END-----------------
---------------初始化 START-----------------
---------------初始化 END-----------------
---------------读取2 START-----------------
map==={name=panziye1}
---------------关闭 START-----------------
---------------关闭 END-----------------
---------------读取2 END-----------------
---------------初始化 START-----------------
---------------初始化 END-----------------
---------------读取3 START-----------------
list=======[{name=panziye0}, {name=panziye1}, {name=panziye2}]
---------------关闭 START-----------------
---------------关闭 END-----------------
---------------读取3 END-----------------
---------------初始化 START-----------------
---------------初始化 END-----------------
---------------删除行 START-----------------
---------------关闭 START-----------------
---------------关闭 END-----------------
---------------删除行 END-----------------
---------------初始化 START-----------------
---------------初始化 END-----------------
---------------删除表 START-----------------
---------------关闭 START-----------------
---------------关闭 END-----------------
---------------删除表 END-----------------

版权声明:本站文章,如无说明,均为本站原创,转载请注明文章来源。如有侵权,请联系博主删除。
本文链接:https://www.panziye.com/java/bigdata/3136.html
喜欢 (2)
请潘老师喝杯Coffee吧!】
分享 (0)
用户头像
发表我的评论
取消评论
表情 贴图 签到 代码

Hi,您需要填写昵称和邮箱!

  • 昵称【必填】
  • 邮箱【必填】
  • 网址【可选】