Hbase java API操作

1 创建maven工程 导入jar包

<repositories><repository><id>cloudera</id><url>https://repository.cloudera.com/artifactory/cloudera-repos/</url></repository></repositories><dependencies><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>2.6.0-mr1-cdh5.14.0</version></dependency><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-client</artifactId><version>1.2.0-cdh5.14.0</version></dependency><dependency><groupId>org.apache.hbase</groupId><artifactId>hbase-server</artifactId><version>1.2.0-cdh5.14.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.0</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding><!--    <verbal>true</verbal>--></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-shade-plugin</artifactId><version>2.2</version><executions><execution><phase>package</phase><goals><goal>shade</goal></goals><configuration><filters><filter><artifact>*:*</artifact><excludes><exclude>META-INF/*.SF</exclude><exclude>META-INF/*.DSA</exclude><exclude>META-INF/*/RSA</exclude></excludes></filter></filters></configuration></execution></executions></plugin></plugins></build>

2 HbaseClient模板

核心代码区

//创建hbase配置对象
Configuration configuration = HBaseConfiguration.create();
//指定访问HBase的地址 在hbase操作中 只需要指定zk就可以了
configuration.set("hbase.zookeeper.quorum","node-1:2181,node-2:2181");
//根据配置属性 过得hbase的连接
Connection connection = ConnectionFactory.createConnection(configuration);
//通过连接获取Hbase客户端主类admin
Admin admin = connection.getAdmin();   //如果是DDL操作 基于admin完成
Table table = connection.getTable();   //如果是DML操作 获取到表之后 基于表操作
public class HbaseClient {private static Connection connection = null;private static Admin admin = null;static {try {//1、配置文件对象Configuration configuration = HBaseConfiguration.create();//指定zk地址  访问hbase不需要指定hmaster地址configuration.set("hbase.zookeeper.quorum","node-1:2181,node-2:2181,node-3:2181");//2、hbase客户端主类connection = ConnectionFactory.createConnection(configuration);//3、创建admin对象admin = connection.getAdmin();} catch (IOException e) {e.printStackTrace();}}//资源关闭public static void close(){if (admin != null){try {admin.close();} catch (IOException e) {e.printStackTrace();}}if (connection!=null){try {connection.close();} catch (IOException e) {e.printStackTrace();}}}public static void main(String[] args) throws IOException {/*调用业务*///final 资源的关闭close();}
}

3 DDL操作—数据定义语言

3.1 判断表是否存在

    public static boolean isTableExist(String tableName) throws IOException {return admin.tableExists(TableName.valueOf(tableName));}

3.2 创建表(表名,列族…)

