HBASE常用JAVA API

根据rowkey进行get

1
Configuration conf = HBaseConfiguration.create(); //创建配置实例 2 HTable table = new HTable(conf,"EVENT_LOG_YZ"); //初始化一个新的表引用 3 Get get = new Get(Bytes.toBytes("nz-NE02967280231-1")); //使用一个指定的行键(即rowkey)构建一个get实例 4 get.addColumn(Bytes.toBytes("f1"), Bytes.toBytes("YZYT_msg_type")); //向get实例中添加一个列,f1是列族,YZYT_msg_type是列名 5 Result result = table.get(get); //从hbase中获取指定行的列数据 6 byte [] val = result.getValue(Bytes.toBytes("f1"), Bytes.toBytes("YZYT_msg_type")); //从返回的结果中获取对应列的数据 7 System.out.println(Bytes.toString(val)); //将数据转化为字符串打印输出

 

根据rowkey的前缀进行get,也相当于scan,较慢

1
Scan scan = new Scan(); 2 scan.setFilter(new PrefixFilter(Bytes.toBytes(express_nbr))); 3 ResultScanner resultScanner = null; 4 HTableInterface eventLogTable = null; 5 List<String> phone_list = new ArrayList<String>(); 6 try { 7 eventLogTable = hbaseHelper.getConnection().getTable("CDPOST_YZYT"); 8 resultScanner = eventLogTable.getScanner(scan); 9 Iterator<Result> it = resultScanner.iterator(); 10 while (it.hasNext()) { 11 Result result = it.next(); 12 String[] phone_num = Bytes.toString(result.getRow()).split("-"); 13 if (!phone_list.contains(phone_num[phone_num.length - 1])) { 14 phone_list.add(phone_num[phone_num.length - 1]); 15 } 16 } 17 } catch (IOException e) { 18 logger.error("消息处理异常", e); 19 } finally { 20 if (resultScanner != null) { 21 resultScanner.close(); 22 } 23 if (eventLogTable != null) { 24 try { 25 eventLogTable.close(); 26 } catch (IOException e) { 27 } 28 } 29 }

 

 

hbase的put操作

1
Configuration conf_hbase = HBaseConfiguration.create(); //创建所需的配置 2 HTable table_express = new HTable(conf_hbase , "EXPRESS_YZYT"); //实例化一个新的客户端 3 String cloumn1 = "TelephoneNbr"; 4 String cloumn2 = "ExpressNbr"; 5 String cloumn3 = "CardNum"; 6 String cloumn4 = "ExpressType"; 7 8 Put put_express = new Put(ExpressNum.getBytes()); //指定一行来创建一个put 9 put_express.add("f1".getBytes(), cloumn1.getBytes(),TelephoneNum.getBytes()); //向put中添加一个column1(TelephoneNbr)的列,列簇为f1 10 put_express.add("f1".getBytes(), cloumn3.getBytes(), CardNo.getBytes()); 11 put_express.add("f1".getBytes(), cloumn4.getBytes(),ExpressType.getBytes()); 12 13 table_express.put(put_express); //将这一行存储到hbase表中

 

 //根据时间戳进行扫描过滤
1
Configuration conf = HBaseConfiguration.create(); 2 HTable table = new HTable(conf,"EVENT_LOG_YZ"); 3 Scan scan = new Scan(); 4 scan.setTimeRange(NumberUtils.toLong("1466646234237"), NumberUtils.toLong("1466646529902")); 5 6 ResultScanner resultScanner = null; 7 8 try { 9 10 resultScanner = table.getScanner(scan); 11 Iterator<Result> it = resultScanner.iterator(); 12 while (it.hasNext()) { 13 Result result = it.next(); 14 String phone_num = Bytes.toString(result.getRow()); 15 System.out.println(phone_num); 16 } 17 } catch (IOException e) { 18 19 } finally { 20 if (resultScanner != null) { 21 resultScanner.close(); 22 } 23 if (table != null) { 24 try { 25 table.close(); 26 } catch (IOException e) { 27 } 28 } 29 }

 

posted @ 2016-06-15 10:03  日月玉朱猪  阅读(338)  评论(0编辑  收藏  举报