java操作hbase

首先要生成table来操作hbase
但是需要resource下面引入
在这里插入图片描述
下面是代码

package util;

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;

/**
 * @Author: zhugl
 * @Description:
 * @Date: Create in 15:41 2019/6/12
 */

public class HbaseConfig {

    static Configuration cfg;
    static Connection connection;

    static {

        try {
            cfg = HBaseConfiguration.create();
            connection = ConnectionFactory.createConnection(cfg);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(cfg.get("hbase.master"));
    }

    public static Connection getHbaseConnection(){
      return   connection;
    }

    public static Configuration getHbaseconf(){
        return   cfg;
    }

    public static Table getTable(String tablename){
        Table table = null;
        try {
             table = connection.getTable(TableName.valueOf(tablename));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return table;


    }




}

操作hbase

package interfaceForData.serviceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import interfaceForData.service.ResolvingService;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import util.DateUtil;
import util.GetRowKey;
import util.HbaseConfig;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 帅逼
 * @Description:
 * @Date: Create in 14:27 2019/5/31
 */
@Component("ResolvingService")
public class ResolvingServiceImpl implements ResolvingService {

    Logger logger = Logger.getLogger(ResolvingServiceImpl.class);
    //解析
    @Override
    public JSONObject getResolvingResult(String json) {

//        id
//        time


        List<String> list =new ArrayList<>();


        try{

            final JSONObject jsonObject = JSON.parseObject(json);
            String vin = jsonObject.getString("id");
            String time = jsonObject.getString("time");
            if(StringUtils.isEmpty(vin) || StringUtils.isEmpty(time)){
                logger.info("resolving:     参数不正确!!!");

                return JSON.parseObject("{\"code\":\"2004\",\"message\":\"参数有误!\"}");
            }


           // DateUtil.getTimeTamp(time)
            final String rowKey = GetRowKey.getRowKey(DateUtil.getTimeTamp(time), vin);
          //  System.out.println(rowKey);
            final Config load = ConfigFactory.load();
            String tableName = load.getString("table.name");
            Table table= HbaseConfig.getTable(tableName);
            Get get=new Get(Bytes.toBytes(rowKey));
            Result result=table.get(get);
		//把result 里保存的数据赋值给cell[]

            Cell[] cells=result.rawCells();
            for(Cell cell:cells) {

		//数据内容
                list.add(Bytes.toString(CellUtil.cloneValue(cell)));

            }
   }catch (Exception e){e.printStackTrace();}
        if(list.size()==0){
            logger.info("resolving:     hbase中查不到该数据!!!");

           return JSON.parseObject("{\"code\":\"2001\",\"message\":\"查询不到数据\"}");
        }

         JSONObject jsonObject = JSON.parseObject(list.get(0));

        jsonObject.put("code","2000");
        logger.info("resolving:     查询成功!!!");

        return jsonObject ;
    }
}

分页查询获取页码

  /**
     * 获取页数
     *
     * @param startRowKey 开始rowKey
     * @param endRowkey 结束rowKey
     * @return 返回页数
     */
    public static int getPages(String startRowKey,String endRowkey,int pageSize) {
        final Config load = ConfigFactory.load();
        String tableName = load.getString("table.name");
        AggregationClient aggregation=null;
        HTable hTable = null;

        final Connection connection = HbaseConfig.getHbaseConnection();
        final Configuration cfg = HbaseConfig.getHbaseconf();

        final Log log = LogFactory.getLog("logger");

        if (StringUtils.isBlank(startRowKey) || StringUtils.isBlank(endRowkey))
              {
            log.error("===Parameters tableName|ddate|pageSize should not be blank,Please check!===");
            return 0;
        }
        int total = 0;
        try {
             hTable = (HTable) HbaseConfig.getTable(tableName);

//            Filter rowFilter = new RowFilter(CompareFilter.CompareOp.EQUAL,
//                    new SubstringComparator(startRowKey));
            Scan scan = new Scan();
            scan.setStartRow(Bytes.toBytes(startRowKey));
            scan.setStopRow(Bytes.toBytes(endRowkey));


          //  scan.setFilter(rowFilter);
             aggregation = new AggregationClient(cfg);
            Long count = aggregation.rowCount(hTable,
                    new LongColumnInterpreter(), scan);
            total = count.intValue();



        } catch (Throwable e) {
            // TODO Auto-generated catch block
            log.error(e);
        }finally {

            try {
                hTable.close();
                aggregation.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return (total%pageSize==0)?total/pageSize:total/pageSize+1 ;
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值