HBase程序查询

代码:

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;



public class HbaseDemo {
    HBaseAdmin hBaseAdmin;
    String TN="phone";
    HTable hTable;
    @Before
    public void begin() throws MasterNotRunningException, ZooKeeperConnectionException, IOException{
        Configuration conf=new Configuration();
        conf.set("hbase.zookeeper.quorum","node1,node2,node3");
        hBaseAdmin=new HBaseAdmin(conf);
        hTable=new HTable(conf, TN);

    }
    @After
    public void end(){
        if(hBaseAdmin!=null){
            try {
                hBaseAdmin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(hTable!=null){
            try {
                hTable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    Random r=new Random();
    //随机生成手机号码
    public String getPhoneNum(String prefix){
        return prefix+String.format("%08d", r.nextInt(99999999));
    }
    //随机生成时间
    public String getDate(String year){
        return year+String.format("%02d%02d%02d%02d%02d",
                new Object[]{r.nextInt(12)+1,r.nextInt(29)+1,
                        r.nextInt(60),r.nextInt(60),r.nextInt(60)});
    }
    //插入10个手机号 100条通话记录,满足查询   时间降序排序
    @Test
    public void insertDB() throws IOException{
        List<Put> puts=new ArrayList<Put>();

        for(int i=0;i<10;i++){
            String rowkey;
            String phoneNum=getPhoneNum("186");
            for(int j=0;j<100;j++){
                String phoneDate=getDate("2016");
                SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
                try {
                    long dataLong=sdf.parse(phoneDate).getTime();

                    rowkey=phoneNum+(Long.MAX_VALUE-dataLong);
                    System.out.println(rowkey);

                    Put put=new Put(rowkey.getBytes());
                    put.add("cf1".getBytes(),"type".getBytes(),(r.nextInt(2)+"").getBytes());
                    put.add("cf1".getBytes(),"time".getBytes(),(phoneDate).getBytes());
                    put.add("cf1".getBytes(),"pnum".getBytes(),(getPhoneNum("170")).getBytes());

                    puts.add(put);

                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        hTable.put(puts);

    }
    //查询某手机某个月份的所有通话详单
    @Test
    public void scanDB() throws ParseException, IOException{

        //186 9651 3780 二月份的通话详单
        //18619376327
        Scan scan=new Scan();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");

        String startRowkey="18619376327"+(Long.MAX_VALUE-sdf.parse("20160401000000").getTime());
        scan.setStartRow(startRowkey.getBytes());
        String stopRowkey="18619376327"+(Long.MAX_VALUE-sdf.parse("20160201000000").getTime());
        scan.setStopRow(stopRowkey.getBytes());

        ResultScanner rss=hTable.getScanner(scan);
        System.out.println("hehe");
        for(Result rs:rss){
            System.out.println(new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf1".getBytes(), 
                    "type".getBytes())))+"-"+new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf1".getBytes(), 
                            "time".getBytes())))+"-"+new String(CellUtil.cloneValue(rs.getColumnLatestCell("cf1".getBytes(), 
                                    "pnum".getBytes()))));
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值