HBase程序查询

来源:互联网 发布:mac应用程序图标大小 编辑:程序博客网 时间:2024/06/09 15:04

代码:

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
原创粉丝点击