溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Hbase中有哪些常用的數(shù)據(jù)庫操作類

發(fā)布時(shí)間:2021-06-24 15:34:22 來源:億速云 閱讀:140 作者:Leah 欄目:大數(shù)據(jù)

這篇文章將為大家詳細(xì)講解有關(guān)Hbase中有哪些常用的數(shù)據(jù)庫操作類,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

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數(shù)據(jù)庫增刪改查操作

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 {

    /**
     * 創(chuàng)建一個(gè)表
     * @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;
    }

    /**
     * 刪除一個(gè)表
     * @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;
    }

    /**
     *  插入一條數(shù)據(jù)
     * @param tableName 表名
     * @param rowkey    主鍵
     * @param cfName    列族
     * @param qualifier 列名
     * @param data   數(shù)據(jù)
     * @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;
    }

    /**
     * 批量添加數(shù)據(jù)
     * @param tableName 表名
     * @param puts      數(shù)據(jù)列表
     * @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查詢數(shù)據(jù)
     * @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 查詢數(shù)據(jù)
     * @param tableName 表名
     * @param startRow  開始的row
     * @param endRow    結(jié)束的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;
    }

    /**
     * 通過過濾器查詢數(shù)據(jù)
     * @param tableName 表名
     * @param startRow  開始
     * @param endRow    結(jié)束
     * @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刪除數(shù)據(jù)
     * @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;

    }

}

關(guān)于Hbase中有哪些常用的數(shù)據(jù)庫操作類就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI