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")); }
阅读全文
0 0
- HBase之Protocolbuffer应用
- ProtocolBuffer
- hbase for hadoop2.X 使用 protocolbuffer 自定义filter
- ProtocolBuffer之Windows配置&C++实例
- iOS之ProtocolBuffer搭建和示例demo
- iOS之ProtocolBuffer搭建和示例demo
- Hadoop学习笔记之HBase应用场景
- Hadoop之Hbase数据库的应用
- Hadoop之Hbase架构以及应用介绍
- [HBase] HBase 之TableOutputFormat
- ZooKeeper在大型分布式系统中的应用之HBase。
- HBase学习之三: hbase filter(过滤器)和coprocessor(协处理器)统计行数的简单应用
- 关于protocolbuffer
- protocolbuffer初探
- protocolBuffer 说明
- protocolBuffer使用
- 浅谈protocolbuffer
- Protocolbuffer配置
- Meta标签中的format-detection属性及含义和移动端的一些事项
- 剑指offer-二叉搜索树的后续遍历序列
- ExtJs之下拉框
- Swift学习笔记第三篇(字符串,数组,集合,字典)
- Retrofit的注解使用
- HBase之Protocolbuffer应用
- 总有你要的编程书单
- linux中Makefile的编写
- 杂记----有道词典更新
- java后端生成图片验证码并转化成base64
- 二进制 八进制 十进制 十六进制
- /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not found错误的解决
- [一天几个linux命令] Ubuntu下给Eclipse增加快捷方式
- ClassLoader()