HBase之Protocolbuffer应用

来源:互联网 发布:yyf 知乎 编辑:程序博客网 时间:2024/06/05 20:51

Step 1:
使用Protocolbuffer生成Cdr.java文件
http://blog.csdn.net/ymf827311945/article/details/72923577

Rowkey 设计:手机号_(Long.MAXVALUE- 通话时间)
table_name:t_phone_cdr

Step 2:
Phonetest.java

    private Configuration config =null;    private byte[] cf ="cf1".getBytes();    private Random r =new Random();

Step 3:
Phonetest.java——before

    public void before(){        config=new Configuration();        config.set("hbase.zookeeper.quorum", "node11,node12,node13");        config =HBaseConfiguration.create(config);//hbase 默认的配置加载    }

Step 4:
Phonetest.java——createtable

    public void createTable()throws Exception{        HBaseAdmin admin =new HBaseAdmin(config);        HTableDescriptor htable =new HTableDescriptor(TableName.valueOf("t_phone_cdr"));        htable.addFamily(new HColumnDescriptor(cf).setMaxVersions(2).setInMemory(true).setBlockCacheEnabled(true));        if(admin.tableExists("t_phone_cdr")){            admin.disableTable("t_phone_cdr");            admin.deleteTable("t_phone_cdr");        }        admin.createTable(htable);        admin.close();//finally    }

Step 5:
Phonetest.java——inserts()

    public void inserts()throws Exception{        HTable table =new HTable(config, "t_phone_cdr");        for(int i=0;i<1000;i++){            String phone=getPhone("186");            List<Put> list =new ArrayList<Put>();            for(int j=0;j<1000;j++){                String date =getDate("2016");                String rowkey =phone+"_"+(Long.MAX_VALUE-Long.parseLong(date));                Put put =new Put(rowkey.getBytes());                put.add(cf, "dest".getBytes(), getPhone("139").getBytes());                put.add(cf, "type".getBytes(), (r.nextInt(2)+"").getBytes());                put.add(cf, "city".getBytes(), "shanghai".getBytes());                put.add(cf, "time".getBytes(), date.getBytes());                list.add(put);            }            table.put(list);        }        table.close();//finally    }

Step 6:
Phonetest.java——inserts2()

    public void inserts2()throws Exception{        HTable table =new HTable(config, "t_phone_cdr");        for(int i=0;i<1000;i++){            String phone=getPhone("186");            String day =getDay("2016");            String rowkey =phone+"_"+(Integer.MAX_VALUE-Integer.parseInt(day));            Put put =new Put(rowkey.getBytes());            Cdr.days_cdr.Builder days_cdr =Cdr.days_cdr.newBuilder();            for(int j=0;j<100;j++){                Cdr.phone_cdr.Builder phone_cdr =Cdr.phone_cdr.newBuilder();                phone_cdr.setDest(getPhone("139"));                phone_cdr.setType(r.nextInt(2));                phone_cdr.setTime(getDayDate(day));                phone_cdr.setCity("上海");                days_cdr.addListCdr(phone_cdr);            }            put.add(cf, "cdr".getBytes(),days_cdr.build().toByteArray() );            table.put(put);        }        table.close();//finally    }

Step 7:
Phonetest.java——getOneRow()

    public void getOneRow()throws Exception{        HTable table =new HTable(config, "t_phone_cdr");        Get get =new Get("18501796701_9223351876326583360".getBytes());        Result res = table.get(get);//result 相当于hbase表中一行数据        System.out.println(new String( CellUtil.cloneValue(res.getColumnLatestCell(cf, "dest".getBytes()))));        System.out.println(new String( CellUtil.cloneValue(res.getColumnLatestCell(cf, "type".getBytes()))));        System.out.println(new String( CellUtil.cloneValue(res.getColumnLatestCell(cf, "city".getBytes()))));        table.close();//finally    }

Step 8:
Phonetest.java——getOneRow2()

    public void getOneRow2()throws Exception{        HTable table =new HTable(config, "t_phone_cdr");        Get get =new Get("18699903693_2127323234".getBytes());        Result res = table.get(get);//result 相当于hbase表中一行数据        byte[] data =CellUtil.cloneValue(res.getColumnLatestCell(cf, "cdr".getBytes()));        Cdr.days_cdr days_cdr =Cdr.days_cdr.parseFrom(data);        for (Iterator i = days_cdr.getListCdrList().iterator(); i.hasNext();) {            Cdr.phone_cdr phone_cdr = (Cdr.phone_cdr) i.next();            System.out.println(phone_cdr.getDest());        }        table.close();//finally    }

Step 9:
Phonetest.java——getRows()

public void getRows()throws Exception{        HTable table =new HTable(config, "t_phone_cdr");        String start ="18501796701_"+(Long.MAX_VALUE-20160930235959l);        String end ="18501796701_"+  (Long.MAX_VALUE-20160901000000l);        Scan scan =new Scan();//      scan.addFamily("cf2".getBytes());        scan.setStartRow(start.getBytes());        scan.setStopRow(end.getBytes());        ResultScanner ress = table.getScanner(scan);//result 相当于hbase表中一行数据        for(Result res :ress){//          res.listCells();//返回当前行下的所有cell            System.out.println(new String( CellUtil.cloneValue(res.getColumnLatestCell(cf, "dest".getBytes()))));            System.out.println(new String( CellUtil.cloneValue(res.getColumnLatestCell(cf, "type".getBytes()))));            System.out.println(new String( CellUtil.cloneValue(res.getColumnLatestCell(cf, "city".getBytes()))));        }        table.close();//finally    }

Step 10:
Phonetest.java——getRows2()

public void getRows2()throws Exception{        HTable table =new HTable(config, "t_phone_cdr");        String phone ="18501796701";        Scan scan =new Scan();        PrefixFilter f1 =new PrefixFilter("18501796701".getBytes());        SingleColumnValueFilter f2 =new SingleColumnValueFilter(cf, "type".getBytes(), CompareOp.EQUAL, "0".getBytes());        FilterList fl =new FilterList();        fl.addFilter(f1);        fl.addFilter(f2);        scan.setFilter(fl);        ResultScanner ress = table.getScanner(scan);//result 相当于hbase表中一行数据        for(Result res :ress){            System.out.println(new String( CellUtil.cloneValue(res.getColumnLatestCell(cf, "dest".getBytes()))));            System.out.println(new String( CellUtil.cloneValue(res.getColumnLatestCell(cf, "type".getBytes()))));            System.out.println(new String( CellUtil.cloneValue(res.getColumnLatestCell(cf, "city".getBytes()))));        }        table.close();//finally    }

Step 11:

private String getPhone(String prefix){        return prefix + String.format("%08d", r.nextInt(100000000));    }    private String getDate(String year){        return year + String.format("%02d%02d%02d%02d%02d", new Object[]{r.nextInt(12)+1,r.nextInt(31)+1,r.nextInt(24),r.nextInt(60),r.nextInt(60)});    }    private String getDay(String year){        return year + String.format("%02d%02d", new Object[]{r.nextInt(12)+1,r.nextInt(31)+1});    }    private String getDayDate(String day){        return day + String.format("%02d%02d%02d", new Object[]{r.nextInt(24),r.nextInt(60),r.nextInt(60)});    }    @Test    public void test1(){        System.out.println(getDate("2016"));        System.out.println(getPhone("186"));    }
原创粉丝点击