package com.utils; import org.apache.hadoop.conf.Configuration; import java.util.ArrayList; public class HBaseUtil { }
HBase的JAVA客户端API简单使用,及工具类编写
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 java.util.Iterator;
import java.util.List;/** * 连接HBase * @return */ public static Connection getHBaseConnection() throws Exception { Configuration conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum","j01:2181,j02:2181,j03:2181"); Connection conn = ConnectionFactory.createConnection(conf); return conn; } /** * 获取admin对象 * @return * @throws Exception */ public static Admin getHBaseAmin(Connection conn) throws Exception { return conn.getAdmin(); } /** * 获取表对象 * @param tablename * @return * @throws Exception */ public static Table getHBaseTableName(Connection conn,String tablename) throws Exception { Table table = conn.getTable(TableName.valueOf(tablename)); return table; } /** * 展示单元格数据 * @param result */ public static void showData(Result result){ while(result.advance()){ Cell cell = result.current(); String row = new String(CellUtil.cloneRow(cell)); String family = new String(CellUtil.cloneFamily(cell)); String qualifier = new String(CellUtil.cloneQualifier(cell)); String value = new String(CellUtil.cloneValue(cell)); System.out.println(row+"->"+family+":"+qualifier+"->"+value); } } /** * 删除指定表 * @param tablename * @throws Exception */ public static void deleteTable(String tablename) throws Exception { // 创建Admin对象 Admin admin = HBaseUtil.getHBaseAmin(HBaseUtil.getHBaseConnection()); // 获取表 TableName tb = TableName.valueOf("tablename"); // 判断表是否存在,存在则删除 if(admin.tableExists(tb)){ // 禁用表 admin.disableTable(tb); // 判断是否禁用,禁用则删除 if (admin.isTableDisabled(tb)){ admin.deleteTable(tb); }else{ System.out.println("please disable table:" + tablename); } }else{ System.out.println("this table:" + tablename + " is not exits"); } // 释放资源 admin.close(); } /** * 修改表结构:修改列族的版本属性 * @param tableName * @param columnFamilyName * @param columnFamilyVersion * @throws Exception */ public static void modifyTableStructrue(String tableName,String columnFamilyName,int columnFamilyVersion) throws Exception { // 创建Admin对象 Admin admin = HBaseUtil.getHBaseAmin(HBaseUtil.getHBaseConnection()); // 创建列族描述器对象 ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(columnFamilyName.getBytes()); // 设置列族参数 cfdb.setMaxVersions(columnFamilyVersion); // 构建列族 ColumnFamilyDescriptor cfd = cfdb.build(); TableName tb = TableName.valueOf(tableName); // 表存在则修改属性 if(admin.tableExists(tb)){ admin.modifyColumnFamily(tb,cfd); } admin.close(); } /** * 创建预分region表,指定分割点 * @param tableName * @param columnFamilyName * @param regionNum * @throws Exception */ public static void createCustomRegions(String tableName,String columnFamilyName,String...regionNum) throws Exception { // 创建Admin对象 Admin admin = HBaseUtil.getHBaseAmin(HBaseUtil.getHBaseConnection()); // 表的描述器 TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)); // 列族的描述器 ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(columnFamilyName.getBytes()); ColumnFamilyDescriptor cfd = cfdb.build(); // 将列族添加到表中 tdb.setColumnFamily(cfd); TableDescriptor td = tdb.build(); byte[][] by = new byte[regionNum.length][1]; for (int i = 0; i < regionNum.length; i++) { by[i] = regionNum[i].getBytes(); } // 建表,指定预分的region的key admin.createTable(td,by); // 释放资源 admin.close(); } /** * 创建多个列族,并指定,数据版本个数,和数据存储时间 * @param tableName * @param columnFamilyVersion * @param columnFamilyDataTimeLive * @param columnFamilyName * @throws Exception */ public static void createMoreColumnFamilyTable(String tableName,int columnFamilyVersion,int columnFamilyDataTimeLive,String... columnFamilyName) throws Exception { // 创建Admin对象 Admin admin = HBaseUtil.getHBaseAmin(HBaseUtil.getHBaseConnection()); // 创建表的构造器 TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)); // 使用集合存储列族对象 List<ColumnFamilyDescriptor> list = new ArrayList<>(); for (int i = 0; i < columnFamilyName.length; i++) { // 创建列族的构造器 ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(columnFamilyName[i].getBytes()); // 设置列族属性 // 设置列族数据存储的版本数 cfdb.setMaxVersions(columnFamilyVersion); // 设置列族中数据的存储时间 cfdb.setTimeToLive((columnFamilyDataTimeLive)); list.add(cfdb.build()); } // 表的构造器添加列族 tdb.setColumnFamilies(list); // 表描述对象 TableDescriptor td = tdb.build(); // 创建多个列族的表 admin.createTable(td); // 释放资源 admin.close(); } /** * 创建单列族的表,设置列族数据版本个数和数据存储时间 * @param tableName * @param columnFamilyName * @param columnFamilyVersions * @param columnFamilyDataliveTime * @throws Exception */ public static void createOneColumnFamilyTable(String tableName,String columnFamilyName,int columnFamilyVersions,int columnFamilyDataliveTime) throws Exception { // 创建Admin对象 Admin admin = HBaseUtil.getHBaseAmin(HBaseUtil.getHBaseConnection()); // 创建表的构造器 TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName.getBytes())); // 创建列族构造器 ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder(columnFamilyName.getBytes()); // 设置列族属性 // 设置数据版本个数和存储时间 cfdb.setTimeToLive(columnFamilyDataliveTime); cfdb.setMaxVersions(columnFamilyVersions); // 创建列族描述对象 ColumnFamilyDescriptor cfd = cfdb.build(); // 向表中加列族 tdb.setColumnFamily(cfd); // 创建表的描述对象 TableDescriptor td = tdb.build(); // 创建表 admin.createTable(td); // 释放资源 admin.close(); } /** * 删除指定列数据 * @param tableName * @param rowKey * @param family * @param column * @throws Exception */ public static void deleteData(String tableName,String rowKey,String family,String column) throws Exception { // 获取表 Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName); // 创建delete对象,指定删除行 Delete delete = new Delete(rowKey.getBytes()); // 指定删除列数据 delete.addColumn(family.getBytes(),column.getBytes()); // 删除数据 tb.delete(delete); // 释放资源 tb.close(); } /** * 删除指定列族 * @param tableName * @param rowKey * @param family * @throws Exception */ public static void deleteFamily(String tableName,String rowKey,String family) throws Exception { // 获取表 Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName); // 创建delete对象,指定删除行 Delete delete = new Delete(rowKey.getBytes()); // 指定删除列族 delete.addFamily(family.getBytes()); // 删除数据 tb.delete(delete); // 释放资源 tb.close(); } /** * 删除指定行 * @param tableName * @param rowKey * @throws Exception */ public static void deleteRow(String tableName,String rowKey) throws Exception { // 获取表 Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName); // 创建delete对象,指定删除行 Delete delete = new Delete(rowKey.getBytes()); // 删除数据 tb.delete(delete); // 释放资源 tb.close(); } /** * 扫描指定rk范围数据 * @param tableName * @param startRowKey * @param endRowKey * @throws Exception */ public static void scanData(String tableName,String startRowKey,String endRowKey) throws Exception { // 获取表 Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName); // 获取扫描对象 Scan scan = new Scan(); // 从startRowKey开始扫描,到endRowKey结束,含头不含尾 scan.withStartRow(startRowKey.getBytes()); scan.withStopRow(endRowKey.getBytes()); ResultScanner scanner = tb.getScanner(scan); Iterator<Result> it = scanner.iterator(); while(it.hasNext()){ Result result = it.next(); HBaseUtil.showData(result); } tb.close(); } /** * 获取单行或多行数据 * @param tableName * @param rowKey * @throws Exception */ public static void getRowDatas(String tableName,String... rowKey) throws Exception { // 获取表 Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName); // 创建集合存储行数据 List<Get> list = new ArrayList<>(); for (int i = 0; i < rowKey.length; i++) { Get get = new Get(rowKey[i].getBytes()); list.add(get); } Result[] results = tb.get(list); // 展示行数据 for (Result result:results) { HBaseUtil.showData(result); System.out.println(result); } tb.close(); } /** * 指定行,列族,列插入数据 * @param tableName * @param rowKey * @param columnFamily * @param column * @param value * @throws Exception */ public static void putData(String tableName,String rowKey,String columnFamily,String column,String value) throws Exception { // 获取表 Table tb = HBaseUtil.getHBaseTableName(HBaseUtil.getHBaseConnection(), tableName); // 获取Put对象,并设置行健 Put put = new Put(rowKey.getBytes()); // 设置添加值 put.addColumn(columnFamily.getBytes(),column.getBytes(),value.getBytes()); // 添加数据 tb.put(put); tb.close(); }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算