    public static void createTable(String tableName,String...columnFamilys) throws IOException {//创建表描述器HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));//添加列族信息for (String columnFamily : columnFamilys) {HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(columnFamily);tableDescriptor.addFamily(hColumnDescriptor);}//调用admin createTable方法创建表admin.createTable(tableDescriptor);}

3.3 删除表

 public static void dropTable(String tableName) throws IOException {//删除表之前 先disable表admin.disableTable(TableName.valueOf(tableName));admin.deleteTable(TableName.valueOf(tableName));}

3.4 创建命名空间

 public static void createNameSpace(String nsName) throws IOException {//创建命名空间描述器NamespaceDescriptor namespaceDescriptor =NamespaceDescriptor.create(nsName).build();admin.createNamespace(namespaceDescriptor);}

4 DML操作—数据操纵语言

4.1 向表中插入数据

 public static void putData() throws IOException {//获取表对象Table tb = connection.getTable(TableName.valueOf("myuser"));//创建put对象,并指定rowkeyPut put = new Put(Bytes.toBytes("0001"));//添加列族 列 列值put.addColumn("f1".getBytes(),"id".getBytes(), Bytes.toBytes("1"));put.addColumn("f1".getBytes(),"name".getBytes(), Bytes.toBytes("tony"));put.addColumn("f1".getBytes(),"age".getBytes(), Bytes.toBytes("20"));put.addColumn("f2".getBytes(),"address".getBytes(), Bytes.toBytes("japan"));put.addColumn("f2".getBytes(),"phone".getBytes(), Bytes.toBytes("15888888888"));//插入数据tb.put(put);//关闭表tb.close();}

4.2 批量插入数据

public static void insertBatchData() throws IOException {//获取表Table myuser = connection.getTable(TableName.valueOf("myuser"));//创建put对象,并指定rowkeyPut put = new Put("0002".getBytes());put.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes("2"));put.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("曹操"));put.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes("30"));put.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));put.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("沛国谯县"));put.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("16888888888"));put.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("helloworld"));Put put2 = new Put("0003".getBytes());put2.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes("3"));put2.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("刘备"));put2.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes("32"));put2.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));put2.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("幽州涿郡涿县"));put2.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("17888888888"));put2.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("talk is cheap , show me the code"));Put put3 = new Put("0004".getBytes());put3.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes("4"));put3.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("孙权"));put3.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes("35"));put3.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));put3.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("下邳"));put3.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("12888888888"));put3.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("what are you 弄啥嘞!"));Put put4 = new Put("0005".getBytes());put4.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes("5"));put4.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("诸葛亮"));put4.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes("28"));put4.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));put4.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("四川隆中"));put4.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("14888888888"));put4.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("出师表你背了嘛"));Put put5 = new Put("0006".getBytes());put5.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes("6"));put5.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("司马懿"));put5.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes("27"));put5.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));put5.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("哪里人有待考究"));put5.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15888888888"));put5.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("跟诸葛亮死掐"));Put put6 = new Put("0007".getBytes());put6.addColumn("f1".getBytes(),"id".getBytes(),Bytes.toBytes("7"));put6.addColumn("f1".getBytes(),"name".getBytes(),Bytes.toBytes("xiaobubu—吕布"));put6.addColumn("f1".getBytes(),"age".getBytes(),Bytes.toBytes("28"));put6.addColumn("f2".getBytes(),"sex".getBytes(),Bytes.toBytes("1"));put6.addColumn("f2".getBytes(),"address".getBytes(),Bytes.toBytes("内蒙人"));put6.addColumn("f2".getBytes(),"phone".getBytes(),Bytes.toBytes("15788888888"));put6.addColumn("f2".getBytes(),"say".getBytes(),Bytes.toBytes("貂蝉去哪了"));List<Put> listPut = new ArrayList<Put>();listPut.add(put);listPut.add(put2);listPut.add(put3);listPut.add(put4);listPut.add(put5);listPut.add(put6);myuser.put(listPut);myuser.close();}

4.3 根据rowkey查询数据

 public static void searchDataByRK() throws IOException {//获取表对象Table myuser = connection.getTable(TableName.valueOf("myuser"));//创建get对象 指定查询的rowkeyGet get = new Get(Bytes.toBytes("0001"));//查询数据Result result = myuser.get(get);//从结果中取得所有cellCell[] cells = result.rawCells();//遍历cells 取结果for (Cell cell : cells) {//得到 rowkeySystem.out.println(" 行 键 :" + Bytes.toString(CellUtil.cloneRow(cell)));//得到列族System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));}}

4.4 根据rowkey查询指定列族指定列的数据

 public static void searchData2() throws IOException {//获取表对象Table myuser = connection.getTable(TableName.valueOf("myuser"));//创建get对象 指定查询的rowkeyGet get = new Get(Bytes.toBytes("0002"));//指定对应的列族和列get.addColumn("f1".getBytes(),"name".getBytes());//查询数据Result result = myuser.get(get);//从结果中取得所有cellCell[] cells = result.rawCells();//遍历cells 取结果for (Cell cell : cells) {//得到 rowkeySystem.out.println(" 行 键 :" + Bytes.toString(CellUtil.cloneRow(cell)));//得到列族System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));}}

4.5 通过scan进行全表扫描

 public static void getAllRows() throws IOException{//获取表对象Table myuser = connection.getTable(TableName.valueOf("myuser"));//通过table对象获取一个扫描器对象Scan scan = new Scan();ResultScanner resultScanner = myuser.getScanner(scan);//遍历结果for(Result result : resultScanner){Cell[] cells = result.rawCells();for (Cell cell : cells) {//得到 rowkeySystem.out.println(" 行 键 :" + Bytes.toString(CellUtil.cloneRow(cell)));//得到列族System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));}}myuser.close();}

4.6 通过startRowKey 和endRowKey进行扫描

        Scan scan = new Scan();scan.setStartRow("0004".getBytes());scan.setStopRow("0006".getBytes());ResultScanner resultScanner = myuser.getScanner(scan);

4.7 .根据rowkey删除数据

 public static void  deleteByRowKey() throws IOException {//获取表对象Table myuser = connection.getTable(TableName.valueOf("myuser"));//指定删除的rowkeyDelete delete = new Delete("0001".getBytes());//执行删除操作myuser.delete(delete);myuser.close();}

4.8 比较过滤器-- rowKey过滤器RowFilter

 public static void rowKeyFilter() throws IOException {//获取表对象Table myuser = connection.getTable(TableName.valueOf("myuser"));Scan scan = new Scan();//创建一个row过滤器RowFilter rowFilter = new RowFilter(CompareFilter.CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes("0003")));//给扫描器添加过滤器scan.setFilter(rowFilter);ResultScanner resultScanner = myuser.getScanner(scan);for(Result result : resultScanner){Cell[] cells = result.rawCells();for (Cell cell : cells) {//得到 rowkeySystem.out.println(" 行 键 :" + Bytes.toString(CellUtil.cloneRow(cell)));//得到列族System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));}}myuser.close();}

4.9 比较过滤器–列族过滤器FamilyFilter

 public static void familyFilter() throws IOException {//获取表对象Table myuser = connection.getTable(TableName.valueOf("myuser"));Scan scan = new Scan();//查找比f2列族小的  只有f1列族FamilyFilter familyFilter = new FamilyFilter(CompareFilter.CompareOp.LESS, new SubstringComparator("f2"));scan.setFilter(familyFilter);ResultScanner resultScanner = myuser.getScanner(scan);for(Result result : resultScanner){Cell[] cells = result.rawCells();for (Cell cell : cells) {//得到 rowkeySystem.out.println(" 行 键 :" + Bytes.toString(CellUtil.cloneRow(cell)));//得到列族System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));}}myuser.close();}

4.10 比较过滤器–列过滤器QualifierFilter

 Table myuser = connection.getTable(TableName.valueOf("myuser"));Scan scan = new Scan();ValueFilter valueFilter = new ValueFilter(CompareFilter.CompareOp.EQUAL, new SubstringComparator("8"));

4.11 专用过滤器–单列值过滤器 SingleColumnValueFilter

SingleColumnValueFilter会返回满足条件的整列值的所有字段

 public static void singleColumnFilter() throws IOException {//获取表对象Table myuser = connection.getTable(TableName.valueOf("myuser"));Scan scan = new Scan();SingleColumnValueFilter singleColumnValueFilter = new SingleColumnValueFilter("f1".getBytes(), "name".getBytes(), CompareFilter.CompareOp.EQUAL, "刘备".getBytes());scan.setFilter(singleColumnValueFilter);ResultScanner resultScanner = myuser.getScanner(scan);for(Result result : resultScanner){Cell[] cells = result.rawCells();for (Cell cell : cells) {//得到 rowkeySystem.out.println(" 行 键 :" + Bytes.toString(CellUtil.cloneRow(cell)));//得到列族System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));}}myuser.close();}

4.12 专用过滤器–列值排除过滤器SingleColumnValueExcludeFilter

与SingleColumnValueFilter相反,会排除掉指定的列,其他的列全部返回

4.13 专用过滤器-- rowkey前缀过滤器PrefixFilter

查询以00开头的所有前缀的rowkey

 public static void preFilter() throws IOException {//获取表对象Table myuser = connection.getTable(TableName.valueOf("myuser"));Scan scan = new Scan();PrefixFilter prefixFilter = new PrefixFilter("00".getBytes());scan.setFilter(prefixFilter);ResultScanner resultScanner = myuser.getScanner(scan);for(Result result : resultScanner){Cell[] cells = result.rawCells();for (Cell cell : cells) {//得到 rowkeySystem.out.println(" 行 键 :" + Bytes.toString(CellUtil.cloneRow(cell)));//得到列族System.out.println(" 列 族 " + Bytes.toString(CellUtil.cloneFamily(cell)));System.out.println(" 列 :" + Bytes.toString(CellUtil.cloneQualifier(cell)));System.out.println(" 值 :" + Bytes.toString(CellUtil.cloneValue(cell)));}}myuser.close();}

Hbase java API操作(模板代码)相关推荐

  1. Nosql数据库MongoDB 理论+实践(JAVA API操作) 代码实践

    文章目录 一.数据库 数据库的概念: 数据库分类 关系型数据库 非关系型数据库 二.Mongo 存储的数据类型 下载 三个层次 基本指令(shell) 三.Mongo Manager Free 概念 ...

  2. 使用 Java API 操作 HBase

    使用 Java API 操作 HBase 数据库,就类似HBase Shell,本质上一个是Java 代码,一个是Shell 命令.(hadoop 的文件系统莫不如此,可用Java API 的方式操作 ...

  3. Hbase 完全分布式模式的搭建、命令行操作、Java API操作

    追风赶月莫停留,平芜尽处是春山. 文章目录 追风赶月莫停留,平芜尽处是春山. 环境 Hbase 完全分布式模式的搭建 一.下载安装包,解压到合适位置: 二.配置相关的文件: 三.将Hbase复制到其他 ...

  4. HBase JAVA API(大章鱼版)

    阅读前请注意: 此api中的环境为大章鱼大数据学习平台提供,非此环境,jar包,与程序代码存在一定问题.如果想本地虚拟机运行请参考分布式数据应用,进行操作 任务目标 1.了解HBase语言的基本语法 ...

  5. Kafka系列三 java API操作

    使用java API操作kafka 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...

  6. HBase–常用API操作篇

    2019独角兽企业重金招聘Python工程师标准>>> [常用到的几个类] 1. org.apache.hadoop.hbase.HBaseConfiguration 每一个hbas ...

  7. hbase java api 两种方式

    NoSQL Hbase JAVA API 实例一 导入架包: <dependency><groupId>org.apache.hbase</groupId>< ...

  8. HBase Java API 创建表时一直卡住

    场景 HBase在CentOS上分布集群安装: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119511593 在上面搭建起来H ...

  9. 6 HBase java API访问HBase数据库

    HBase java API访问HBase数据库 package com.hunan.hbase_options;import org.apache.hadoop.conf.Configuration ...

最新文章

  1. dubbo服务发布一之服务暴露
  2. 是否患有新冠肺炎? 你咳嗽一声
  3. Collection View Programming Guide for iOS---(四)---Using the Flow Layout
  4. vscode 加参数运行_VSCode 调试 Webpack 指南
  5. 【struts2】第一个struts2实例
  6. 计算机基础的函数公式,大学计算机基础 excle 公式与函数
  7. PHP笔记-Smarty模板引擎的使用
  8. 笨方法“学习python笔记之条件控制
  9. 初识Jasima-调度仿真系列教程预告
  10. 使用keras创建自己的图像标题生成器
  11. Debian 配置Bind9 DNS服务器
  12. 游戏软件测试用例编写范文,软件测试用例报告模板.doc
  13. CAN 通信协议文档集锦
  14. java算法训练 调和数列问题
  15. oracle 计算母亲节日期,致母亲节:云和数据 一个满满是爱的地方
  16. C++实现十进制转换
  17. vue中如何实现列表的详情页获取及渲染
  18. 【pytorch】torch2trt
  19. 多层嵌套json转换为Map再转换为单层
  20. 图书推荐系统(附源码链接)

热门文章

  1. Studio3t 过期激活办法/以及重新设置使用日期的脚本不可用解决办法/Studio 3T无限激活原创
  2. 跳转到wps查看文件
  3. python两个数组合并排序_合并Python中的排序数组
  4. JAVA中GUI在Button中设置显示文字时中文乱码问题
  5. unity火焰粒子效果
  6. GAN生成对抗网络综述
  7. 浪潮AI的五个关键抉择
  8. 使用canvas对图片进行裁切
  9. 计算机视觉论文-2021-07-12
  10. 04_Python简答题