hbase 实战项目

hbase 实战项目首先根据 hadoop 搭建 hbase 搭建把环境弄好由于 hbase 依赖于 hdfs 所以需要进入 hadoop sbin 下启动 start dfs sh start yarn sh 然后进 hbase gt 下启动 start hbase sh 如果后面运行失败报找不到 zookeeper 八成你需要进 hbase bin gt

首先 根据 hadoop 搭建 + hbase 搭建把 环境弄好

由于 hbase 依赖于 hdfs ,所以 需要 进入 hadoop --》sbin 下 启动 start-dfs.sh , start-yarn.sh 然后 进 hbase --> 下 启动 start-hbase.sh 如果 后面 运行失败 报 找不到 zookeeper 八成 你需要进 hbase-bin-> stop-hbase 然后 重run start-hbase.sh

636379-20181119105851808-289675178.png

这里列举下 hbase shell 的常用操作

#最有用命令 help help 'status' #建表 create 'FileTable','fileInfo','saveInfo' #列出有哪些表 list #描述表信息 desc 'FileTable' #统计 表 count 'FileTable' #添加列簇 alter 'FileTable','cf' # 删除列簇 ,一定要注意大小写 alter 'FileTable',{NAME=>'cf',METHOD=>'delete'} #插入数据 put 'FileTable','rowkey1','fileInfo:name','file1.txt' 0 row(s) in 3.9840 seconds put 'FileTable','rowkey1','fileInfo:type','txt' 0 row(s) in 0.0410 seconds put 'FileTable','rowkey1','fileInfo:size','1024' 0 row(s) in 0.1100 seconds put 'FileTable','rowkey1','saveInfo:path','/home/pics' 0 row(s) in 0.1320 seconds put 'FileTable' , 'rowkey1','saveInfo:creator','tom' 0 row(s) in 0.1430 seconds #查询表总行数 hbase(main):016:0> count 'FileTable' 1 row(s) in 0.8500 seconds # get 查询 get 'FileTable','rowkey1' get 'FileTable','rowkey1','fileInfo' #delete #deleteall #scan #删除表之前必须先禁用表Drop the named table. Table must first be disabled: disable 'FileTable' is_enabled is_disabled drop 'FileTable' 

查询所有列簇

636379-20181119134425662-990892802.png

查询指定列簇

636379-20181119134642138-522582948.png

HBase 连接类

package com.ghc.hbase.api; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import org.apache.hadoop.hbase.client.Table; import java.io.IOException; public class HBaseConn { private static final HBaseConn INSTANCE = new HBaseConn(); private static Configuration configuration; private static Connection connection; private HBaseConn(){ try{ if(configuration == null){ configuration = HBaseConfiguration.create(); configuration.set("hbase.zookeeper.quorum","192.168.32.129:2181"); } }catch(Exception e){ e.printStackTrace(); } } private Connection getConnection(){ if(connection == null || connection.isClosed()){ try{ connection = ConnectionFactory.createConnection(configuration); }catch(IOException e){ e.printStackTrace(); } } return connection; } public static Connection getHBaseConnection(){ return INSTANCE.getConnection(); } public static Table getTable(String tableName) throws IOException{ return INSTANCE.getConnection().getTable(TableName.valueOf(tableName)); } public static void closeConn(){ if(connection != null){ try{ connection.close(); }catch (IOException e){ e.printStackTrace(); } } } }

junit 测试一波连接类

import com.ghc.hbase.api.HBaseConn; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.Table; import org.junit.Test; import java.io.IOException; public class HBaseConnTest { @Test public void getConnTest(){ Connection conn = HBaseConn.getHBaseConnection(); System.out.println(conn.isClosed()); HBaseConn.closeConn(); System.out.println(conn.isClosed()); } @Test public void getTableTest(){ Table table = null; try{table = HBaseConn.getTable("FileTable"); System.out.println(table.getName()); }catch(IOException ioe){ ioe.printStackTrace(); } } } 

hbase 增删操作类

