前言

封装了一些常用的方法

  • 添加一行数据
  • 创建表(单列族)
  • 创建表(多列族)
  • 删除表
  • 判断表是否存在
  • 获取一行数据(根据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)的更多相关文章

  1. 一起学HBase——简单介绍HBase各种组件

    HBase是谷歌BigTble的开源实现.谷歌的三篇论文拉开了大数据江湖的序幕,铸就了现在以Hadoop为主的大数据技术生态圈.而HBase是开源的大数据数据库,和传统的行式数据库不同的是,HBase ...

  2. 简单封装kafka相关的api

    一.针对于kafka版本 <dependency> <groupId>org.apache.kafka</groupId> <artifactId>ka ...

  3. Phoenix(sql on hbase)简单介绍

    Phoenix(sql on hbase)简单介绍 介绍: Phoenix is a SQL skin over HBase delivered as a client-embedded JDBC d ...

  4. Hbase简单配置与使用

    一. HBase的 二.基于Hadoop的HBase架构 HBase内置有zookeeper,但一般我们会有其他的Zookeeper集群来监管master和regionserver,Zookeeper ...

  5. Hbase框架原理及相关的知识点理解、Hbase访问MapReduce、Hbase访问Java API、Hbase shell及Hbase性能优化总结

    转自:http://blog.csdn.net/zhongwen7710/article/details/39577431 本blog的内容包含: 第一部分:Hbase框架原理理解 第二部分:Hbas ...

  6. [How to] MapReduce on HBase ----- 简单二级索引的实现

    1.简介 MapReduce计算框架是二代hadoop的YARN一部分,能够提供大数据量的平行批处理.MR只提供了基本的计算方法,之所以能够使用在不用的数据格式上包括HBase表上是因为特定格式上的数 ...

  7. hbase简单操作

    hbase有hbase shell以及hbase 客户端api两种方式进行hbase数据库操作: 首先,hbase shell是在linux命令行进行操作,输入hbase shell命令,进入shel ...

  8. HBase操作(Shell与Java API)

    版权声明:本文为博主原创文章,未经博主允许不得转载.     转: http://blog.csdn.net/u013980127/article/details/52443155 下面代码在Hado ...

  9. 简单封装axios api

    可以在代码逻辑中写axios请求,处理请求结果,但是随着项目越来越大,代码会很繁琐,不容易维护,所以,可以把一些在所有请求中都要处理的逻辑抽取出来,封装成api方法.比如每次请求中都要判断是否有权限, ...

随机推荐

  1. -bash: bash_profile: command not found问题

    这个问题一般就是.bash_profile 文件内容错误,里面内容没加注释之内的,vi .bash_profile打开文件检查一下,然后:wq!保存退出 我的错误就是红圈处没有注释造成的

  2. hadoop之yarn(优化篇)

    最近一直在学习hadoop的一些原理和优化,然后也做了一些实践,也有没有去做实践的,反正个人观点都记录下来 一.yarn的介绍 YARN的基本结构由一个ResourceManager与多个NodeMa ...

  3. Exactly Once 语义

    将服务器的 ACK 级别设置为-1,可以保证 Producer 到 Server 之间不会丢失数据,即 At Least Once 语义. 相对的,将服务器 ACK 级别设置为 0,可以保证生产者每条 ...

  4. one-wallhaven 一个壁纸程序

    one-wallhaven 一款基于 Electron 壁纸客户端 . gitee:https://gitee.com/ml13/wallhaven-electron github:https://g ...

  5. java实现 阿拉伯数字转换为汉字数字(转载)

    public class VedioExtractSpeech { public static void main(String[] args) { System.out.println(" ...

  6. Ceph S3 基于NGINX的集群复制方案

    前言 ceph的s3数据的同步可以通过radosgw-agent进行同步,同region可以同步data和metadata,不同region只能同步metadata,这个地方可以参考下秦牧羊梳理的 c ...

  7. Hive 报错Class path contains multiple SLF4J bindings.

    进入hive报错信息如下 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/ ...

  8. ctf-misc-图片隐写术套路总结

    1.直接右键notepad打开,搜索flag,如果图片很多的话,可以写py脚本也    可以打开后搜索全部打开文件 2.是一个压缩包,改了后缀 3.图片中藏了一个二维码,用Stegsolve加几次滤镜 ...

  9. 打包错误:Failed to execute goal org.scala-tools:maven-scala-plugin:2.15.2:compile (default) on project MusicProject: wrap: org.apache.commons.exec.ExecuteException:

    错误:Failed to execute goal org.scala-tools:maven-scala-plugin:2.15.2:compile (default) on project Mus ...

  10. 面试官:小伙子,你给我说一下Java中什么情况会导致内存泄漏呢?

    概念 内存泄露:指程序中动态分配内存给一些临时对象,但对象不会被GC回收,它始终占用内存,被分配的对象可达但已无用.即无用对象持续占有内存或无用对象的内存得不到及时释放,从而造成的内存空间浪费. 可达 ...