Hbase 简单封装(Hbase 2.0+ API)
前言
封装了一些常用的方法
- 添加一行数据
- 创建表(单列族)
- 创建表(多列族)
- 删除表
- 判断表是否存在
- 获取一行数据(根据rowkey)
- 获取某个列族某个列的某行数据
- 打印出result(方便展示数据)
工具类
类代码:
package com.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;
public class HbaseUtil {
private static ThreadLocal<Connection> connHolder= new ThreadLocal<Connection>();
/**
* 创建connection
* @throws IOException
*/
public static void makeHbaseConnection() throws IOException {
Connection connection = connHolder.get();
if (connection == null){
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop100,hadoop101,hadoop102");
conf.set("hbase.zookeeper.property.clientPort", "2181");
connection = ConnectionFactory.createConnection(conf);
connHolder.set(connection);
}
}
/**
* 关闭连接
* @throws IOException
*/
public static void closeHbseConn() throws IOException {
Connection connection = connHolder.get();
if (connection != null){
connection.close();
connHolder.remove();
}
}
/**
* 添加一行数据
* @param tableName 表名
* @param rowKey 行号
* @param family 列族
* @param column 列名
* @param value
* @throws IOException
*/
public static void insertData(String tableName,String rowKey,String family,String column,String value) throws IOException {
//获取连接
Connection connection = connHolder.get();
//获取表对象
Table table = connection.getTable(TableName.valueOf(tableName));
//获取添加对象
Put put = new Put(Bytes.toBytes(rowKey));
//添加一列
put.addColumn(Bytes.toBytes(family),Bytes.toBytes(column),Bytes.toBytes(value));
//添加
table.put(put);
//关闭
table.close();
}
/**
* 创建表
* @param tableName
* @param family
* @throws IOException
*/
public static void createTable(String tableName,String family) throws IOException {
Connection connection = connHolder.get();
//获取admin
Admin admin = connection.getAdmin();
//列族描述对象建造者
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family));
//设置最大版本号
columnFamilyDescriptorBuilder.setMaxVersions(3);
//列族描述对象
ColumnFamilyDescriptor columnFamilyDescriptor = columnFamilyDescriptorBuilder.build();
//表描述对象建造者
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
//将列族对象添加进表描述
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
//创建表描述对象
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
//创建表
admin.createTable(tableDescriptor);
admin.close();
}
/**
* 创建表(多列族)
* @param tableName
* @param familys
* @throws IOException
*/
public static void createTable(String tableName,String[] familys) throws IOException {
Connection connection = connHolder.get();
//获取admin
Admin admin = connection.getAdmin();
//表描述对象建造者
TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName));
for (String family : familys) {
//列族描述对象建造者
ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family));
//设置最大版本号
columnFamilyDescriptorBuilder.setMaxVersions(3);
//将列族对象添加进表描述
tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptorBuilder.build());
}
//创建表描述对象
TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
//创建表
admin.createTable(tableDescriptor);
admin.close();
}
/**
* 根据行号查询数据
* @param tableName
* @param rowKey
* @return
* @throws IOException
*/
public static Result selectDataByRowkey(String tableName,String rowKey) throws IOException {
Connection connection = connHolder.get();
//获取表
Table table = connection.getTable(TableName.valueOf(tableName));
//获取表描述对象
Get get = new Get(Bytes.toBytes(rowKey));
Result result = table.get(get);
return result;
}
/**
* 获取某一列族中某一列的某一行数据
* @param tableName
* @param rowKey
* @param family
* @param column
* @return
* @throws IOException
*/
public static Result selectDataByCol(String tableName,String rowKey,String family,String column) throws IOException {
Connection connection = connHolder.get();
//获取表
Table table = connection.getTable(TableName.valueOf(tableName));
//获取表描述对象
Get get = new Get(Bytes.toBytes(rowKey));
get.addColumn(Bytes.toBytes(family),Bytes.toBytes(column));
Result result = table.get(get);
return result;
}
/**
* 打印result
* @param result
*/
public static void showResult(Result result){
Cell[] cells = result.rawCells();
for (Cell cell : cells) {
System.out.println("family:" + Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("qualifier:" + Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("row:" + Bytes.toString(CellUtil.cloneRow(cell)));
System.out.println("value:" + Bytes.toString(CellUtil.cloneValue(cell)));
}
}
/**
* 删除表
* @param tableName
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
Connection connection = connHolder.get();
Admin admin = connection.getAdmin();
TableName name = TableName.valueOf(tableName);
if (admin.tableExists(name)){
admin.deleteTable(name);
}
}
/**
* 是否存在表
* @param tableName
* @return
* @throws IOException
*/
public static boolean tableExists(String tableName) throws IOException {
Connection connection = connHolder.get();
Admin admin = connection.getAdmin();
return admin.tableExists(TableName.valueOf(tableName));
}
}
Hbase 简单封装(Hbase 2.0+ API)的更多相关文章
- 一起学HBase——简单介绍HBase各种组件
HBase是谷歌BigTble的开源实现.谷歌的三篇论文拉开了大数据江湖的序幕,铸就了现在以Hadoop为主的大数据技术生态圈.而HBase是开源的大数据数据库,和传统的行式数据库不同的是,HBase ...
- 简单封装kafka相关的api
一.针对于kafka版本 <dependency> <groupId>org.apache.kafka</groupId> <artifactId>ka ...
- Phoenix(sql on hbase)简单介绍
Phoenix(sql on hbase)简单介绍 介绍: Phoenix is a SQL skin over HBase delivered as a client-embedded JDBC d ...
- Hbase简单配置与使用
一. HBase的 二.基于Hadoop的HBase架构 HBase内置有zookeeper,但一般我们会有其他的Zookeeper集群来监管master和regionserver,Zookeeper ...
- Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结
转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...
- [How to] MapReduce on HBase ----- 简单二级索引的实现
1.简介 MapReduce计算框架是二代hadoop的YARN一部分,能够提供大数据量的平行批处理.MR只提供了基本的计算方法,之所以能够使用在不用的数据格式上包括HBase表上是因为特定格式上的数 ...
- hbase简单操作
hbase有hbase shell以及hbase 客户端api两种方式进行hbase数据库操作: 首先,hbase shell是在linux命令行进行操作,输入hbase shell命令,进入shel ...
- HBase操作(Shell与Java API)
版权声明:本文为博主原创文章,未经博主允许不得转载. 转: http://blog.csdn.net/u013980127/article/details/52443155 下面代码在Hado ...
- 简单封装axios api
可以在代码逻辑中写axios请求,处理请求结果,但是随着项目越来越大,代码会很繁琐,不容易维护,所以,可以把一些在所有请求中都要处理的逻辑抽取出来,封装成api方法.比如每次请求中都要判断是否有权限, ...
随机推荐
- Layui弹出层详解
今天空了学习一下弹出层 还是一步步展示把 首先,layer可以独立使用,也可以通过Layui模块化使用.我个人一直是用的模块化的 所以下面素有的都是基于模块化的. 引入好相关文件就可以开始啦 今天放 ...
- spring处理静态资源方式
1. <mvc:default-servlet-handler/>default-servlet-handler在SpringMVC上下文定义一个org.springframework.w ...
- 消失的两个数字(1-N缺两个数)
给定一个数组,包含从 1 到 N 所有的整数,但其中缺了两个数字.你能在 O(N) 时间内只用 O(1) 的空间找到它们吗? 以任意顺序返回这两个数字均可. 示例 1: 输入: [1]输出: [2,3 ...
- 五:key关键字 string字符串 list列表 set集合 Zset有序集合
key 1.keys 命令用于查找所有符合给定模式 pattern 的 key . keys * 查找所有的key返回一个列表 2.EXISTS xxx 检查是否有指定名字为xxx的key 有就返回 ...
- 2020年最新ZooKeeper面试题(附答案)
2020年最新ZooKeeper面试题 1. ZooKeeper 是什么? ZooKeeper 是一个开源的分布式协调服务.它是一个为分布式应用提供一致性服务的软件,分布式应用程序可以基于 Zooke ...
- JetCache 源码分析
一.简介 JetCache是一个基于Java的缓存系统封装,提供统一的API和注解来简化缓存的使用. JetCache提供了比SpringCache更加强大的注解,可以原生的支持TTL.两级缓存.分布 ...
- 精尽 MyBatis 源码分析 - 基础支持层
该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...
- js 进度条效果
<!DOCTYPE html><html><head><meta charset="utf-8"><title>< ...
- 使用phpExcel读取excel文件
include_once '../include/common.inc.php'; include_once MC_ROOT.'include/smarty.inc.php'; date_defaul ...
- 如何将多个网页合并成一个PDF文件
pdfFactory是一款PDF虚拟打印软件,但与其他虚拟打印机软件不同的是,它使用起来更加简单高效.由于无需Acrobat就能生成Adobe PDF文件,它可以帮助用户在系统没有连接打印机的情况下, ...