Hbase-增删查改(Java版本)

API

package com.fromjoker;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;


public class MyHbase {
    private static Configuration conf;
    private static Connection hbaseConn;
    private static final String QUORUM = "zookeeper-0.zookeeper.default.svc.cluster.local,zookeeper-1.zookeeper.default.svc.cluster.local,zookeeper-2.zookeeper.default.svc.cluster.local";
    private static final String CLIENT_PORT = "2181";
    //Hbase参数设置
    static
    {
        conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", QUORUM);
        conf.set("hbase.zookeeper.property.clientPort", CLIENT_PORT);
        conf.set("hbase.master", "kylin-master-0.kylin-master.default.svc.cluster.local:60000");
        conf.set("dfs.datanode.socket.write.timeout","1000000");
        conf.set("dfs.datanode.handler.count","20");
        try {
            hbaseConn = ConnectionFactory.createConnection(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //hbase链接获取
    public static Connection getHbaseConn(){
        if(hbaseConn == null){
            try {
                hbaseConn = ConnectionFactory.createConnection(conf);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return hbaseConn;
    }
    //hbase表结构获取 表中有数据才能获取结构!!!
    public static List<String> getTableStructure(String tablename,String Family)throws IOException{
        if(hbaseConn != null){
            Table table = hbaseConn.getTable(TableName.valueOf(tablename));
            ResultScanner results = table.getScanner(new Scan());
            List<String> list = new ArrayList<>();
            Result result = results.next();
            if(result.isEmpty()) {
                System.out.println("表中无数据,无法获取表结构");
                return null;
            }
            Map<byte[], byte[]> familyMap = result.getFamilyMap(Bytes.toBytes(Family));
            for (Map.Entry<byte[], byte[]> entry : familyMap.entrySet()) {
                String ss = new String(entry.getKey());
                list.add(ss);
            }
            return list;
        }
        return new ArrayList<>();
    }
    //hbase插入数据
    public static boolean insert(String tablename,String family,String key,List<String> value)throws IOException{
        if(hbaseConn != null) {
            Table table = hbaseConn.getTable(TableName.valueOf(tablename));
            List<String> column = getTableStructure(tablename,family);
            int col_len = column.size();
            int val_len = value.size();
            if(col_len != val_len)
                return false;
            Put line = new Put(key.getBytes());
            for(int i=0;i<col_len;i++) {
                line.addColumn(family.getBytes(),column.get(i).getBytes(),value.get(i).getBytes());
            }
            table.put(line);
            return true;
        }
        return false;
    }
    //hbase更新数据
    public static boolean update(String tablename,String family,String key,List<String> column,List<String> value)throws IOException{
        if(hbaseConn != null) {
            Table table = hbaseConn.getTable(TableName.valueOf(tablename));
            Put line = new Put(key.getBytes());
            for(int i=0;i<column.size();i++) {
                line.addColumn(family.getBytes(),column.get(i).getBytes(),value.get(i).getBytes());
            }
            table.put(line);
            return true;
        }
        return false;
    }
    //扫描全表数据
    public static void scanTable(String tableName) throws Exception{
        try (
            Table table =  Spark2Hbase.getHbaseConn().getTable(TableName.valueOf(tableName));
        ){
            ResultScanner rs = table.getScanner(new Scan());
            printResultScanner(rs);
        }
    }
    //清空表数据
    public static boolean truncateTable(String tablename) {
        try {
            HBaseAdmin admin = new HBaseAdmin(conf);
            TableName table = TableName.valueOf(tablename);
            System.out.println("开始清空数据...");
            admin.disableTable(table);
            admin.truncateTable(table,true);
            System.out.println("已清空该表");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    //根据rowkey查询数据
    public static void queryByRowkey(String tableName,String rowkey) {
        try (
                HTable table = (HTable) getHbaseConn().getTable(TableName.valueOf(tableName));
        ){
            Get scan = new Get(rowkey.getBytes());
            Result res = table.get(scan);
            printResult(res);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //条件查询hbase数据 可输入多条件
    public static void queryByCondition(String tableName,String Family,List<Condition> conditions) {

        try {
            HTable table = (HTable) getHbaseConn().getTable(TableName.valueOf(tableName));
            List<Filter> filters = new ArrayList<Filter>();
            //过滤条件遍历
            for(int i=0;i<conditions.size();i++) {
                Filter filter = new SingleColumnValueFilter(
                        Bytes.toBytes(Family),
                        Bytes.toBytes(conditions.get(i).getColomn()), CompareFilter.CompareOp.EQUAL,
                        Bytes.toBytes(conditions.get(i).getValue()));
                filters.add(filter);
            }
            FilterList filterList = new FilterList(filters);
            Scan scan = new Scan().setFilter(filterList);
            ResultScanner rs = table.getScanner(scan);
            printResultScanner(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //打印扫描结果
    public static void printResultScanner(ResultScanner rs){
        int i=0;
        for(Result res:rs){
            for(Cell cell:res.rawCells()){
                String row = "rowkey: "+Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                String colName = " colomn: "+Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
                String value = " value: "+Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                System.out.println(row+colName+value);
            }
            ++i;
        }
        System.out.println("count: "+i);
    }
    //打印单条扫描结果
    public static void printResult(Result rs){
        for(Cell cell:rs.rawCells()){
            String row = "rowkey: "+Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
            String colName = " colomn: "+Bytes.toString(cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
            String value = " value: "+Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
            System.out.println(row+colName+value);
        }
    }
}

Condition.class

package com.fromjoker;
public class Condition {
    private String colomn;
    private String value;

    public Condition(String colomn,String value){
        this.colomn = colomn;
        this.value = value;
    }
    public String getColomn() {
        return colomn;
    }

    public void setColomn(String colomn) {
        this.colomn = colomn;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值