hadoop学习(二)----Hbase二---hbasetemplate的使用

hbasetemplate的execute, get, find 方法的使用

package com.changhong.industry.bd.cloudlogservice.service;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.hadoop.hbase.HbaseTemplate;
import org.springframework.data.hadoop.hbase.RowMapper;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author QiYao
 * @date 2018/4/16
 * 查询测试日志服务
 */
@Service
public class QueryTestLogService {

    private static final String TABLE_NAME = "ailinktestdatalog";
    @Autowired
    HbaseTemplate hbaseTemplate;

    public  List>> query(String id){
        List>> md = hbaseTemplate.find(TABLE_NAME, id, (result, rowNum)->{
            Cell[] cells = result.rawCells();
            Map> data = new HashMap<>(16);
            for(Cell c : cells){
                String columnFamily = new String(CellUtil.cloneFamily(c));
                String rowName = new String(CellUtil.cloneQualifier(c));
                String value = new String(CellUtil.cloneValue(c));

                Map obj = data.get(columnFamily);
                if(null == obj){
                    obj = new HashMap<>(16);
                }
                obj.put(rowName, value);
            }
            return data;
        });

        return md;
    }

    /**
     *数据插入
     *@author : zhangai
     *@date : 11:42 2018/6/22
     *@description:
      * @param tableName 表名
     */
    public Boolean execute(String tableName) {
        return  hbaseTemplate.execute(tableName,(hTableInterface)->{
            boolean flag = false;
            try{
                Put put = new Put("123".getBytes());
                hTableInterface.put(put);
                flag = true;
            }catch(Exception e){
                e.printStackTrace();
            }
            return flag;
        });
    }

    /**
     *通过表名和key获取一行数据
     *@author : zhangai
     *@date : 14:21 2018/6/22
     *@description:
      * @param tableName
     * @param rowName
     */
    public Map<String, Object> get(String tableName, String rowName) {
        return hbaseTemplate.get(tableName, rowName,new RowMapper>(){
            @Override
            public Map mapRow(Result result, int i) throws Exception {
                List ceList =   result.listCells();
                Map map = new HashMap(16);
                if(ceList!=null&&ceList.size()>0){
                    for(Cell cell:ceList){
                        map.put(Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+
                                        "_"+Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()),
                                Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
                    }
                }
                return map;
            }
        });
    }


    /**
     * 通过表名  key 和 列族 和列 获取一个数据
     * @param tableName
     * @param rowName
     * @param familyName
     * @param qualifier
     * @return
     */
    public String get(String tableName ,String rowName, String familyName, String qualifier) {
        return hbaseTemplate.get(tableName, rowName,familyName,qualifier ,new RowMapper(){
            @Override
            public String mapRow(Result result, int i) throws Exception {
                List ceList = result.listCells();
                String res = "";
                if(ceList!=null&&ceList.size()>0){
                    for(Cell cell:ceList){
                        res = Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                    }
                }
                return res;
            }
        });
    }

    /**
     * 通过表名,开始行键和结束行键获取数据
     * @param tableName
     * @param startRow
     * @param stopRow
     * @return
     */
    public List<Map<String,Object>> find(String tableName , String startRow,String stopRow) {
        Scan scan = new Scan();
        if(startRow==null){
            startRow="";
        }
        if(stopRow==null){
            stopRow="";
        }
        scan.setStartRow(Bytes.toBytes(startRow));
        scan.setStopRow(Bytes.toBytes(stopRow));
        /* PageFilter filter = new PageFilter(5);
         scan.setFilter(filter);*/
        return  hbaseTemplate.find(tableName, scan,new RowMapper>(){
            @Override
            public Map mapRow(Result result, int rowNum) throws Exception {
                List ceList =   result.listCells();
                Map map = new HashMap();
                Map> returnMap = new HashMap>();
                String  row = "";
                if(ceList!=null&&ceList.size()>0){
                    for(Cell cell:ceList){
                        row =Bytes.toString( cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
                        String value =Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
                        String family =  Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength());
                        String quali = Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength());
                        map.put(family+"_"+quali, value);
                    }
                    map.put("row",row );
                }
                return  map;
            }
        });
    }

    /**
     * 通过表名和key获取一行数据
     * @param id
     * @return
     */
    public Map<String,Object> get(String id){
        Map<String,Object> map = hbaseTemplate.get(TABLE_NAME, id, (result, rowNum)->{
            List listCell = result.listCells();
            Map cellMap = new HashMap(16);
            if(listCell!=null&&listCell.size()>0){
                for(Cell cell:listCell){
                    cellMap.put(Bytes.toString(cell.getFamilyArray(),cell.getFamilyOffset(),cell.getFamilyLength())+
                                    "_"+Bytes.toString( cell.getQualifierArray(),cell.getQualifierOffset(),cell.getQualifierLength()),
                            Bytes.toString( cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
                }
            }
            return  cellMap;
        });
        //补全查询的ID
        map.put("id", id);
        return map;
    }

}

你可能感兴趣的:(hadoop)