Hbase scan过滤器的使用

来源:互联网 发布:word数据导入excel中 编辑:程序博客网 时间:2024/05/29 02:29

前面一些重复代码不做删除:

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.apache.hadoop.hbase.filter.CompareFilter.CompareOp;import org.apache.hadoop.hbase.filter.FilterList;import org.apache.hadoop.hbase.filter.PrefixFilter;import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;import org.junit.After;import org.junit.Before;import org.junit.Test;public class HbaseDemo {    //对表的操作都需要HBaseAdmin来操作    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();            }        }    }    //插入操作    @Test    public void insert() throws IOException{        String rowkey="13964223261_201612312456";        Put put=new Put(rowkey.getBytes());        put.add("cf1".getBytes(), "type".getBytes(), "1".getBytes());        put.add("cf1".getBytes(), "time".getBytes(), "2016".getBytes());        put.add("cf1".getBytes(), "time".getBytes(), "2016".getBytes());        hTable.put(put);    }    //===================================================================================    //建表    @Test    public void createTable() throws IOException{        if(hBaseAdmin.tableExists(TN)){            //若表已经存在,先禁用表再删除            hBaseAdmin.disableTable(TN);            hBaseAdmin.deleteTable(TN);        }        //设置表名        HTableDescriptor hTableDescriptor=new                 HTableDescriptor(TableName.valueOf(TN));        //设置列族        HColumnDescriptor family=new HColumnDescriptor("cf1");        family.setBlockCacheEnabled(true);        family.setInMemory(true);        family.setMaxVersions(1);        hTableDescriptor.addFamily(family);        hBaseAdmin.createTable(hTableDescriptor);    }    //=================================================================================================    //查表操作    @Test    public void get() throws IOException{        String rowkey="13964223261_201612312456";        Get get=new Get(rowkey.getBytes());        //下面两行表示请求查询服务器时只查询'type'、'time'字段        get.addColumn("cf1".getBytes(), "type".getBytes());        get.addColumn("cf1".getBytes(), "time".getBytes());        Result res=hTable.get(get);        //从结果中拿出需要的结果        Cell cell=res.getColumnLatestCell("cf1".getBytes(), "time".getBytes());        System.out.println("========="+new String(CellUtil.cloneValue(cell)));        Cell cell1=res.getColumnLatestCell("cf1".getBytes(), "type".getBytes());        System.out.println("========="+new String(CellUtil.cloneValue(cell1)));    }    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()))));        }    }    //===============================================================================================================    //查询某个手机所有主叫type=0的通话详单--过滤器实现    @Test    public void scanDB2() throws IOException{        //18619376327        //FilterList.Operator.MUST_PASS_ALL---满足所有过滤器        FilterList list=new FilterList(FilterList.Operator.MUST_PASS_ALL);        //前缀过滤器        PrefixFilter prefixFilter=new PrefixFilter("18619376327".getBytes());        list.addFilter(prefixFilter);        //含义在列族"cf1"中寻找"type"满足"等于(CompareOp.EQUAL)0的结果        SingleColumnValueFilter singleColumnValueFilter=                new SingleColumnValueFilter("cf1".getBytes(), "type".getBytes(), CompareOp.EQUAL, "0".getBytes());        list.addFilter(singleColumnValueFilter);        Scan scan=new Scan();        scan.setFilter(list);        ResultScanner rss=hTable.getScanner(scan);        for(Result rs:rss){            String rowkey=new String(rs.getColumnLatestCell("cf1".getBytes(), "type".getBytes()).getRow());            System.out.println(rowkey+"-"+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
原创粉丝点击