package com.ghc.hbase.api; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.HColumnDescriptor; import org.apache.hadoop.hbase.HTableDescriptor; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.filter.FilterList; import org.apache.hadoop.hbase.util.Bytes; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class HBaseUtil { public static Boolean createTable(String tableName,String[] cfs){ try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){ if(admin.tableExists(tableName)) { return false; } // 不存在 则创建 HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); Arrays.stream(cfs).forEach(cf->{ HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf); hColumnDescriptor.setMaxVersions(1); hTableDescriptor.addFamily(hColumnDescriptor); }); admin.createTable(hTableDescriptor); }catch(Exception e){ e.printStackTrace(); } return true; } public static Boolean putRow(String tableName, String rowKey, String cfName,String qualifier,String data){ try(Table table = HBaseConn.getTable(tableName)){ Put put = new Put(Bytes.toBytes(rowKey)); put.addColumn(Bytes.toBytes(cfName), Bytes.toBytes(qualifier), Bytes.toBytes(data)); table.put(put); }catch (IOException ioe){ ioe.printStackTrace(); } return true; } public static Boolean putRows(String tableName,List 
   
     puts){ try(Table table = HBaseConn.getTable(tableName)){ table.put(puts); }catch(IOException ioe){ ioe.printStackTrace(); } return true; } public static Result getRow(String tableName, String rowKey){ try(Table table = HBaseConn.getTable(tableName)){ Get get = new Get(Bytes.toBytes(rowKey)); return table.get(get); }catch(IOException ioe){ ioe.printStackTrace(); } return null; } public static Result getRow(String tableName, String rowKey, FilterList filterList){ try(Table table = HBaseConn.getTable(tableName)){ Get get = new Get(Bytes.toBytes(rowKey)); get.setFilter(filterList); return table.get(get); }catch(IOException ioe){ ioe.printStackTrace(); } return null; } public static ResultScanner getScanner(String tableName){ try(Table table = HBaseConn.getTable(tableName)){ Scan scan = new Scan(); scan.setCaching(1000); return table.getScanner(scan); }catch(IOException ioe){ ioe.printStackTrace(); } return null; } public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey){ try(Table table = HBaseConn.getTable(tableName)){ Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(startRowKey)); scan.setStopRow(Bytes.toBytes(endRowKey)); scan.setCaching(1000); return table.getScanner(scan); }catch(IOException ioe){ ioe.printStackTrace(); } return null; } // 使用过滤器 public static ResultScanner getScanner(String tableName, String startRowKey, String endRowKey, FilterList filterList){ try(Table table = HBaseConn.getTable(tableName)){ Scan scan = new Scan(); scan.setStartRow(Bytes.toBytes(startRowKey)); scan.setStopRow(Bytes.toBytes(endRowKey)); scan.setCaching(1000); scan.setFilter(filterList); return table.getScanner(scan); }catch(IOException ioe){ ioe.printStackTrace(); } return null; } // 删除 public static Boolean deleteRow(String tableName,String rowKey){ try(Table table = HBaseConn.getTable(tableName)){ Delete delete = new Delete(Bytes.toBytes(rowKey)); table.delete(delete); return true; }catch(IOException ioe){ioe.printStackTrace();} return null; } // 删除 列簇 用 admin public static Boolean deleteColumnFamily(String tableName,String cfName){ try(HBaseAdmin admin = (HBaseAdmin)HBaseConn.getHBaseConnection().getAdmin()){ admin.deleteColumn(tableName,cfName); }catch(IOException ioe){ ioe.printStackTrace(); } return true; } //删除 某列 用 table public static Boolean deleteQualifier(String tableName, String rowKey, String cfName,String qualifier){ try(Table table = HBaseConn.getTable(tableName)){ Delete delete = new Delete(Bytes.toBytes(rowKey)); delete.addColumn(Bytes.toBytes(cfName),Bytes.toBytes(qualifier)); table.delete(delete); return true; }catch(IOException ioe){ ioe.printStackTrace(); } return null; } } 
   

转载于:https://www.cnblogs.com/Frank99/p/9981993.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/216114.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午12:31
下一篇 2026年3月18日 下午12:31


相关推荐

  • Wappalyzer-python 安装

    Wappalyzer-python 安装1 主机环境 ubuntu16 04 内核 4 4 0 31 genericx86 64anaconda2 7 4 02 需要安装的 python 模块 requestslxml 已经安装过 PyV8 搞了一个上午 pip 翻墙 svn 源代码各种 也没装成 最后找解决方案的时候看到心明的日志 我也搜到类似的 由于没有 python2 7 版本

    2026年3月20日
    2
  • 深度学习中常见的打标签工具和数据集集合

    深度学习中常见的打标签工具和数据集集合集大家之所长汇集于此 希望对有需要的你能有所帮助 一 打标签工具 1 labelimg labelme 这两款工具简便易行 前者主要用于对目标进行大致的标定 用于常见的框选标定 后者主要用于较为细致的轮廓标定 多用于 maskrcnn 等 安装也是很方便的 直接在终端下用 pipinstallla 即可 至于 labelme 需要先安装 pyqt 所以先 pipinsta

    2026年3月17日
    2
  • Server.MapPath 的使用方法

    Server.MapPath 的使用方法Server.MapPath的使用方法用法:1.Server.MapPath("/")应用程序根目录所在的位置如C:\Inetpub\wwwroot\2.Serve

    2022年6月30日
    24
  • 十进制与八进制和十六进制之间的转换

    十进制与八进制和十六进制之间的转换

    2022年3月5日
    41
  • DataFrame的apply()、applymap()、map()方法[通俗易懂]

    DataFrame的apply()、applymap()、map()方法[通俗易懂]对DataFrame对象中的某些行或列,或者对DataFrame对象中的所有元素进行某种运算或操作,我们无需利用低效笨拙的循环,DataFrame给我们分别提供了相应的直接而简单的方法,apply()和applymap()。其中apply()方法是针对某些行或列进行操作的,而applymap()方法则是针对所有元素进行操作的。1map()方法Themapmethod…

    2022年5月17日
    32
  • mpu6050计步原理_佳明速度传感器安装

    mpu6050计步原理_佳明速度传感器安装下面三部分内容包括:计步原理、基于MPU6050的计步实现、基于MPU6050的久坐检测实现,第一部分为转载内容,做东西最好要了解下关键部分的原理。一、计步原理以下转自:http://www.elecfans.com/yuanqijian/sensor/20170816539797.html1.先要摸清模型的特征目前,大部分设备都提供了可以检测各个方向的加速度传感器…

    2026年4月17日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号