一.前述。
Hbase shell启动命令窗口,然后再Hbase shell中对应的api命令如下。

二.说明
Hbase shell中删除键是空格+Ctrl键。
三.代码
1.封装所有的API
1package com.sxt.hbase; 2 3import java.io.IOException; 4import java.util.ArrayList; 5import java.util.List; 6 7import org.apache.hadoop.conf.Configuration; 8import org.apache.hadoop.hbase.Cell; 9import org.apache.hadoop.hbase.CellUtil; 10import org.apache.hadoop.hbase.HColumnDescriptor; 11import org.apache.hadoop.hbase.HTableDescriptor; 12import org.apache.hadoop.hbase.KeyValue; 13import org.apache.hadoop.hbase.MasterNotRunningException; 14import org.apache.hadoop.hbase.TableName; 15import org.apache.hadoop.hbase.ZooKeeperConnectionException; 16import org.apache.hadoop.hbase.client.Delete; 17import org.apache.hadoop.hbase.client.Get; 18import org.apache.hadoop.hbase.client.HBaseAdmin; 19import org.apache.hadoop.hbase.client.HConnection; 20import org.apache.hadoop.hbase.client.HConnectionManager; 21import org.apache.hadoop.hbase.client.HTable; 22import org.apache.hadoop.hbase.client.HTableInterface; 23import org.apache.hadoop.hbase.client.HTablePool; 24import org.apache.hadoop.hbase.client.Put; 25import org.apache.hadoop.hbase.client.Result; 26import org.apache.hadoop.hbase.client.ResultScanner; 27import org.apache.hadoop.hbase.client.Scan; 28import org.apache.hadoop.hbase.filter.BinaryComparator; 29import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; 30import org.apache.hadoop.hbase.filter.Filter; 31import org.apache.hadoop.hbase.filter.FilterList; 32import org.apache.hadoop.hbase.filter.PrefixFilter; 33import org.apache.hadoop.hbase.filter.RowFilter; 34import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; 35import org.apache.hadoop.hbase.filter.SubstringComparator; 36import org.apache.hadoop.hbase.util.Bytes; 37import org.junit.Test; 38 39 40public class HBaseDAOImp { 41 42 HConnection hTablePool = null; 43 static Configuration conf =null; 44 public HBaseDAOImp() 45 { 46 conf = new Configuration(); 47 String zk_list = "node1,node2,node3";//只指定zookeeper集群即可,因为在zookeeper中存贮所有Region的寻址入口,而客户端只需要知道需要存储的具体位置后, //即可请求对应的regionServer上去写入数据。 48 conf.set("hbase.zookeeper.quorum", zk_list); 49 try { 50 hTablePool = HConnectionManager.createConnection(conf) ;//申请一个HTablePool可以解决HTable存在的线程不安全问题, // 同时通过维护固定数量的HTable对象,能够在程序运行期间复用这些HTable资源对象 51 } catch (IOException e) { 52 e.printStackTrace(); 53 } 54 } 55 public void save(Put put, String tableName) { 56 // TODO Auto-generated method stub 57 HTableInterface table = null; 58 try { 59 table = hTablePool.getTable(tableName) ; 60 table.put(put) ; 61 62 } catch (Exception e) { 63 e.printStackTrace() ; 64 }finally{ 65 try { 66 table.close() ; 67 } catch (IOException e) { 68 e.printStackTrace(); 69 } 70 } 71 } 72 73 /** 74 * 插入一个cell 75 * @param tableName 76 * @param rowKey 77 * @param family 78 * @param quailifer 79 * @param value 80 */ 81 public void insert(String tableName, String rowKey, String family, 82 String quailifer, String value) { 83 // TODO Auto-generated method stub 84 HTableInterface table = null; 85 try { 86 table = hTablePool.getTable(tableName) ;//针对哪张表操作 87 Put put = new Put(rowKey.getBytes());//增添数据通过Put对象操作,添加一条rowkey 88 put.add(family.getBytes(), quailifer.getBytes(), value.getBytes()) ;//添加哪个列族,列,value值。 89 table.put(put);//放置到hbase的对象中去。 90 } catch (Exception e) { 91 e.printStackTrace(); 92 }finally 93 { 94 try { 95 table.close() ; 96 } catch (IOException e) { 97 e.printStackTrace(); 98 } 99 } 100 } 101 102 /** 103 * 在一个列族下插入多个单元格 104 * @param tableName 105 * @param rowKey 106 * @param family 107 * @param quailifer 108 * @param value 109 */ 110 public void insert(String tableName,String rowKey,String family,String quailifer[],String value[]) 111 { 112 HTableInterface table = null; 113 try { 114 table = hTablePool.getTable(tableName) ; 115 Put put = new Put(rowKey.getBytes()); 116 // 批量添加 117 for (int i = 0; i < quailifer.length; i++) { 118 String col = quailifer[i]; 119 String val = value[i]; 120 put.add(family.getBytes(), col.getBytes(), val.getBytes()); 121 } 122 table.put(put); 123 } catch (Exception e) { 124 e.printStackTrace(); 125 }finally 126 { 127 try { 128 table.close() ; 129 } catch (IOException e) { 130 e.printStackTrace(); 131 } 132 } 133 } 134 public void save(List<Put> Put, String tableName) { 135 // TODO Auto-generated method stub 136 HTableInterface table = null; 137 try { 138 table = hTablePool.getTable(tableName) ; 139 table.put(Put) ; 140 } 141 catch (Exception e) { 142 // TODO: handle exception 143 }finally 144 { 145 try { 146 table.close() ; 147 } catch (IOException e) { 148 e.printStackTrace(); 149 } 150 } 151 152 } 153 154 155 public Result getOneRow(String tableName, String rowKey) { 156 // TODO Auto-generated method stub 157 HTableInterface table = null; 158 Result rsResult = null; 159 try { 160 table = hTablePool.getTable(tableName) ; 161 Get get = new Get(rowKey.getBytes()) ;/通过get获取一条数据 162 rsResult = table.get(get) ;//返回一个result对象 163 } catch (Exception e) { 164 e.printStackTrace() ; 165 } 166 finally 167 { 168 try { 169 table.close() ; 170 } catch (IOException e) { 171 e.printStackTrace(); 172 } 173 } 174 return rsResult; 175 } 176 177 /** 178 * 最常用的方法,优化查询 179 * 查询一行数据, 180 * @param tableName 181 * @param rowKey 182 * @param cols 183 * @return 184 */ 185 public Result getOneRowAndMultiColumn(String tableName, String rowKey,String[] cols) { 186 // TODO Auto-generated method stub 187 HTableInterface table = null; 188 Result rsResult = null; 189 try { 190 table = hTablePool.getTable(tableName) ; 191 Get get = new Get(rowKey.getBytes()) ; 192 for (int i = 0; i < cols.length; i++) { 193 get.addColumn("cf".getBytes(), cols[i].getBytes()) ; 194 } 195 rsResult = table.get(get) ; 196 } catch (Exception e) { 197 e.printStackTrace() ; 198 } 199 finally 200 { 201 try { 202 table.close() ; 203 } catch (IOException e) { 204 e.printStackTrace(); 205 } 206 } 207 return rsResult; 208 } 209 210 211 public List<Result> getRows(String tableName, String rowKeyLike) { 212 // TODO Auto-generated method stub 213 HTableInterface table = null; 214 List<Result> list = null; 215 try { 216 FilterList fl = new FilterList(FilterList.Operator.MUST_PASS_ALL); 217 table = hTablePool.getTable(tableName) ; 218 PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); 219 SingleColumnValueFilter filter1 = new SingleColumnValueFilter( 220 "order".getBytes(), 221 "order_type".getBytes(), 222 CompareOp.EQUAL, 223 Bytes.toBytes("1") 224 ); 225 fl.addFilter(filter); 226 fl.addFilter(filter1); 227 Scan scan = new Scan(); 228 scan.setFilter(fl); 229 ResultScanner scanner = table.getScanner(scan) ; 230 list = new ArrayList<Result>() ; 231 for (Result rs : scanner) { 232 list.add(rs) ; 233 } 234 } catch (Exception e) { 235 e.printStackTrace() ; 236 } 237 finally 238 { 239 try { 240 table.close() ; 241 } catch (IOException e) { 242 e.printStackTrace(); 243 } 244 } 245 return list; 246 } 247 248 249 public List<Result> getRows(String tableName, String rowKeyLike ,String cols[]) { 250 // TODO Auto-generated method stub 251 HTableInterface table = null; 252 List<Result> list = null; 253 try { 254 table = hTablePool.getTable(tableName) ; 255 PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); 256 257 Scan scan = new Scan(); 258 for (int i = 0; i < cols.length; i++) { 259 scan.addColumn("cf".getBytes(), cols[i].getBytes()) ; 260 } 261 scan.setFilter(filter); 262 ResultScanner scanner = table.getScanner(scan) ; 263 list = new ArrayList<Result>() ; 264 for (Result rs : scanner) { 265 list.add(rs) ; 266 } 267 } catch (Exception e) { 268 e.printStackTrace() ; 269 } 270 finally 271 { 272 try { 273 table.close() ; 274 } catch (IOException e) { 275 e.printStackTrace(); 276 } 277 } 278 return list; 279 } 280 281 public List<Result> getRowsByOneKey(String tableName, String rowKeyLike ,String cols[]) { 282 // TODO Auto-generated method stub 283 HTableInterface table = null; 284 List<Result> list = null; 285 try { 286 table = hTablePool.getTable(tableName) ; 287 PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); 288 289 Scan scan = new Scan(); 290 for (int i = 0; i < cols.length; i++) { 291 scan.addColumn("cf".getBytes(), cols[i].getBytes()) ; 292 } 293 scan.setFilter(filter); 294 ResultScanner scanner = table.getScanner(scan) ; 295 list = new ArrayList<Result>() ; 296 for (Result rs : scanner) { 297 list.add(rs) ; 298 } 299 } catch (Exception e) { 300 e.printStackTrace() ; 301 } 302 finally 303 { 304 try { 305 table.close() ; 306 } catch (IOException e) { 307 e.printStackTrace(); 308 } 309 } 310 return list; 311 } 312 313 /** 314 * 范围查询 315 * @param tableName 316 * @param startRow 317 * @param stopRow 318 * @return 319 */ 320 public List<Result> getRows(String tableName,String startRow,String stopRow) 321 { 322 HTableInterface table = null; 323 List<Result> list = null; 324 try { 325 table = hTablePool.getTable(tableName) ; 326 Scan scan = new Scan() ; 327 scan.setStartRow(startRow.getBytes()) ; 328 scan.setStopRow(stopRow.getBytes()) ; 329 ResultScanner scanner = table.getScanner(scan) ; 330 list = new ArrayList<Result>() ; 331 for (Result rsResult : scanner) { 332 list.add(rsResult) ; 333 } 334 335 }catch (Exception e) { 336 e.printStackTrace() ; 337 } 338 finally 339 { 340 try { 341 table.close() ; 342 } catch (IOException e) { 343 e.printStackTrace(); 344 } 345 } 346 return list; 347 } 348 349 350 public void deleteRecords(String tableName, String rowKeyLike){ 351 HTableInterface table = null; 352 try { 353 table = hTablePool.getTable(tableName) ; 354 PrefixFilter filter = new PrefixFilter(rowKeyLike.getBytes()); 355 Scan scan = new Scan(); 356 scan.setFilter(filter); 357 ResultScanner scanner = table.getScanner(scan) ; 358 List<Delete> list = new ArrayList<Delete>() ; 359 for (Result rs : scanner) { 360 Delete del = new Delete(rs.getRow()); 361 list.add(del) ; 362 } 363 table.delete(list); 364 } 365 catch (Exception e) { 366 e.printStackTrace() ; 367 } 368 finally 369 { 370 try { 371 table.close() ; 372 } catch (IOException e) { 373 e.printStackTrace(); 374 } 375 } 376 377 } 378 379 public void deleteCell(String tableName, String rowkey,String cf,String column){ 380 HTableInterface table = null; 381 try { 382 table = hTablePool.getTable(tableName) ; 383 Delete del = new Delete(rowkey.getBytes()); 384 del.deleteColumn(cf.getBytes(), column.getBytes()); 385 table.delete(del); 386 } 387 catch (Exception e) { 388 e.printStackTrace() ; 389 } 390 finally 391 { 392 try { 393 table.close() ; 394 } catch (IOException e) { 395 e.printStackTrace(); 396 } 397 } 398 399 } 400 401 public void createTable(String tableName, String[] columnFamilys){ 402 try { 403 // admin 对象 404 HBaseAdmin admin = new HBaseAdmin(conf); 405 if (admin.tableExists(tableName)) { 406 System.err.println("此表,已存在!"); 407 } else { 408 HTableDescriptor tableDesc = new HTableDescriptor( 409 TableName.valueOf(tableName)); 410 411 for (String columnFamily : columnFamilys) { 412 tableDesc.addFamily(new HColumnDescriptor(columnFamily)); 413 } 414 415 admin.createTable(tableDesc); 416 System.err.println("建表成功!"); 417 418 } 419 admin.close();// 关闭释放资源 420 } catch (MasterNotRunningException e) { 421 // TODO Auto-generated catch block 422 e.printStackTrace(); 423 } catch (ZooKeeperConnectionException e) { 424 // TODO Auto-generated catch block 425 e.printStackTrace(); 426 } catch (IOException e) { 427 // TODO Auto-generated catch block 428 e.printStackTrace(); 429 } 430 431 } 432 433 /** 434 * 删除一个表 435 * 436 * @param tableName 437 * 删除的表名 438 * */ 439 public void deleteTable(String tableName) { 440 try { 441 HBaseAdmin admin = new HBaseAdmin(conf); 442 if (admin.tableExists(tableName)) { 443 admin.disableTable(tableName);// 禁用表 444 admin.deleteTable(tableName);// 删除表 445 System.err.println("删除表成功!"); 446 } else { 447 System.err.println("删除的表不存在!"); 448 } 449 admin.close(); 450 } catch (MasterNotRunningException e) { 451 // TODO Auto-generated catch block 452 e.printStackTrace(); 453 } catch (ZooKeeperConnectionException e) { 454 // TODO Auto-generated catch block 455 e.printStackTrace(); 456 } catch (IOException e) { 457 // TODO Auto-generated catch block 458 e.printStackTrace(); 459 } 460 } 461 462 /** 463 * 查询表中所有行 464 * @param tablename 465 */ 466 public void scaner(String tablename) { 467 try { 468 HTable table =new HTable(conf, tablename); 469 Scan s =new Scan(); 470// s.addColumn(family, qualifier) 471// s.addColumn(family, qualifier) 472 ResultScanner rs = table.getScanner(s); 473 for (Result r : rs) { 474 475 for(Cell cell:r.rawCells()){ 476 System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); 477 System.out.println("Timetamp:"+cell.getTimestamp()+" "); 478 System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); 479 System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); 480 System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); 481 } 482 } 483 } catch (IOException e) { 484 e.printStackTrace(); 485 } 486 } 487 public void scanerByColumn(String tablename) { 488 489 try { 490 HTable table =new HTable(conf, tablename); 491 Scan s =new Scan(); 492 s.addColumn("cf".getBytes(), "201504052237".getBytes()); 493 s.addColumn("cf".getBytes(), "201504052237".getBytes()); 494 ResultScanner rs = table.getScanner(s); 495 for (Result r : rs) { 496 497 for(Cell cell:r.rawCells()){ 498 System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); 499 System.out.println("Timetamp:"+cell.getTimestamp()+" "); 500 System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); 501 System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); 502 System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); 503 } 504 } 505 } catch (IOException e) { 506 e.printStackTrace(); 507 } 508 } 509 public static void main(String[] args) { 510 511 512 513 514// 创建表 515// String tableName="test"; 516// String cfs[] = {"cf"}; 517// dao.createTable(tableName,cfs); 518 519// 存入一条数据 520// Put put = new Put("bjsxt".getBytes()); 521// put.add("cf".getBytes(), "name".getBytes(), "cai10".getBytes()) ; 522// dao.save(put, "test") ; 523 524// 插入多列数据 525// Put put = new Put("bjsxt".getBytes()); 526// List<Put> list = new ArrayList<Put>(); 527// put.add("cf".getBytes(), "addr".getBytes(), "shanghai1".getBytes()) ; 528// put.add("cf".getBytes(), "age".getBytes(), "30".getBytes()) ; 529// put.add("cf".getBytes(), "tel".getBytes(), "13889891818".getBytes()) ; 530// list.add(put) ; 531// dao.save(list, "test"); 532 533// 插入单行数据 534// dao.insert("test", "testrow", "cf", "age", "35") ; 535// dao.insert("test", "testrow", "cf", "cardid", "12312312335") ; 536// dao.insert("test", "testrow", "cf", "tel", "13512312345") ; 537 538 539 540// List<Result> list = dao.getRows("test", "testrow",new String[]{"age"}) ; 541// for(Result rs : list) 542// { 543// for(Cell cell:rs.rawCells()){ 544// System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); 545// System.out.println("Timetamp:"+cell.getTimestamp()+" "); 546// System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); 547// System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); 548// System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); 549// } 550// } 551 552// Result rs = dao.getOneRow("test", "testrow"); 553// System.out.println(new String(rs.getValue("cf".getBytes(), "age".getBytes()))); 554 555// Result rs = dao.getOneRowAndMultiColumn("cell_monitor_table", "29448-513332015-04-05", new String[]{"201504052236","201504052237"}); 556// for(Cell cell:rs.rawCells()){ 557// System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" "); 558// System.out.println("Timetamp:"+cell.getTimestamp()+" "); 559// System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" "); 560// System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" "); 561// System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" "); 562// } 563 564// dao.deleteTable("cell_monitor_table"); 565// 创建表 566 String tableName="cell_monitor_table"; 567 String cfs[] = {"cf"}; 568// dao.createTable(tableName,cfs); 569 } 570 571 572 public static void testRowFilter(String tableName){ 573 try { 574 HTable table =new HTable(conf, tableName); 575 Scan scan = new Scan(); 576 scan.addColumn(Bytes.toBytes("column1"),Bytes.toBytes("qqqq")); 577 Filter filter1 = new RowFilter(CompareOp.LESS_OR_EQUAL,new BinaryComparator(Bytes.toBytes("laoxia157"))); 578 scan.setFilter(filter1); 579 ResultScanner scanner1 = table.getScanner(scan); 580 for (Result res : scanner1) { 581 System.out.println(res); 582 } 583 scanner1.close(); 584 585// 586// Filter filter2 = new RowFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator("laoxia4\\d{2}")); 587// scan.setFilter(filter2); 588// ResultScanner scanner2 = table.getScanner(scan); 589// for (Result res : scanner2) { 590// System.out.println(res); 591// } 592// scanner2.close(); 593 594 Filter filter3 = new RowFilter( CompareOp.EQUAL,new SubstringComparator("laoxia407")); 595 scan.setFilter(filter3); 596 ResultScanner scanner3 = table.getScanner(scan); 597 for (Result res : scanner3) { 598 System.out.println(res); 599 } 600 scanner3.close(); 601 } catch (IOException e) { 602 // TODO Auto-generated catch block 603 e.printStackTrace(); 604 } 605 } 606 607 @Test 608 public void testTrasaction(){ 609 try{ 610 HTableInterface table = null; 611 table = hTablePool.getTable("t_test".getBytes()); 612// Put put1 =new Put("002".getBytes()); 613// put1.add("cf1".getBytes(), "name".getBytes(), "王五".getBytes()); 614// table.put(put1); 615 Put newput =new Put("001".getBytes()); 616 newput.add("cf1".getBytes(), "like".getBytes(), "看书".getBytes()); 617 618 boolean f= table.checkAndPut("001".getBytes(), "cf1".getBytes(), "age".getBytes(), "24".getBytes(), newput); 619 System.out.println(f); 620 621 }catch (Exception e){ 622 e.printStackTrace(); 623 } 624 625 } 626}
2.原始Api
1package com.sxt.hbase; 2 3import java.io.IOException; 4import java.text.SimpleDateFormat; 5import java.util.ArrayList; 6import java.util.List; 7import java.util.Random; 8 9import org.apache.hadoop.conf.Configuration; 10import org.apache.hadoop.hbase.Cell; 11import org.apache.hadoop.hbase.CellUtil; 12import org.apache.hadoop.hbase.HColumnDescriptor; 13import org.apache.hadoop.hbase.HTableDescriptor; 14import org.apache.hadoop.hbase.TableName; 15import org.apache.hadoop.hbase.client.Get; 16import org.apache.hadoop.hbase.client.HBaseAdmin; 17import org.apache.hadoop.hbase.client.HTable; 18import org.apache.hadoop.hbase.client.Put; 19import org.apache.hadoop.hbase.client.Result; 20import org.apache.hadoop.hbase.client.ResultScanner; 21import org.apache.hadoop.hbase.client.Scan; 22import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; 23import org.apache.hadoop.hbase.filter.FilterList; 24import org.apache.hadoop.hbase.filter.PrefixFilter; 25import org.apache.hadoop.hbase.filter.SingleColumnValueFilter; 26import org.junit.After; 27import org.junit.Before; 28import org.junit.Test; 29 30public class HBaseDemo { 31 32 Configuration conf; 33 HBaseAdmin admin; 34 HTable htable; 35 byte[] family = "cf".getBytes(); 36 37 // 1、删除 cell?? 38 // 2、表设计 39 40 41 String TN = "phone"; 42 43 @Before 44 public void begin() throws Exception { 45 conf = new Configuration(); 46 47 // 分布式hbase zk列表指定zk集群 48 conf.set("hbase.zookeeper.quorum", "node05"); 49 50 admin = new HBaseAdmin(conf);//通过admin对象操作DDL语言 51 htable = new HTable(conf, TN);//通过Htable对象操作表DML语言 52 } 53 54 @After 55 public void end() throws Exception { 56 if(admin != null) { 57 admin.close(); 58 } 59 if(htable != null) { 60 htable.close(); 61 } 62 } 63 64 @Test 65 public void createTbl() throws Exception { 66 if(admin.tableExists(TN)) { 67 admin.disableTable(TN); 68 admin.deleteTable(TN); 69 } 70 71 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(TN));//表的描述 72 73 HColumnDescriptor cf = new HColumnDescriptor("cf"); 74 cf.setInMemory(true);//设置读缓存 75 cf.setMaxVersions(1); 76 77 desc.addFamily(cf);//创建表的时候必须制定列族,相当于一个DDL语言描述。 78 79 admin.createTable(desc); 80 } 81 82 @Test 83 public void insertDB1() throws Exception { 84 String rowkey = "123"; 85 86 Put put = new Put(rowkey.getBytes()); 87 put.add("cf".getBytes(), "name".getBytes(), "xiaoming".getBytes()); 88 put.add("cf".getBytes(), "sex".getBytes(), "man".getBytes()); 89 90 htable.put(put); 91 } 92 93 @Test 94 public void getDB1() throws Exception { 95 String rowkey = "123"; 96 Get get = new Get(rowkey.getBytes()); 97 get.addColumn("cf".getBytes(), "name".getBytes()); 98 99 Result rs = htable.get(get); 100 Cell cell = rs.getColumnLatestCell("cf".getBytes(), "name".getBytes());//result返回的是一个Cell对象 101 102 System.out.println(new String(CellUtil.cloneValue(cell)));//取出Cell对象中的值 103 } 104 105 /** 106 * 通话详单 107 * 包含:手机号,对方手机号,日期,通话时长,主叫被叫类型... 108 * 109 * Rowkey设计:手机号_(Long.Max-时间戳) 110 * 111 * 1、查询某个月份 的 所有的通话详单 时间降序 112 * 113 * 2、查询某个手机号 所有主叫类型 通话记录 114 * @throws Exception 115 */ 116 117 HTools t = new HTools(); 118 119 Random r = new Random(); 120 121 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); 122 123 /** 124 * 生成测试数据 125 * 126 * 十个用户 产生一百条通话记录 127 */ 128 @Test 129 public void insertDB2() throws Exception { 130 131 132 List<Put> puts = new ArrayList<Put>(); 133 134 for (int i = 0; i < 10; i++) { 135 String pnum = t.getPhoneNum("186"); 136 137 for (int j = 0; j < 100; j++) { 138 String dnum = t.getPhoneNum("177"); 139 String datestr = t.getDate("2018"); 140 String length = r.nextInt(99) + ""; 141 String type = r.nextInt(2) + ""; 142 143 String rowkey = pnum + "_" + (Long.MAX_VALUE-sdf.parse(datestr).getTime());//默认Hbase是按照row_key的字典升序排列,此处降序。 144 145 Put put = new Put(rowkey.getBytes()); 146 147 put.add(family, "dnum".getBytes(), dnum.getBytes()); 148 put.add(family, "date".getBytes(), datestr.getBytes()); 149 put.add(family, "length".getBytes(), length.getBytes()); 150 put.add(family, "type".getBytes(), type.getBytes()); 151 152 puts.add(put); 153 } 154 } 155 156 htable.put(puts); 157 } 158 159 /** 160 * 查询某个手机号 某个月份所有的通话记录 161 * 范围 162 * @throws Exception 163 */ 164 @Test 165 public void scanDB1() throws Exception { 166 Scan scan = new Scan(); 167 168 String pnum = "18692739289_"; 169 170 String startRowkey = pnum + (Long.MAX_VALUE-sdf.parse("20181001000000").getTime()); 171 String stopRowkey = pnum + (Long.MAX_VALUE-sdf.parse("20180901000000").getTime()); 172 173 scan.setStartRow(startRowkey.getBytes()); 174 scan.setStopRow(stopRowkey.getBytes());//scan操作设置起始和结束的rowkey 175 176 ResultScanner rss = htable.getScanner(scan);//返回一个Result集合。 177 for (Result rs : rss) {//遍历Result集合 178 System.out.print(new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "dnum".getBytes()))));//得到一条条数据。 179 System.out.print(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "date".getBytes())))); 180 System.out.print(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "type".getBytes())))); 181 System.out.println(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "length".getBytes())))); 182 } 183 } 184 185 /** 186 * 查询某个手机号 所有的主叫type=1 187 * 过滤器 188 * @throws Exception 189 */ 190 @Test 191 public void scanDB2() throws Exception { 192 Scan scan = new Scan(); 193 194 FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ALL);//定义多个过滤器,必须全部筛选 195 196 PrefixFilter filter1 = new PrefixFilter("18692739289".getBytes());//前缀过滤器,先返回所有以这个判定条件的rowkey 197 list.addFilter(filter1); 198 199 SingleColumnValueFilter filter2 = new SingleColumnValueFilter(family, //列值过滤器,因为是两个查询条件,所以需要两个过滤器 200 "type".getBytes(), CompareOp.EQUAL, "1".getBytes()); //判断所有type等于1的值 201 202 list.addFilter(filter2);//加第二个过滤器 203 204 scan.setFilter(list); 205 206 ResultScanner rss = htable.getScanner(scan); 207 for (Result rs : rss) { 208 System.out.print(new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "dnum".getBytes())))); 209 System.out.print(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "date".getBytes())))); 210 System.out.print(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "type".getBytes())))); 211 System.out.println(" - " + new String(CellUtil.cloneValue(rs.getColumnLatestCell(family, "length".getBytes())))); 212 } 213 214 } 215 216}
解析:
1.
String rowkey **= pnum + "_" + (Long.MAX_VALUE-**sdf.parse(datestr).getTime());//默认Hbase是按照row_key的字典升序排列,此处降序。时间戳越来越大,则此数越来越小,达到按照时间降序的结果。

2.过滤器介绍
1FilterList代表一个过滤器列表 2 FilterList.Operator.MUST_PASS_ALL --> 取交集 相当一and操作 3 FilterList.Operator.MUST_PASS_ONE --> 取并集 相当于or 操作 4 FilterList list = new FilterList(FilterList.Operator.MUST_PASS_ONE); 5 2、SingleColumnValueFilter 列值过滤器 6 ColumnPrefixFilter用于指定列名前缀值相等 7 MultipleColumnPrefixFilter和ColumnPrefixFilter行为差不多,但可以指定多个前缀。 8 QualifierFilter是基于列名的过滤器。 9 2、RowFilter 行过滤器 10 RegexStringComparator是支持正则表达式的比较器。 11 SubstringComparator用于检测一个子串是否存在于值中,大小写不敏感。