关于hbase:Hbase常用数据库操作类

40次阅读

共计 5521 个字符,预计需要花费 14 分钟才能阅读完成。

pom.xml 中援用 hbase-client

<dependency>
            <groupId>org.apache.hbase</groupId>
            <artifactId>hbase-client</artifactId>
            <version>2.2.5</version>
</dependency>

HBaseConn.java 获取 hbase 链接

package com.rumenz;

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.82.177:2181");
              }
          }catch (Exception e){e.printStackTrace();
          }
      }
      private Connection getConnection(){if(connection==null||connection.isClosed()){
               try{connection=ConnectionFactory.createConnection(configuration);
               }catch(Exception e){e.printStackTrace();
               }
          }
          return connection;
      }
      public static Connection getHbaseConn(){return INSTANCE.getConnection();
      }
      public static Table getTable(String table) throws IOException {return INSTANCE.getConnection().getTable(TableName.valueOf(table));
      }
      public static void closeConn(){if(connection!=null){
             try{connection.close();
             }catch(Exception e){e.printStackTrace();
             }
          }
      }

}

HBaseUtil.java 数据库增删改查操作

package com.rumenz;

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.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HBaseUtil {

    /**
     * 创立一个表
     * @param tableName 表名
     * @param cf 列族名
     * @return
     * @throws IOException
     */

    public static boolean createTable(String tableName,String[] cf) throws IOException {try(HBaseAdmin admin = (HBaseAdmin) HBaseConn.getHbaseConn().getAdmin()){if(admin.tableExists(TableName.valueOf(tableName))){return false;}
            List<ColumnFamilyDescriptor> res=new ArrayList<>();
            Arrays.stream(cf).forEach(cff->{res.add(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(cff)).build());
            });
            TableDescriptor hTableDescriptor= TableDescriptorBuilder.newBuilder(TableName.valueOf(tableName)).setColumnFamilies(res).build();
            admin.createTable(hTableDescriptor);
        }catch(Exception e){e.printStackTrace();
        }
        return true;
    }

    /**
     * 删除一个表
     * @param tableName 表名
     * @return
     */

    public static boolean deleteTable(String tableName){try(HBaseAdmin admin= (HBaseAdmin) HBaseConn.getHbaseConn().getAdmin()){admin.disableTable(TableName.valueOf(tableName));
            admin.deleteTable(TableName.valueOf(tableName));
        }catch(Exception e){e.printStackTrace();

        }
        return true;
    }

    /**
     *  插入一条数据
     * @param tableName 表名
     * @param rowkey    主键
     * @param cfName    列族
     * @param qualifier 列名
     * @param data   数据
     * @return
     */
    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 (Exception e){e.printStackTrace();
        }
        return true;
    }

    /**
     * 批量增加数据
     * @param tableName 表名
     * @param puts      数据列表
     * @return
     */
    public static boolean putRows(String tableName,List<Put> puts){try(Table table=  HBaseConn.getTable(tableName)){table.put(puts);
        }catch (Exception e){e.printStackTrace();
        }
        return true;
    }

    /**
     * 通过 rowkey 查问数据
     * @param tableName 表名
     * @param rowkey    rowkey
     * @return
     * @throws IOException
     */
    public static Result getRow(String tableName,String rowkey) throws IOException {try(Table table=HBaseConn.getTable(tableName)){Get get=new Get(Bytes.toBytes(rowkey));
           return table.get(get);
       }catch (Exception e){e.printStackTrace();
       }
        return null;
    }

    /**
     * 通过 scan 查问数据
     * @param tableName 表名
     * @param startRow  开始的 row
     * @param endRow    完结的 row
     * @return
     */
    public static ResultScanner getScanner(String tableName,String startRow,String endRow){try(Table table=HBaseConn.getTable(tableName)){Scan scan=new Scan();
            scan.withStartRow(Bytes.toBytes(startRow));
            scan.withStopRow(Bytes.toBytes(endRow));
            scan.setCaching(1000);
            return  table.getScanner(scan);
        }catch (Exception e){e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过过滤器查问数据
     * @param tableName 表名
     * @param startRow  开始
     * @param endRow    完结
     * @param flist     过滤器
     * @return
     */
    public static ResultScanner getScanner(String tableName, String startRow, String endRow, FilterList flist){try(Table table=HBaseConn.getTable(tableName)){Scan scan=new Scan();
            scan.withStartRow(Bytes.toBytes(startRow));
            scan.withStopRow(Bytes.toBytes(endRow));
            scan.setFilter(flist);
            scan.setCaching(1000);
            return  table.getScanner(scan);
        }catch (Exception e){e.printStackTrace();
        }
        return null;
    }

    /**
     * 通过 rowkey 删除数据
     * @param tableName 表名
     * @param rowKey    rowkey
     * @return
     */
    public static boolean deleteRow(String tableName,String rowKey){try(Table table=HBaseConn.getTable(tableName)){Delete delete=new Delete(Bytes.toBytes(rowKey));
           table.delete(delete);
       }catch (Exception e){e.printStackTrace();
       }
       return true;
    }

    /**
     * 删除列族
     * @param tableName 表名
     * @param cfName    列族
     * @return
     */
    public static boolean deleteColumnFamily(String tableName,String cfName){try(HBaseAdmin admin= (HBaseAdmin) HBaseConn.getHbaseConn().getAdmin()){admin.deleteColumnFamily(TableName.valueOf(tableName),Bytes.toBytes(cfName));
        }catch (Exception e){e.printStackTrace();
        }
        return true;
    }

    /**
     * 删除列名
     * @param tableName  表名
     * @param rowKey     rowkey
     * @param cfName     列族
     * @param qualifier  列名
     * @return
     */
    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);
        }catch (Exception e){e.printStackTrace();
        }
        return true;

    }

}

正文完
 0